Read the official PHP manual, but also "concise" ... Attribute definition is also directly Var, this fools is also drunk, alas ...
Here is a post with the code for the test: http://bbs.phpchina.com/thread-116668-1-1.html
Does the inheritance have the private properties and methods of the parent class? Does inheritance mean a simple code copy of the parent class? Is it said that the subclass will also have the private of the parent class, only the parent class private can only be accessed by the parent class's own method, the subclass of private only the subclass of its own method access? In fact, their own private or individual?
Reply content:
Read the official PHP manual, but also "concise" ... Attribute definition is also directly Var, this fools is also drunk, alas ...
Here is a post with the code for the test: http://bbs.phpchina.com/thread-116668-1-1.html
Does the inheritance have the private properties and methods of the parent class? Does inheritance mean a simple code copy of the parent class? Is it said that the subclass will also have the private of the parent class, only the parent class private can only be accessed by the parent class's own method, the subclass of private only the subclass of its own method access? In fact, their own private or individual?
Must be clear: private
, public
and protected
these few keywords, and inherit a dime of the relationship are not. It is called "visibility (Visibility)". the base class cannot control that all its members inherit from the quilt class, but can constrain some inherited members, and the newly-added members of the subclass are not visible.
From the perspective of "owning", the subclass is the shell of the base class, and all of the base classes "exist" in the subclass-just from the angle of the subclass can "see" only.
such as (blue Arrow part), private member must exist in the subclass of the storage space, that is to say that the quilt class inherited to. However, the new method for subclasses is not accessible.
It is very easy to be clear about this in terms of architectural design. A subclass must adhere to all the characteristics of the base class and then make additions and expansions on it. And if all the members of the base class are open to the subclass casually to look at, casually change, the original characteristics of the base class can be arbitrarily torn down, then also inherit what to do?
In the actual project, the private members of the base class are either used internally by the base class or, as in the case of the Red Arrow part, protected
make an accessor by means of which the access rights are indirectly opened to the subclass and the necessary control logic can be executed when the subclass accesses it.
Of course not, behind the relay.
I feel that inheritance should be to look up a process, when we need to access the properties of a class, first of all see the current class has no, if not to its parent class query! Var_dump () When you print a subclass, you can see the private properties of the parent class, but you cannot access
You can refer to this link access control (visibility), which is part of the text that explains the problem. The key part is bold
The access control of a property or method is achieved by adding the keyword public (publicly), protected (protected), or private (privately) in front.
Class members that are defined as public can be accessed from anywhere. A class member that is defined as protected can be accessed by itself and its subclasses and parent classes. A class member that is defined as private can only be accessed by the class in which it is defined.
Example #1 Property Declaration
"!--? php/** * Define MyClass */class myclass{public $public = ' public '; Protected $protected = ' protected '; Private $private = ' private '; function Printhello () {echo $this--->public; Echo $this->protected; Echo $this->private; }} $obj = new MyClass (); Echo $obj->public; This line can be performed normally echo $obj->protected; This guild produces a fatal error. Echo $obj->private; This line will also produce a fatal error $obj->printhello (); Output public, Protected, and private/** * Define MyClass2 */class MyClass2 extends myclass{//can be redefined for public and Protected , but private but not protected $protected = ' Protected2 '; function Printhello () {echo $this->public; Echo $this->protected; Echo $this->private; }} $obj 2 = new MyClass2 (); Echo $obj 2->public; This line can be performed normally echo $obj 2->private; Privateecho $obj 2->protected; Not defined This guild produces 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 line can be executed normally $myclass->myprotected (); This guild produces a fatal error $myclass->myprivate (); This guild produces a fatal error $myclass->foo (); Public, protected, private can execute/** * Define MyClass2 */class MyClass2 extends myclass{//This method is public function Foo2 () {$this ->mypublic (); $this->myprotected (); $this->myprivate (); This guild produces a fatal error}} $myclass 2 = new MyClass2; $myclass 2->mypublic (); This line can be executed normally $myclass2->foo2 (); Both public and protected can be executed, but not 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?>