Home  >  Article  >  Backend Development  >  Sharing on the use of inheritance during PHP development_PHP tutorial

Sharing on the use of inheritance during PHP development_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:28:10881browse

Inheritance
usually requires classes that have the same variables and functions as other existing classes. In fact, it would be a good exercise to define a common class that is used in all projects, and to continuously enrich this class to adapt to each specific project. To make this easier, classes can extend from other classes. An extended or derived class owns all the variables and functions of its base class (this is called "inheritance", except no one dies), and includes all the parts defined in the derived class. The elements in the class cannot be reduced, that is, any existing functions or variables cannot be unregistered. An extension class always depends on a single base class, that is, multiple inheritance is not supported. Use the keyword "extends" to extend a class.

Copy code The code is as follows:

class test {
public function __construct() {
}
public function name() {
$this->xname('John');
}
private function showName($name) {
echo 'my name in test is '.$name;
}
}
class extendTest extends test {
public function __construct() {
parent::__construct();
}
private function showName($name) {
echo 'my name in extendTest is '.$name;
}
}
$test = new extendTest();
$test-> ;name();
?>

The above example defines a class named Named_Cart, which has all the variables and functions of the Cart class, plus the additional variable $owner and a Additional function set_owner(). Now, a named shopping cart is created in the normal way, and the owner of the shopping cart can be set and retrieved. The normal shopping cart class functions can still be used in the named shopping cart class:
$ncart = new Named_Cart; // Create a new named shopping cart
$ncart ->set_owner("kris"); // Name the shopping cart
print $ncart->owner; // Output the name of the shopping cart owner
$ncart->add_item("10" , 1); // (Function inherited from the shopping cart class)
?>
This can also be called the "father-child" relationship. Create a class, the parent class, and use extends to create a new class based on the parent class: the child class. You can even use this new subclass to create another class based on this subclass.
Note:
A class can only be used after it is defined! If you need class Named_Cart to inherit 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. To put it simply: the order of class definitions is very important.
Copy code The code is as follows:

class Person{
protected $name;//protected permissions, in Subclasses can access, but not externally
protected $age;
protected $sex;
function __construct($name,$age,$sex){
$this->name=$name ;//When using this, even if the name is not declared, it will be declared again
$this->age=$age;
$this->sex=$sex;
echo "# #############";
}
public function say(){
echo "My name: {$this->name}, my age {$this->age}:, my gender: {$this->sex}
";
}
protected function eat(){
echo "wwwwwwwwwwwwwwwwwwwww< br>";
}
function run(){
}
protected $name;//protected permissions can be accessed in subclasses but not externally
protected $age;
protected $sex;
}
//Inherited
class Student extends Person{
var $school;
function __construct($name,$age,$sex,$school) {
parent::__construct();//Call the constructor method of the parent class
$this->school=$school;
}
//Overload the say() method to expand
protected function say(){//The parent class uses public, the permissions of the subclass cannot be lower than the parent class, and can have the same permissions as the parent class
//Person::say();//Call the parent class The say() method
parent::say();//Call the parent class say() method, parent represents the parent class name, and can also be called when the parent class name changes.
echo "My school{$this->school}
";//www.3ppt.com
}
function study(){
echo "{ $this->name} is studying
";
}
}
$s=new Student("zhangsan",23,"male");
$s ->say();
$s->study();

* 1. One of the three major characteristics of object-oriented
*
* 2. Openness and extensibility
*
* 3. Increase code reusability
*
* 4. Improves the maintainability of software
*
* 5. Inheritance is to use subclasses to "extend" parent classes
*
* C++ belongs to multiple inheritance, and the same class can There are multiple parent classes
*
* PHP and JAVA belong to single inheritance. The same class can only have one parent class
*
* Whether it is multiple inheritance or single inheritance, it can have multiple children. Class
*
* As long as you have members that can be shared when designing two classes, use the shared content as a base class alone
*
* 1. Application of class inheritance
*
* 1. Declare a subclass and use the extends keyword to inherit (extend) a parent class
*
* 2. The subclass can inherit all content from the parent class, including Member attribute methods, construction methods... can be used in subclasses
*
* 2. Access type control
*
* Although subclasses can inherit from parent classes All content, but private private members, can only be used in this class, and cannot be used in subclasses.
*
* When encapsulating, you can not only make the inside of your own class accessible, but also make it available to subclasses. , but it cannot be used outside the class, as long as the permission is set to protected
*
*
*
* 3. Overloading the method of the parent class in the subclass
*
* 1 .The subclass can declare the same method name as the parent class, that is, the subclass overrides the method with the same name of the parent class
*
* 2. The method of the subclass extends the method of the parent class
*
* 3. Call the overridden method in the parent class in the child class
* Use parent class name::method name() parent::method name()
*
* 4. In the child Write a constructor in the class. If there is a constructor in the parent class, be sure to call the overridden constructor in the parent class
*
* Note: The overloaded methods in the subclass cannot be lower than those in the parent class. Medium access permissions (subclasses can enlarge permissions, but cannot reduce permissions)

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/323672.htmlTechArticleInheritance usually requires classes that have the same variables and functions as other existing classes. In fact, define a common class for all projects, and continuously enrich this class to...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn