How to Use inheritance during php development

Source: Internet
Author: User
Tags php source code

Introduction: This is a detailed page about the inherited usage of PHP during development. It introduces PHP, related knowledge, skills, experience, and some PHP source code.

Class = 'pingjiaf' frameborder = '0' src = 'HTTP: // biancheng.dnbc?info/pingjia.php? Id = 338756 'rolling = 'no'>Inheritance

These classes are usually required. These classes have the same variables and functions as other existing classes. In fact, defining a general class for all projects, and constantly enriching this class to adapt to each specific project will be

Wrong exercise. 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 called "inheritance", but no one dies), and

Contains the parts defined in all derived classes. The elements in the class cannot be reduced. That is to say, you cannot cancel any existing functions or variables. An extended class always depends on a separate base class, that is, it does not support multiple
Inheritance. Use the keyword "extends" to extend a class.

class test {

public function _ construct () {
}

Public Function Name () {
$ this-> xname ('john');
}

private function showname ($ name) {
echo 'My name in test is '. $ name;
}< BR >}

class extendtest extends test {

public function _ construct () {
parent ::__ construct ();
}

private function showname ($ name) {
echo 'My name in extendtest is '. $ name;
}< BR >}

$ test = new extendtest ();
$ test-> name ();
?>

The above example defines the class named named_cart, which has all the variables and functions of the cart class, plus the additional variable $ owner and an additional function
Set_owner (). Now, a shopping cart with a name is created in a normal way, and the shopping cart owner can be set and obtained. Normal shopping cart functions can still be purchased with names.
Use in vehicle categories:

<? PHP
$ Ncart = new named_cart; // create a shopping cart with a name.
$ Ncart-> set_owner ("Kris"); // name the cart.
Print $ ncart-> owner; // output the name of the shopping cart owner.
$ Ncart-> add_item ("10", 1); // (functions inherited from the shopping cart class)
?>
This can also be called the "Parent-Child" relationship. Create a class, parent class, and use extends to create a new class: subclass Based on the parent class. You can even use this new subclass to create another class based on this subclass.

Note:

Class can only be used after definition! If the class named_cart is required to inherit the class cart, the cart class must be defined first. If you want to create another
The yellow_named_cart class of the named_cart class. The named_cart class must be defined first. Simply put, the sequence of class definitions is very important.

Class person {
Protected $ name; // The permission protected by protected. It can be accessed by sub-classes, but cannot be accessed externally.
Protected $ age;
Protected $ sex;

Function _ construct ($ name, $ age, $ sex ){
$ This-> name = $ name; // when this is used,
$ 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 "wwwwwwwwwwwwwwwwwww <br> ";

}

Function run (){

}
Protected $ name; // The permission protected by protected. It can be accessed by sub-classes, but cannot be accessed externally.
Protected $ age;
Protected $ sex;
}

// Inherit
Class student extends person {
VaR $ School;

Function _ construct ($ name, $ age, $ sex, $ School ){
Parent: :__ construct (); // call the constructor of the parent class.
$ This-> school = $ School;
}

// Reload the say () method for extension
Protected function say () {// The parent class uses public. the permission of the subclass cannot be lower than that of the parent class, and the permission of the parent class can be the same.
// Person: Say (); // call the say () method of the parent class
Parent: Say (); // call the statement () method of the parent class. Parent indicates the parent class name. It can also be called when the parent class name changes.

Echo "my school {$ this-> school} <br/>"; // www.3ppt.com
}

Function Study (){
Echo "{$ this-> name} learning <br/> ";
}

}

$ S = new student ("zhangsan", 23, "male ");
$ S-> say ();
$ S-> Study ();

* 1. One of the three features of object-oriented
*
* 2. Openness and scalability
*
* 3. AddCode Reusability
*
* 4. Improved Software maintainability
*
* 5. inheritance is to "extend" the parent class with sub-classes.
*
* C ++ is multi-inheritance. The same class can have multiple parent classes.
*
* PHP and Java belong to single inheritance. The same class can only have one parent class.
*
* No matter whether it is multi-inheritance or single-inheritance, there can be multiple sub-classes
*
* If you have members that can be shared when designing two classes, you can use the shared content separately as a base class.
*
* 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 the member attribute method and constructor method..., which can be used in the subclass.
*
* 2. Access Type Control
*
* Although the subclass can inherit all content from the parent class, Private Members can only be used in this class, and not in the subclass.
*
* During encapsulation, the class can be accessed internally or used by subclass, but the class cannot be used externally. You only need to set the permission to protected.
*
*
*
* 3. Method for overloading the parent class in the subclass
*
* 1. The subclass can declare the same method name as the parent class, that is, the subclass overwrites the method with the same name as the parent class.
*
* 2. Extension of the subclass Method to the parent class Method
*
* 3. Call the override method in the parent class in the subclass.
* Use the parent class name: Method Name () parent: Method Name ()
*
* 4. Compile the constructor In the subclass. If there is a constructor in the parent class, you must call the constructor that is overwritten in the parent class.
*
* Note: the method that is overloaded in the subclass cannot be lower than the access permission in the parent class (the subclass can enlarge the permission, but cannot narrow down the permission)

?

Love J2EE follow Java Michael Jackson video station JSON online tools

Http://biancheng.dnbcw.info/php/338756.html pageno: 8.

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.