PHP Learning----Object-oriented---inheritance

Source: Internet
Author: User
Tags modifiers

Yesterday we learned about object-oriented principles and fundamentals, and today we are going to learn one of the object-oriented features----inheritance.

I. Inheritance 1. Basic Concepts of inheritance

The attribute information in a Class A is passed to another class B, at which point, b inherits A,a derives B;

Basic syntax:

      

Several basic concepts about inheritance

Inheritance: A class obtains its attributes from another existing class, called inheritance.

Derivation: Produces a new class from an existing class;

Parent class/Subclass: An existing class is a parent class, and a new class is a subclass.

Single inheritance: A class can inherit its attribute information from only one ancestor class, and PHP and most object-oriented languages are single-inheritance patterns. C + + for multiple inheritance;

Extension: The information that defines some of the new features of the subclass. Without extension, inheritance has no meaning;

2. Access Control modifiers

The access control modifier is used to restrict the accessibility of the members it modifies;

Accessibility: It is the validity of the two grammatical forms that are used in the code;

Object, instance property or method

Class:: Static property or method

Form:

Class Name {

Access control modifier properties or method definitions;

}

There are 3 types of access control modifiers:

Public: Common, accessible in all locations;

Protected: Protected, accessible only within the class or in the subclass or parent class of the class;

Private: Accessible only within that class;

Summarize:

The members of the public modifier, where they can be accessed;

Within the class, you can access members of any level;

      

      

3.parent keywords

Parent means ' parents ', in object-oriented syntax, represents ' parent class ';

It is essentially the class that represents the parent class, not the object of the class;

It is used as: Parent:: Property or Method//here is usually a static property or static method, but sometimes it may be an instance property or an instance method;

      

      

4. Construction method and destructor method call higher class method problem

1. If a class has its own construction method, it will use its own constructor when new, and if not, the constructor of the parent class.

2. The destruction method is similar to the construction method, ibid.

3. If there is a constructor method and a destructor method in a class, you can call the constructor and destructor of the parent class manually: Using Parent:: .....

4. In the construction method of subclasses, it is often called to construct the parent class to simplify the initialization of the object.

      

 

5. Overwrite

Redefine the properties and methods that inherit a class from the parent class----at this point the subclass does not want to use the property or method of the parent class, but rather the new definition.

The implementation of the overlay requires:

For a parent class, perhaps the existing data of its properties, subclasses feel unsuitable, but need to have their own new description, perhaps its methods, subclasses feel inappropriate, need to redefine the method to do;

Overrides are called "overrides";

      

Requirements for rewriting:

The access control permission for a property or method overridden by a subclass cannot be lower than the access control permission for the overridden property or method of the parent class:

Parent class: Public sub-class: can only be public;

Parent class: Protected sub-class: protected or public;

When a subclass overrides a method of the same name in the parent class, the parameters are consistent with the parent class;

Attention:

A parent class private property cannot be overridden, but a subclass can define its own property with the same name as the parent class.

The private method of the parent class cannot be overridden, nor can a subclass define a method with the same name;

6. Final class

    The final class is actually a class of special requirements, requiring that the class is not allowed to inherit down;

Final Class class Name {

There is no difference between classes and other classes;

}

7. Final Approach

    The final method is a method that does not allow the subordinate class to overwrite!!

class {

Final function F_name () {...};

}

8. Design mode

    What do you mean by design mode?

In short, a design pattern is an empirical summary of the general code that solves a problem.

Factory mode:

The so-called factory pattern, is such a Class (factory Class): It can be "passed" to his class name, and to produce the corresponding class object;

      

Single-Case mode:

The simple interest pattern is the design of such a class, which can only create an object;

      

Two. Design a class, MySQL database operation class

Design goal:

1. If the class is instantiated, the MySQL database can be connected automatically;

2. The class can be set separately to use the connection code) (set names XXX)

3. This class can be used to set the database (use XXX) alone;

4. Can actively close the connection;

The above design directory, roughly equivalent to the following lines of code:

$link = mysql_connect ("", "", "");

mysql_query (' Set names XXX ');

mysql_query ("Use XXX");

Then, a variety of SQL statements can be executed later;

PHP Learning----Object-oriented---inheritance

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.