Re: working with class inheritance
| From: | Jim Lucas | Date: | Tue, 20 Mar 2007 09:18:47 +0000 |
| Subject: | Re: working with class inheritance | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-250892@lists.php.net to get a copy of this message | ||
Jeff Taylor wrote:
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 Parent At 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() {you are missing your open '{' for this methodreturn $this->type;} } class Child implements Parent {$public function __construct()public should not be prefixed with '$'
You need to call parent::__construct();
$this->type= 'Child';
}
}
$Child= new Child();
echo $Child->getType;
also, you are calling a method, not accessing a property.
it should be
echo $Child->getType();
Can u see any reason why the type would return null? Thanks, Jeff well, 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 $type var $type; #<--- With this, I CAN echo $type public 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(); ?>