PHP class inheritance [public protected private] parent constructor method destructor override final class and method design pattern

Source: Internet
Author: User
Tags modifiers php class


Inheritance of Classes

Simple to understand:

a class A has some characteristics, another class B, also has all the characteristics of Class A, and may also have its own more characteristics, at this time, we can implement: Class B uses a Feature information and continue to add some of your own unique feature information.


650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/7E/71/wKiom1b_r0ewCfJPAACjf5klJ54790.png "title=" Untitled. png "alt=" Wkiom1b_r0ewcfjpaacjf5klj54790.png "/>


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

    • Derivation: Produces a new class from an existing class, called a derivation.

Inheritance and derivation, in fact, are only expressed in different directions (angles), which is essentially a matter.

    • Parent class/Subclass: An existing class is a parent class, and a new class is a subclass. The parent class is also called the base class, and the subclass is also called a derived class.

    • Single inheritance: A class can inherit its attribute information only from a parent class. PHP and most object-oriented languages are single-inheritance modes. C + + is multiple inheritance.

    • Extensions: Define some of your own new and unique attribute information (properties, methods, and constants) in the subclass. Without extension, inheritance is meaningless.

access (permission) modifier

members in a class can usually be preceded by the following 3 modifiers:

public: Communal, shared, public

Protected: Protected

Private: Privately-owned,

PublicPublic-

Members decorated with this modifier can be used (accessed) at any location.

Access (using) is such a syntax pattern:

objects , and members;

class name :: member;

The access location is divided into 3 A:

1: Inside a class: Naturally, in a method of that class

2: The Inner of a child (parent) class that has an inheritance relationship to a class: Refers to a method in another class.

3, outside of a class: it is generally a separate code area (not in a class), similar to our previous code.

The code demonstrates the following:

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/7E/6E/wKioL1b_sBCjzfgyAACNal-w-A8524.png "title=" Untitled. png "alt=" Wkiol1b_sbcjzfgyaacnal-w-a8524.png "/>



class allows only the definition of properties and methods, and does not allow the use of ECHO.

subclasses can access the public property of the parent class directly in the method

The parent class can also access the properties of the child class

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/7E/6E/wKioL1b_sD3jz-z5AAPdNKIaD9A660.png "title=" Untitled. png "alt=" Wkiol1b_sd3jz-z5aapdnkiad9a660.png "/>


Protectedthe protected

A protected decorated member that can be accessed in a class with an inheritance relationship in the current class or in the hierarchy of the current class.

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/7E/71/wKiom1b_r7vALYFnAAHyJn6fsek201.png "title=" Untitled. png "alt=" Wkiom1b_r7valyfnaahyjn6fsek201.png "/>

Privateof Private

A member of the private adornment, accessible only in the class in which it resides.

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/7E/71/wKiom1b_r-KBSqdbAADCNLf0uaI556.png "title=" Untitled. png "alt=" Wkiom1b_r-kbsqdbaadcnlf0uai556.png "/>


650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/7E/6E/wKioL1b_sUKydFu8AAARfqtIRIA889.png "title=" Untitled. png "alt=" Wkiol1b_sukydfu8aaarfqtiria889.png "/>


Parentrepresents the parent class

Contrast: Self stands for "This class" (Own Current Class)

The parent is typically used when calling members of the parents in a subclass, most often using a static class member of the parent class.

--because the parent represents the class, not the object.

The following are 2 common uses of the parent keyword:

the current presentation, with self, $this the same effect

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/7E/72/wKiom1b_sMXjceysAADloLx2owE413.png "title=" Untitled. png "alt=" Wkiom1b_smxjceysaadlolx2owe413.png "/>


The representation of construction method in inheritance
    • When a constructor method is not defined in a subclass, the constructor method of the parent class is automatically called. Therefore, when you instantiate a subclass, you do so in the form of the parent class's construction method.

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/7E/6E/wKioL1b_scrTfRiVAADrREz4NNw240.png "title=" Untitled. png "alt=" Wkiol1b_scrtfrivaadrrez4nnw240.png "/>


Modified to:


650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/7E/72/wKiom1b_sTrzhr1dAACHl7MbnTs041.png "title=" Untitled. png "alt=" Wkiom1b_strzhr1daachl7mbnts041.png "/>






    • When a subclass defines its own constructor method, the constructor of the parent class is not automatically called, but it can be called manually: Parent::__construct ();


650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/7E/72/wKiom1b_sWrCPzQyAAQlDiqWxgA964.png "title=" Untitled. png "alt=" Wkiom1b_swrcpzqyaaqldiqwxga964.png "/>

But usually, in subclasses, many times, in a constructor method, you should (need) call the constructor of the parent class to save code and increase readability:

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/7E/6E/wKioL1b_si6xHKuEAADebd7qt68036.png "title=" Untitled. png "alt=" Wkiol1b_si6xhkueaadebd7qt68036.png "/>


    • The destructor method of the parent class is automatically called when no destructor is defined in the subclass.

    • When a subclass defines its own destructor, the destructor of the parent class is not automatically called, but it can be called manually: Parent::__destruct ()

      650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/7E/6E/wKioL1b_smPjjpZ7AAQlDiqWxgA203.png "title=" Untitled. png "alt=" Wkiol1b_smpjjpz7aaqldiqwxga203.png "/>


rewriteOverride

Rewriting, or overwrite, is to redefine the property or method inherited from the parent class--that is, from the new write.


650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/7E/6E/wKioL1b_so-gE9zrAADIggHOdiE899.png "title=" Untitled. png "alt=" Wkiol1b_so-ge9zraadigghodie899.png "/>

Note: The subclass overrides the parent class method, although it is possible to call the parent class with the same name method to do some work, but it is not necessary. It is also possible that the result of the method performed by the parent class is not suitable for subclasses, at which time the subclass is completely self-written.

