SystemOverload Posted September 24, 2011 Share Posted September 24, 2011 I'm doing some basic object coding to get my head around OOP PHP, but I've come across something I can't get to work. I've got two identical peices of code, MyClass is defined, MyClass2 is then defined as an extension of MyClass. MyClass has three properties Public, Private and Protected. In my first example I can access Public and Protected via a method in MyClass2, this class does NOT have a constructor. The second example has a constructor but I cannot access Public: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <body> <?php ECHO "MyClass2 does not have a constructor, Public & Protected both echo correctly from MyClass2->printHello!!!<BR>"; /** * Define MyClass */ class MyClass { public $public; protected $protected; private $private; protected $varXXX; function __construct() { $this->public = "Public"; $this->protected = "Protected"; $this->private = "Private"; //$this->printHello(); } function printHello() { echo $this->public . "!!!<BR>"; echo $this->protected . "!!!<BR>"; echo $this->private . "!!!<BR>"; } } /** * Define MyClass2 */ class MyClass2 extends MyClass { // We can redeclare the public and protected method, but not private protected $protected = 'Protected2'; /* function __construct() { echo $this->public . "#<BR>"; echo $this->protected . "#<BR><BR>"; //echo $this->private . "???<BR>"; } */ function printHello() { echo $this->public . "???<BR>"; echo $this->protected . "???<BR><BR>"; //echo $this->private . "???<BR>"; } } $obj = new MyClass(); //echo $obj->public . "<BR>"; // Works //echo $obj->protected; // Fatal Error //echo $obj->private; // Fatal Error //$obj->printHello(); // Shows Public, Protected and Private $obj2 = new MyClass2(); //echo $obj2->public; // Works //echo $obj2->private; // Undefined //echo $obj2->protected; // Fatal Error $obj2->printHello(); // Shows Public, Protected2, Undefined ?> </body> </html> RESULTS IN: Public??? Protected??? <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <body> <?php ECHO "MyClass2 DOES have a constructor, Public does not work from MyClass2->printHello!!!<BR>"; /** * Define MyClass */ class MyClass { public $public; protected $protected; private $private; protected $varXXX; function __construct() { $this->public = "Public"; $this->protected = "Protected"; $this->private = "Private"; //$this->printHello(); } function printHello() { echo $this->public . "!!!<BR>"; echo $this->protected . "!!!<BR>"; echo $this->private . "!!!<BR>"; } } /** * Define MyClass2 */ class MyClass2 extends MyClass { // We can redeclare the public and protected method, but not private protected $protected = 'Protected2'; function __construct() { echo $this->public . "#<BR>"; echo $this->protected . "#<BR><BR>"; //echo $this->private . "???<BR>"; } function printHello() { echo $this->public . "???<BR>"; echo $this->protected . "???<BR><BR>"; //echo $this->private . "???<BR>"; } } $obj = new MyClass(); //echo $obj->public . "<BR>"; // Works //echo $obj->protected; // Fatal Error //echo $obj->private; // Fatal Error //$obj->printHello(); // Shows Public, Protected and Private $obj2 = new MyClass2(); //echo $obj2->public; // Works //echo $obj2->private; // Undefined //echo $obj2->protected; // Fatal Error $obj2->printHello(); // Shows Public, Protected2, Undefined ?> </body> </html> RESULTS IN: # << Missing the property's contents Protected2# ??? << Missing the property's contents Protected2??? Any ideas why I cannot access the property's contents in the second example when the only difference is that it has a constructor? Quote Link to comment https://forums.phpfreaks.com/topic/247803-class-constructors-and-properties/ Share on other sites More sharing options...
jcbones Posted September 24, 2011 Share Posted September 24, 2011 You cannot access private, or protected directly: This tutorial is one of the best for PHP OOP: http://www.tuxradar.com/practicalphp/6/0/0 Quote Link to comment https://forums.phpfreaks.com/topic/247803-class-constructors-and-properties/#findComment-1272495 Share on other sites More sharing options...
SystemOverload Posted September 25, 2011 Author Share Posted September 25, 2011 JC, you can, because in both examples I am accessing properties from the parent object, just in the second example I try to access those from the child's constructor first, which seems to erase the contents in the child. ??? Quote Link to comment https://forums.phpfreaks.com/topic/247803-class-constructors-and-properties/#findComment-1272559 Share on other sites More sharing options...
trq Posted September 25, 2011 Share Posted September 25, 2011 You need to call the parent's construct in your child. eg; public function __construct() { parent::__construct(); // rest of your code } Quote Link to comment https://forums.phpfreaks.com/topic/247803-class-constructors-and-properties/#findComment-1272561 Share on other sites More sharing options...
SystemOverload Posted September 25, 2011 Author Share Posted September 25, 2011 Thorpe Thanks, but why were there parent's variables available in the normal methods, but not the constructor. Without the parent::__constructor() was 'echo $this->protected;' effectively redeclaring it (and consequentially setting it's value to nothing) in the child object? Quote Link to comment https://forums.phpfreaks.com/topic/247803-class-constructors-and-properties/#findComment-1272565 Share on other sites More sharing options...
trq Posted September 25, 2011 Share Posted September 25, 2011 Without calling the parent's constructor the properties are never assigned any values. Quote Link to comment https://forums.phpfreaks.com/topic/247803-class-constructors-and-properties/#findComment-1272566 Share on other sites More sharing options...
SystemOverload Posted September 25, 2011 Author Share Posted September 25, 2011 So why were the properties assigned values in the example where the child had no constructor??? Quote Link to comment https://forums.phpfreaks.com/topic/247803-class-constructors-and-properties/#findComment-1272621 Share on other sites More sharing options...
jcbones Posted September 25, 2011 Share Posted September 25, 2011 When you extend a class, if there is no constructor in the extended class, it will call the constructor of the parent. So the variables will be assigned. BUT, since you assign the variables in the constructor, the properties of MyClass2's protected variable will not be set, but rather will revert to "protected". You can use the functions of the parent, through the child, so there is no need to remake the same functions. Try this: <?php class MyClass { public $public; protected $protected; private $private; protected $varXXX; function __construct() { $this->public = "Public"; $this->protected = "Protected"; $this->private = "Private"; //$this->printHello(); } function printHello() { echo $this->public . "!!!<BR>"; echo $this->protected . "!!!<BR>"; echo $this->private . "!!!<BR>"; } } class MyClass2 extends MyClass { // We can redeclare the public and protected method, but not private protected $protected = 'Protected2'; function changeProtected() { $this->protected = 'Protected2'; } } $class = new MyClass(); echo $class->public; //accessable directly. //echo $class->protected; //not accessable directly, uncomment to see it throw a fatal error. //echo $class->private; //not accessable directly, uncomment to see it throw a fatal error. echo '<br />[Class1]<br />PrintHello function: '; $class->printHello(); $class2 = new MyClass2(); echo $class2->public; //echo $class2->protected; //not accessable directly, uncomment to see it throw a fatal error. //echo $class2->private; //not accessable directly, uncomment to see it throw a fatal error. echo '<br />[Class2 Before Change]<br />PrintHello function: '; $class2->printHello(); //Now let us change the protected variable. $class2->changeProtected(); echo '<br />[Class2 After change]<br />'; $class2->printHello(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/247803-class-constructors-and-properties/#findComment-1272651 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.