Re: working with class inheritance
| From: | Jim Lucas | Date: | Tue, 20 Mar 2007 09:23:05 +0000 |
| Subject: | Re: working with class inheritance | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-250893@lists.php.net to get a copy of this message | ||
Jim Lucas wrote:
Jeff Taylor wrote:ok, responded too quickly. Roman has it right with the settype method This now works. <?php class myParent { private $type; public function __construct() { return $this; } public function myParent() { return $this->__construct(); } public function setType($value='') { $this->type = $value; } public function getType() { return $this->type; } } class myChild extends myParent { public function __construct() { parent::__construct(); $this->setType('Child'); return $this; } public function myChild() { return $this->__construct(); } } $Child = new myChild(); echo $Child->getType(); ?>Hey all, got a slight problem, where for some reasons my variables dont seem to be getting stored in the child class: e.g class ParentAt least in PHP 5.2.1 on windows xp (for testing only), the class name Parent is a reserved class name, you cannot define a class by that name.{ $private type; public function __construct() { } public function GetType() {public should not be prefixed with '$' you are missing your open '{' for this methodreturn $this->type;} } class Child implements Parent {$public function __construct()You need to call parent::__construct();also, you are calling a method, not accessing a property. it should be echo $Child->getType();$this->type= 'Child'; }} $Child= new Child(); echo $Child->getType;Can u see any reason why the type would return null? Thanks, Jeffwell, for starters, I think it is incorrect to have public and private prefixed with a '$'. But then you have it on some, but not all of them..... Besides that, does it give you an error? What does it return if it doesn't return 'Child'?? What version of PHP are you running on. there is a differnet way the constuct() method gets called. public and private are not available in versions older than php 5 in PHP version < 5 you have to have a method named the same as the class that you are creating. Try this: <?php class myParent {# private $type; #<--- With this, I cannot echo $typevar $type; #<--- With this, I CAN echo $typepublic function __construct() { }public function myParent() { $this->__construct(); } public function getType() { echo $this->type; }} class myChild extends myParent {public function __construct() { parent::__construct(); $this->type = 'Child'; } public function myChild() { $this->__construct(); }} $Child = new myChild(); echo $Child->getType(); ?>