I want to be able to set the value of a personal property in the parent manufacturer, and the value of a child's creator or method I want to call.
For example:
& lt ;? Php table class main class {Private $ prop_1; Private $ prop_2; Function __construct () {$ this-> Prop_2 = 'this' is prop_2 'property'; }} Class sub-class increases the main square {function __construct () {origin: __ creation (); $ This- & gt; Prop_1 = 'this' is propi 'property'; } Public Function GetBothProperties () {Return Array ($ this-> prop_1, $ this-> prop_2); }} $ Subclass = new sub-class (); Print_r ($ subclass-> GetBothProperties ()); ? & Gt; Output:
array ([0] => this is the "prop_1" property [1] = & gt;) However, if I change prop_2 to protected , the output will be: Array ([0 ] = & Gt; This is the "prop_1" property [1] => this is the "prop_2" property) I have basic the OO and PHP's Knowledge is there, but I can not understand that by calling (or shown) when calling it prop_2 it is private ; This can not be a private / public / protected problem because 'prop_1' is private and able to call and show ... right? Is this the issue of assigning values to the parent class vs parent class?
I help understand why.
Thank you. P>
The private property of the guardian class can not be used in the child class and vice versa.
You can do this
Abstract class main category {Private $ prop_1; Private $ prop_2; Function __construct () {$ this-> Prop_2 = 'this' is prop_2 'property'; } Secure Function Set ($ name, $ value) {$ this- & gt; $ Name = $ value; } Get a secure function ($ name) {return $ $ -> $ name; }} Class sub-class increases the main square {function __construct () {origin: __ creation (); $ This- & gt; Set ('prop_1', 'this' is prop_1 "property ');} public function GetBothProperties () {get return array ($->' prop_1 '), get $ this-> (' Prop_2 '));}}
Comments
Post a Comment