[PHP] extended class question
How come the output to this script is World Trade Center instead of Pizza Delivery? Thanks, Ed ?php class test { private $var1; function __construct() { $this-var1 = World Trade Center; } function get_var1() { return $this-var1; } function set_var1($data) { $this-var1 = $data; } } class testing extends test { function __construct() { parent::__construct(); $this-var1 = Pizza Delivery; } } $test2 = new testing(); print var1 = . $test2-get_var1() . br; ? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] extended class question
* Thus wrote Ed Lazor: How come the output to this script is World Trade Center instead of Pizza Delivery? Thanks, Ed ?php class test { private $var1; defines 'test' class to only have access. function __construct() { $this-var1 = World Trade Center; } function get_var1() { return $this-var1; accesses the private $var1 } function set_var1($data) { $this-var1 = $data; } } class testing extends test { function __construct() { parent::__construct(); $this-var1 = Pizza Delivery; This creates a publicly accessable member for the *testing* class. } } $test2 = new testing(); print var1 = . $test2-get_var1() . br; If you wish for it to change within testing class, declare the member as a protected var in the test class. class test { protected $var1; /* allows access to extending classes */ } See also: http://php.net/oop5.visibility Curt -- The above comments may offend you. flame at will. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] extended class question
Ed Lazor wrote: How come the output to this script is World Trade Center instead of Pizza Delivery? Thanks, Ed ?php class test { private $var1; function __construct() { $this-var1 = World Trade Center; } function get_var1() { return $this-var1; } function set_var1($data) { $this-var1 = $data; } } class testing extends test { function __construct() { parent::__construct(); $this-var1 = Pizza Delivery; } } $test2 = new testing(); print var1 = . $test2-get_var1() . br; ? Because test's var1 is private. test-var1 isn't accessible by class testing, so the assignment you're doing in testing's constructor is assigning Pizza Delivery to testing-var1 instead. When you call $test2-get_var1() you're calling the parent's get_var1() method, which prints out the parent's var1 property. (which hasn't been touched.) Change test's var1 to protected and it'll work. --rick -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] extended class question
Because test's var1 is private. test-var1 isn't accessible by class testing, so the assignment you're doing in testing's constructor is assigning Pizza Delivery to testing-var1 instead. When you call $test2-get_var1() you're calling the parent's get_var1() method, which prints out the parent's var1 property. (which hasn't been touched.) Change test's var1 to protected and it'll work. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php