Basic requirements for rewriting:

Access control permissions:

Subordinate access control permissions should not be lower than the superior access control permissions:

Superior:public subordinate: only public

Superior:protected Subordinate: protected, public

Superior:private subordinate:private protected public--this situation is meaningless.

Private can not be covered, but completely as a brand-new.

The Parameter form of the method:

Should be consistent with the parent class.


650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/7E/72/wKiom1b_sg-Q8IN0AACLHhP3mCw307.png "title=" Untitled. png "alt=" Wkiom1b_sg-q8in0aaclhhp3mcw307.png "/>

The right approach:


650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/7E/6E/wKioL1b_ssqhxp8CAAA6gP6GNZI862.png "title=" Untitled. png "alt=" Wkiol1b_ssqhxp8caaa6gp6gnzi862.png "/>

More parameters are also incorrect:

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/7E/6E/wKioL1b_suCQ0YJIAAB6qsg70jw707.png "title=" Untitled. png "alt=" Wkiol1b_sucq0yjiaab6qsg70jw707.png "/>


    • Overriding problem with private properties and private methods: private properties and methods cannot be overridden, but subclasses can define properties or methods that are private to the parent class with the same name. Just as a new property or method of its own. However, the parameters of the method must be identical.

    • The overriding problem with the constructor method: The constructor method can be overridden not just like other common methods, but more lenient than the normal method: the arguments can be inconsistent when overridden.


650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/7E/6E/wKioL1b_swvSE6bXAACiJtvLy3o495.png "title=" Untitled. png "alt=" Wkiol1b_swvse6bxaacijtvly3o495.png "/>

Final ClassFinal class:

Usually, a class, without special statements, "others" can be arbitrarily used and "extended"-inheritance.

But:

If a class does not want to extend it, it can be declared as a "final class."

Form:

Final class class name { .... Class definition .... }

Final ApproachFinal method

Typically, a method, if not specifically declared, can be overridden by a subordinate class.

But:

If a method does not want to be overridden by a subordinate class, it can be a "final method" of its life.

Form:

Final function method name () {.... Method definition .... }



Design PatternsWhat do you mean design mode

The so-called design pattern, is a number of problems to solve the "conventional approach" is a good experience to conclude. Faced with different problems, there may be different solutions, at this point can be called different design patterns.

Factory mode

In practice, we always need to instantiate a lot of classes--to get objects.

The

We can design a "factory" (in fact, a class), the role of the plant (the task) is to "produce" a variety of objects for people. This kind of factory usually can get an object of this class as long as the class name is specified.


650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/7E/6E/wKioL1b_szGx9d2rAACVbGHr57o547.png "title=" Untitled. png "alt=" Wkiol1b_szgx9d2raacvbghr57o547.png "/>

Review points:

Inheritance of Classes

Basic Concepts :

Inheritance: The process of obtaining its attribute information (properties and methods) from a parent class. b<<==a,B inherits from A

Derivation: A==>>b, a new class B is derived from a, b has "almost all characteristics" of a and can have its own characteristics.

Parent class / subclass, base class / derived class, ancestor class, subordinate class.

single inheritance: in PHP, the inheritance pattern of a class can only be inherited from a parent class. Java,C # is also true.

Extensions: B inherits some features from a and adds some of its own features, which can be called extensions.

Access (permission) modifier

in front of a member (attribute / method / constant), you can add an access modifier:public, protected, private

The so-called access is a grammatical form like this:

Object , properties / methods;

class :: Properties / methods;

Location (range) Difference of access:

the interior of a class (certainly within a method)

B the interior of a class's inheritance relationship class (also within a method)

the outside of a class C.

Public Public- :

protected the protected

Private of Private


650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/7E/72/wKiom1b_sqKi5vtgAAATGPXwalY014.png "title=" Untitled. png "alt=" Wkiom1b_sqki5vtgaaatgpxwaly014.png "/>



Attention:

1, for methods, if the access modifier is omitted, by default

2, for normal attributes, cannot be omitted, but var stands for public

3, for static properties, can be omitted, omitted by the public count

4, for constants, the default is public

Parent represents the parent class :

You can use it in a subclass to represent the parent class to access members in the parent class.

Parent:: property that represents the property that gets the parent class.

Parent:: method (): Represents the method that invokes the parent class. At this point, a situation may occur:

the parent class method may use the $this keyword, which represents "current object", but actually represents the current object, not the object of the parent class, but the object that invokes the subclass of the method.

This usage is typically used when a subclass overrides a method of the parent class but needs to use the same name method of the parent class.

The representation of construction method in inheritance :

If they are not defined, they will automatically call the parent class with the same name method.

If defined, it is not automatically called, but can be called manually in it, similar to this pattern:

Parent::__constuct (...)

Parent::__destruct ()

rewrite Override

A subclass defines a property or method with the same name as the parent class, which is an override.

Basic requirements for rewriting:

1, access modifiers cannot be lower than the parent class (must be the same level or more open)

2, for a method, its formal parameter must be the same as the parent class.

Final Class Final Class : classes that cannot be inherited (not allowed)

Final class a{....}

Final Approach Final Method : Methods that cannot be overridden (not allowed)

Finale function F1 () {...}

Design Patterns

What do you mean design mode

Design pattern is the application of a particular situation in the face of a problem and designed to some of the common effective solution, is the experience of the summary.

Factory mode :

is a structural pattern of objects designed to specialize in "production" classes.

Design Patterns

Factory mode:


















This article is from the "Soul Bucket" blog, please be sure to keep this source http://990487026.blog.51cto.com/10133282/1759611

PHP class inheritance [public protected private] parent constructor method destructor override final class and method design pattern

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.