[PHP] Re: working with class inheritance

2007-03-20 Thread Jeff Taylor
OOPS!... typo

Please replace implements with extends:

class Child extends Parent

Sorry about that
Jeff Taylor [EMAIL PROTECTED] wrote in message
news:[EMAIL PROTECTED]
 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
 {
   $private type;

   public function __construct()
   {
   }

public function GetType()
{
   return $this-type;
   }
 }

 class 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?

 Thanks,

 Jeff


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] Re: working with class inheritance

2007-03-20 Thread Jeff Taylor
I forgot to mention that I have some __construct operations in the parent
class, and I want to add to those (and overwrite sometimes) in the child
class __construct

 i.e Parent class __construct
$this-foo = 'Bar';
$this-foo2 = '10';

Child class __construct
  $this-foo.='Bar';
  $this-foo2 .= rand(1,6);
  $this-type = 'Child';

Outputs of variables foo == Bar Bar
   foo2 == 11-16
type == Child

Thanks :)



Jeff Taylor [EMAIL PROTECTED] wrote in message
news:[EMAIL PROTECTED]
 OOPS!... typo

 Please replace implements with extends:

 class Child extends Parent

 Sorry about that
 Jeff Taylor [EMAIL PROTECTED] wrote in message
 news:[EMAIL PROTECTED]
  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
  {
$private type;
 
public function __construct()
{
}
 
 public function GetType()
 {
return $this-type;
}
  }
 
  class 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?
 
  Thanks,
 
  Jeff
 

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Re: working with class inheritance

2007-03-20 Thread Roman Neuhauser
# [EMAIL PROTECTED] / 2007-03-20 19:14:17 +1030:
 Jeff Taylor [EMAIL PROTECTED] wrote in
 message news:[EMAIL PROTECTED]
  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
  {
$private type;
 
public function __construct()
{
}
 
 public function GetType()
 {
return $this-type;
}
  }
 
  class 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?

 OOPS!... typo
 
 Please replace implements with extends:
 
 class Child extends Parent

The code you posted doesn't parse either way, next time please post the
real thing. There's several problems in the code as posted, and either
could cause your problem.  You're probably looking for this:

class parent {
public function __construct() {}
private $type;
protected function settype($type) {
$this-type = $type;
}
public function gettype() {
return $this-type;
}
}
class child {
public function __construct() {
$this-settype('Child');
}
}

If all instances of parent subclasses must have a type assigned for
their whole lifetime it's better to have parent require it, and not
provide its descendants a way to change the type during their life:

class parent {
protected function __construct($type) {
$this-type = $type;
}
private $type;
public function gettype() {
return $this-type;
}
}
class child {
public function __construct() {
parent::__construct('Child');
}
}


-- 
How many Vietnam vets does it take to screw in a light bulb?
You don't know, man.  You don't KNOW.
Cause you weren't THERE. http://bash.org/?255991

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] Re: working with class inheritance

2007-03-20 Thread Gregory Beaver
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
 {
   $private type;
 
   public function __construct()
   {
   }
 
public function GetType()
{
   return $this-type;
   }
 }
 
 class 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 class, you must use protected or declare a setter function as
Roman suggested.  However, setter functions are far less efficient than
simply using protected:

?php
class whatever
{
protected $type = 'whatever';
}

class Child extends whatever
{
public function __construct() {$this-type='Child';}
}
?

It should be noted that if you simply want to store the class name, a
better approach is to use get_class()

?php
class whatever {}
class child {}
$a = new whatever;
$b = new child;
echo get_class($a), ' ', get_class($b);
?

Of course, you may want to remove a prefix from the classname, in which
case you could also use a simple __get()/__set() declaration that
prevents accidental modification of object type:

?php
class prefix_myclass
{
function __get($var)
{
if ($var == 'type') return str_replace('prefix_', '',
get_class($this));
}

function __set($var, $value)
{
if ($var == 'type') return; // ignore
}
}
class prefix_child extends prefix_myclass {}

$a = new prefix_myclass;
$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

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php