How to share the use of inheritance in PHP development process

Source: Internet
Author: User
Inherited
Typically, these classes are required to have the same variables and functions as other existing classes. In fact, it would be a good practice to define a generic class for all projects and enrich this class to fit each specific project. To make this easier, classes can be extended from other classes. The extended or derived class has all the variables and functions of its base class (this is known as "inheritance", but no one dies), and contains all the parts defined in the derived classes. The elements in a class cannot be reduced, that is, you cannot unregister any existing functions or variables. An extension class always relies on a single base class, that is, multiple inheritance is not supported. Use the keyword "extends" to extend a class.
Copy the 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 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 that owns all the variables and functions of the Cart class, plus the additional variable $owner and an additional function, Set_owner (). Now, the normal way to create a name shopping cart, and can set up and get the owner of the cart. and the normal shopping cart function can still be used in the name of the 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 owner of the cart
$ncart->add_item ("10", 1); (features inherited from the shopping cart class)
?>
This can also be called a "parent-child" relationship. Create a class, parent class, and use extends to create a new class based on the parent class: subclass. You can even use this new subclass to create another class based on this subclass.
Note:
Classes can only be used after they are defined! If you need class Named_cart to inherit the 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. Simply stated: The Order of class definitions is very important.
Copy the code code as follows:
Class person{
protected $name;//protected protected permissions, which can be accessed by subclasses, cannot be accessed externally
protected $age;
protected $sex;
function __construct ($name, $age, $sex) {
$this->name= $name; When you use this, even if name is not declared, it will again declare a
$this->age= $age;
$this->sex= $sex;
echo "###############";
}
Public function say () {
echo "My name: {$this->name}, my age {$this->age}:, my gender: {$this->sex}<br/>";
}
protected function Eat () {
echo "Wwwwwwwwwwwwwwwwwwwww<br>";
}
function Run () {
}
protected $name;//protected protected permissions, which can be accessed by subclasses, cannot be accessed externally
protected $age;
protected $sex;
}
Inherited
Class Student extends person{
var $school;
function __construct ($name, $age, $sex, $school) {
Parent::__construct ();//constructor method for calling parent class
$this->school= $school;
}
Overloading the Say () method to extend
protected function Say () {//Parent class Use public, subclass permissions cannot be lower than parent class, can drink the same permissions as parent class
Person::say ();//Call the Say () method of the parent class
Parent::say ();//Call the parent class say () method, which represents the parent class name and can be called when the parent class name changes.
echo "My School {$this->school}<br/>";//www.php.cn
}
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 and extensibility
*
* 3. Increase the reusability of the Code
*
* 4. Improved maintainability of the software
*
* 5. Inheritance is to "extend" the parent class with subclasses
*
* C + + is multi-inheritance, the same class can have more than one parent class
*
* PHP and Java belong to single inheritance, the same class can have only one parent class
*
* Regardless of multiple inheritance or single inheritance, you can have multiple subclasses
*
* 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
*
* I. Application of class inheritance
*
* 1. Declare a subclass and use the extends keyword to inherit (extend) a parent class
*
* 2. Subclasses can inherit all content from the parent class, including the member property method, the constructor method ..., which can be used in subclasses.
*
* Second, Access type control
*
* Although subclasses can inherit all content from a parent class, private, privately-owned members can only be used in this class and cannot be used in subclasses
*
* When encapsulating, you can make the inside of your class accessible, and the subclass can be used, but the outside of the class cannot be used, as long as the permission is set to protected
*
*
*
* Methods for overloading parent classes in subclasses
*
* 1. Subclasses can declare a method name that can be declared with the same parent class, that is, a subclass overrides a method with the same name as the parent class
*
* 2. Methods of subclasses extend the method of the parent class
*
* 3. Calling the overridden method in the parent class in the subclass
* Use parent class Name:: Method Name () Parent:: Method Name ()
*
* 4. Write the constructor method in the subclass, and if there is a constructor in the parent class, make sure to call the overridden constructor in the parent class
*
* Note: Overloaded methods in subclasses cannot be lower than access permissions in the parent class (subclasses can magnify permissions but not reduce permissions)
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.