Exemplo n.º 1
0
<?php

//=====Abstract classes are those classes which can not be directly initialized
//Main motive of creating abstract classes in php is to apply restriction of direct initialization or object creation.===
//Abstract classes in php are simillar like other oop languages. You can create abstract classes in php using abstract keyword.
//Once you will make any class abstract in php you can not create object of that class.
abstract class parentClass
{
    public function addition()
    {
        echo 'Hello';
    }
}
//$onject=new parentClass();//it will give the error
class childClass extends parentClass
{
    public function subtration()
    {
        return 'Childclass subtrations';
    }
}
$objectofchildclass = new childClass();
echo $objectofchildclass->addition();
Exemplo n.º 2
0
<?php

class myClass
{
    public $name = "Matt";
    function myClass($n)
    {
        $this->name = $n;
    }
    function sayHello()
    {
        echo "HELLO! My name is " . $this->name;
    }
}
class childClass extends myClass
{
    function sayHello()
    {
        echo "I will not tell you my name.";
    }
}
$object1 = new childClass("Baby Matt");
$object1->sayHello();
Exemplo n.º 3
0
//   public function __construct()
//   {
//       echo "A new constructor in " . __CLASS__ . ".<br />";
//   }
//   public function newMethod()
//   {
//       echo "From a new method in " . __CLASS__ . ".<br />";
//   }
// }
// // Create a new object
// $newobj = new MyOtherClass;
// // Output the object as a string
// echo $newobj->newMethod();
// // Use a method from the parent class
// echo $newobj->getProperty();
class parentClass
{
    public $firstName = "Md Ariful";
    public $lastName = "Islam";
}
class childClass extends parentClass
{
    public function getName()
    {
        //return $this->firstName = $firstName;
        //return $this->lastName = $lastName;
        echo $this->firstName . " " . $this->lastName;
    }
}
$obj = new childClass();
echo $obj->getName();
Exemplo n.º 4
0
<?php

/*
* As we know that abstract functions are those functions of abstract class which is only defined.
* It will be declared in your child class. 
* You can create any method abstract using keyword abstract. 
* You can only create abstract method either in abstract class or interface. 
*/
abstract class parentClass
{
    protected abstract function xyz($a, $b);
}
class childClass extends parentClass
{
    public function xyz($name, $email)
    {
        return $name . ' ' . $email;
    }
}
$object = new childClass('Male');
echo $object->xyz('Basant', '*****@*****.**');
/*
Important:-Note
 In above code you can see that you have declare f3 function in abstract class.
 But private declaration of the abstract method will always throw error.
 Because private method is available only in the same class context. 
 But in case of xyz. This is protected. 
 Now in child class we have defined it as public because public is less restricted than protected. 
 We have defined it public because no any visibility is less restricted than public.
*/