Does inheritance have the Private attributes and methods of the parent class?

Source: Internet
Author: User
I read the official PHP manual, which is also "concise "... the attribute definition is also var, which is also drunk, alas... here is a post attached with the test code: The bbs.phpchina.comthread-116668-1-1.html that ultimately inherit will not have the private attribute of the parent class... I read the official PHP manual, which is also "concise "... the attribute definition is also var, which is also drunk, alas...

Here is a post with the code for testing attached: http://bbs.phpchina.com/thread-116668-1-1.html

Will Inheritance have the private attributes and methods of the parent class? In the sense of inheritance, is it equivalent to a simple code copy of the parent class? Does it mean that sub-classes will also have the private of the parent class, but the private of the parent class can only be accessed by the parent class's own method, and the private of the sub-class can only be accessed by the sub-class's own method? In fact, are their private or their respective?

Reply content:

I read the official PHP manual, which is also "concise and concise"... the attribute definition is also var, which is also drunk, alas...

Here is a post with the code for testing attached: http://bbs.phpchina.com/thread-116668-1-1.html

Will Inheritance have the private attributes and methods of the parent class? In the sense of inheritance, is it equivalent to a simple code copy of the parent class? Does it mean that sub-classes will also have the private of the parent class, but the private of the parent class can only be accessed by the parent class's own method, and the private of the sub-class can only be accessed by the sub-class's own method? In fact, are their private or their respective?

It must be clear:private,publicAndprotectedThese keywords have nothing to do with inheriting a dime. People call it "Visibility )".The base class cannot control that all its members are inherited by the quilt class. However, it can restrict some inherited members and make the new Child class member invisible..

From the perspective of "ownership", subclass is the shell of the base class, of course, all of the base classes are "existent" in the subclass. It's just that we can't "see" it from the subclass's perspective.

For example (the Blue Arrow part), private member must exist in the sub-class bucket, that is, the quilt class must inherit. However, the method added by the subclass cannot be accessed.

In the architectural design sense, it is easy to clarify this issue. A subclass must abide by all the features of the base class and then add and expand it. If all the members of the base class are open to the subclass and can be viewed and changed at will, the original features of the base class can be removed at will. What should we do with inheritance?

In the actual project, the private member of the base class is either used internally by the base class, or like (the Red Arrow part) throughprotectedThe method is used as an accesser to ensure that both the access permission is indirectly open to the subclass and some necessary control logic can be executed during the subclass access.

Of course not.

I think the inheritance should be a process of searching up. When we need to access the attributes in a class, we will first check whether the current class exists. If not, we will query it in its parent class! When var_dump () is used to print a subclass, the private attribute of the parent class can be seen, but it cannot be accessed.

You can refer to this link for access control (visibility). Here, I paste some text to explain this problem. Key parts have been bold

Access control over properties or methods is implemented by adding the public, protected, or private keywords to the front.
It is defined as a public class member that can be accessed anywhere. A protected class member can be accessed by itself, its subclass, and its parent class. A private class member can only be accessed by the class it defines.

Example #1 attribute Declaration


  Public; echo $ this-> protected; echo $ this-> private; }}$ obj = new MyClass (); echo $ obj-> public; // This line can be normally executed echo $ obj-> protected; // this line will generate a fatal error echo $ obj-> private; // This line also produces a fatal error $ obj-> printHello (); // output Public, Protected, and Private/*** Define MyClass2 */class MyClass2 extends MyClass {// public and protected can be redefined, but private, but not protected $ protected = 'protected2 '; function printHello () {echo $ this -> Public; echo $ this-> protected; echo $ this-> private; }}$ obj2 = new MyClass2 (); echo $ obj2-> public; // This line can be normally executed echo $ obj2-> private; // undefined privateecho $ obj2-> protected; // this line will generate a fatal error $ obj2-> printHello (); // output Public, Protected2, and Undefined?>

Example #2 method declaration


  MyPublic (); $ this-> MyProtected (); $ this-> MyPrivate () ;}$ myclass = new MyClass; $ myclass-> MyPublic (); // This row can be normally executed $ myclass-> MyProtected (); // This row produces a fatal error $ myclass-> MyPrivate (); // This row generates a fatal error $ myclass-> Foo (); // public, protected, you can run/*** Define MyClass2 */class MyClass2 extends MyClass {// this method is a public function Foo2 () {$ this-> MyPublic (); $ this-> MyProtected (); $ this-> MyPrivate (); // this row produces a fatal error.} $ myclass2 = new M YClass2; $ myclass2-> MyPublic (); // This line can be normally executed $ myclass2-> Foo2 (); // both public and protected can be executed, but private class Bar {public function test () {$ this-> testPrivate (); $ this-> testPublic ();} public function testPublic () {echo "Bar:: testPublic \ n ";} private function testPrivate () {echo" Bar: testPrivate \ n ";} class Foo extends Bar {public function testPublic () {echo" Foo:: testPublic \ n ";} private function testPrivate () {Echo "Foo: testPrivate \ n" ;}}$ myFoo = new foo (); $ myFoo-> test (); // Bar: testPrivate // Foo :: testPublic?>

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.