PHP Object-oriented-inheritance and rewriting
Inherited:
In PHP, the purpose is achieved by using special operations on the class.
By defining the class, the extends is used to indicate that the current class object inherits the object of that 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, which refers to between two objects, so where are these two objects?
Instanceof operator (determines whether an object is an instance of a 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 that the class inherits from.
Class D extends C
Class D Objects, inherited from Class C objects.
Parent Class: Inherited class, Class C
Subclass: Classes that need to be inherited, Class D
Base class: Class C is the base class for Class D
Extension class: Class D is an extension class for Class C.
Important:
PHP is a single inheritance.
Purpose of Inheritance:
is to extend, or use a class of operations and data that already exists.
overriding override
When inheriting, if a member conflict occurs, the way PHP is handled is rewritten. That is, the child is a member of a similar name that overrides the parent. You cannot see a member of the same name as the parent class.
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
Construction method Overrides:
Example:
Class P
{
Public__construct ()
{
echo "Parent::construct";
}
}
Class D extends P
{
Public__construct ()
{
echo "Self::construct";
}
}
$o =new D;
Output: Self::construct
If required, enforces the overridden parent class method, which can be displayed using the parent class to invoke 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, instead of 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 structure of the parent class requires a corresponding parameter, you need to pass the parameters required by the parent class construction method to the method at the time of invocation.
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;
}
}