Jeff Taylor wrote:
> Hey all, got a slight problem, where for some reasons my variables dont seem
> to be getting stored in the child cl***:
>
> e.g
>
> cl*** Parent
> {
> $private type;
>
> public function __construct()
> {
> }
>
> public function GetType()
> {
> return $this->type;
> }
> }
>
> cl*** Child implements Parent
> {
> $public function __construct()
>
>
> $this->type= 'Child';
> }
> }
>
> $Child= new Child();
> echo $Child->getType;
>
> Can u see any reason why the type would return null?
Hi Jeff,
Aside from your obvious parse errors, the main problem is that you are
incorrectly using "private." Since you wish to access $type from the
child cl***, you must use "protected" or declare a setter function as
Roman suggested. However, setter functions are far less efficient than
simply using protected:
<?php
cl*** whatever
{
protected $type = 'whatever';
}
cl*** Child extends whatever
{
public function __construct() {$this->type='Child';}
}
?>
It should be noted that if you simply want to store the cl*** name, a
better approach is to use get_cl***()
<?php
cl*** whatever {}
cl*** child {}
$a = new whatever;
$b = new child;
echo get_cl***($a), ' ', get_cl***($b);
?>
Of course, you may want to remove a prefix from the cl***name, in which
case you could also use a simple __get()/__set() declaration that
prevents accidental modification of object type:
<?php
cl*** prefix_mycl***
{
function __get($var)
{
if ($var == 'type') return str_replace('prefix_', '',
get_cl***($this));
}
function __set($var, $value)
{
if ($var == 'type') return; // ignore
}
}
cl*** prefix_child extends prefix_mycl*** {}
$a = new prefix_mycl***;
$b = new prefix_child;
echo $a->type , ' ' , $b->type;
$a->type = 5;
echo $a->type , ' ' , $b->type;
?>
Regards,
Greg
--
Experience the revolution, buy the PEAR Installer Manifesto
http://www.packtpub.com/book/PEAR-installer