Inherited
It is often necessary to have classes that have the same variables and functions as other existing classes. In fact, it is a good practice to define a generic class for all projects and to enrich the class to fit each specific project. To make this easier, classes can be extended from other classes. The extended or derived class owns all the variables and functions of its base class (this is called "inheritance", but nobody dies), and contains the parts defined in all derived classes. The elements in a class cannot be reduced, that is, you cannot unregister any existing functions or variables. An extended class always relies on a single base class, which means that multiple inheritance is not supported. Use the keyword "extends" to extend a class.
Copy Code code as follows:
<?php
Class Test {
Public Function __construct () {
}
Public Function name () {
$this->xname (' John ');
}
Private Function ShowName ($name) {
Echo ' My name in test '. $name;
}
}
Class Extendtest extends Test {
Public Function __construct () {
Parent::__construct ();
}
Private Function ShowName ($name) {
Echo ' My name in Extendtest '. $name;
}
}
$test = new Extendtest ();
$test->name ();
?>
The above example defines a class named Named_cart that has all the variables and functions of the Cart class, plus an additional variable $owner and an additional function Set_owner (). Now, the normal way to create a shopping cart with a name, and you can set up and get the owner of the cart. The function of a normal shopping cart class can still be used in a named Shopping cart class:
<?php
$ncart = new Named_cart; Create a new shopping cart with a name
$ncart->set_owner ("Kris"); Give the shopping cart a name
Print $ncart->owner; Output the name of the cart owner
$ncart->add_item ("10", 1); (features inherited from the shopping cart class)
?>
This can also be called the "parent-child" relationship. Create a class, a parent class, and use extends to create a new class based on the parent class: subclasses. You can even use this new subclass to create another class based on this subclass.
Note:
Class can only be used if it is defined! If you need class Named_cart inheritance class cart, you must first define the cart class. If you need to create another Yellow_named_cart class based on the Named_cart class, you must first define the Named_cart class. It is simple to say: the Order of class definition is very important.
Copy Code code as follows:
Class person{
Protected $name//protected protected permissions that are accessible in subclasses and cannot be accessed externally
protected $age;
protected $sex;
function __construct ($name, $age, $sex) {
$this->name= $name;//When this is used, even if name is not declared, it is declared again
$this->age= $age;
$this->sex= $sex;
echo "###############";
}
Public function say () {
echo "My name: {$this->name}, my age {$this->age}:, my sex: {$this->sex}<br/>";
}
protected function Eat () {
echo "Wwwwwwwwwwwwwwwwwwwww<br>";
}
function Run () {
}
Protected $name//protected protected permissions that are accessible in subclasses and cannot be accessed externally
protected $age;
protected $sex;
}
Inherited
Class Student extends person{
var $school;
function __construct ($name, $age, $sex, $school) {
Parent::__construct ()//Calling the parent class's construction method
$this->school= $school;
}
Overloading the Say () method to extend
protected function Say () {//Parent class uses public, the permissions of subclasses cannot be lower than the parent class, and can drink the same permissions as the parent class
Person::say ()//Calling the parent class's say () method
Parent::say ()//invokes the parent class say () method, which is represented by the parent class name and can be invoked when the parent class name changes.
echo "My School {$this->school}<br/>";//www.3ppt.com
}
Function study () {
echo "{$this->name} in learning <br/>";
}
}
$s =new Student ("Zhangsan", 23, "male");
$s->say ();
$s->study ();
* 1. One of the three main features of object-oriented
*
* 2. Openness, extensibility
*
* 3. Increase the reusability of the Code
*
* 4. Improved software maintainability
*
* 5. Inheritance is using subclasses to "extend" the parent class
*
* C + + belongs to multiple inheritance, the same class can have multiple parent classes
*
* PHP and Java belong to single inheritance, the same class can have only one parent class
*
* Whether multiple inheritance or single inheritance, you can have more than one child class
*
* As long as you are designing two classes, there are members that can be shared, and the content that can be shared is used separately as a base class
*
* First, the application of class inheritance
*
* 1. Declare a subclass, use the extends keyword to inherit (extend) a parent class
*
* 2. Subclasses can inherit all content from the parent class, including member property methods, construction methods ..., which are available in subclasses.
*
* Two, Access type control
*
* Although subclasses can inherit all content from the parent class, privately owned private members can only be used in this class and cannot be used in subclasses
*
* Encapsulation, you can make the internal access to the class, but also allow subclasses to use, but the outside of the class can not use, as long as the permissions are set to protected
*
*
*
* Third, the method of overloading the parent class in subclasses
*
* 1. Subclasses can declare a method name that can declare the same as the parent class, that is, a subclass overrides a method with the same name as the parent class
*
* 2. The method of subclasses extends to the parent class method
*
* 3. Calling the overridden method in the parent class in a subclass
* Use parent class Name:: Method Name () Parent:: Method Name ()
*
* 4. Write the construction method in the subclass, if there is a constructor in the parent class, be sure to call the overridden construction method in the parent class
*
* Note: Overloaded methods in subclasses cannot be less than access rights in the parent class (subclasses can enlarge permissions, but not narrow permissions)