Php object-oriented – inheritance and overriding
Inheritance:
In PHP, the purpose is achieved by using special operations on classes.
Use extends to indicate which class object the current class object inherits when defining a class.
Example:
class C
{
public $p_c = “value c”;
}
class D extends C
{
public $p_d = “value d”;
}
$o = new D;
var_dump($o->p_c)
var_dump($o->p_d)
Output: string(7) "value c" string(7) "value d"
Inheritance refers to between two objects, so where are these two objects?
Instanceof operator (determines whether the object is an instance of a certain class)
var_dump($o instanceof D);
Output: bool(true)
var_dump($o instanceof C);
Output: bool(true)
Therefore, an object is an instance of the current class and an instance of the class it inherits.
Class D extends C
D class object, inherited from C class object.
Parent class: inherited class, class C
Subclass: the class that needs to be inherited, class D
Base class: Class C is the base class of class D
Extension class: Class D is an extension class of class C.
Important:
php is single inheritance.
Purpose of inheritance:
It lies in extending, or using certain types of existing operations and data.
override
When inheriting, if a member conflict occurs, PHP's handling method is to rewrite. That is, members of the subclass with the same name override members of the parent class with the same name. Members of the parent class with the same name cannot be seen.
Example:
1.
class P
{
public $name = ‘P’;
}
class C extends P
{
public $name = “C”;
}
$o = new C;
echo $o->name;
2.
class P
{
public $name = ‘P’;
public function sayName()
{
echo ‘parent::name’,$this->name;
}
}
class C extends P
{
public $name = “C”;
public function sayName()
{
echo ‘self::name’,$this->name;
}
}
$o = new C;
$o->sayName();
Output: self::name C
Constructor method overriding:
Example:
class P
{
public__construct()
{
echo "parent::construct";
}
}
class D extends P
{
public__construct()
{
echo "self::construct";
}
}
$o =new D;
Output: self::construct
If necessary, to force the execution of the overridden parent class method, you can explicitly use the parent class to call the corresponding parent class method:
Example:
class P
{
public__construct()
{
echo "parent::construct";
}
}
class D extends P
{
public__construct()
{
P::__construct();
echo "self::construct";
}
}
$o =new D;
Output: parent::construct self::construct
You can use a keyword within a class to replace the current parent class
parent keyword
Example:
class P
{
public__construct()
{
echo "parent::construct";
}
}
class D extends P
{
public__construct()
{
parent::__construct();
echo "self::construct";
}
}
$o =new D;
If the construction of the parent class requires corresponding parameters, the parameters required by the parent class's construction method need to be passed into the method when calling.
Example:
class Goods
{
public $goods_name ;
public $goods_price;
public function __construct($name,$price)
{
$this->goods_name= $name;
$this->goods_price= $price;
}
}
class GoodsBook extends Goods
{
public $pages;
public function __construct($name,$price,$pages)
{
parent::__construct($name,$price);
$this->pages= $pages;
}
}
when defining a class