Re: working with class inheritance
| From: | Jeff Taylor | Date: | Wed, 21 Mar 2007 03:20:14 +0000 |
| Subject: | Re: working with class inheritance | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-250963@lists.php.net to get a copy of this message | ||
Thanks everyone,
Gave me a much better understanding of it
"Jim Lucas" <lists@cmsws.com> wrote in message
news:45FFA7F9.3090105@cmsws.com...
> Jim Lucas wrote:
> > 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()
> >> {
> >> return $this->type;
> >> }
> >> }
> >>
> >> class Child implements Parent
> >> {
> >> $public function __construct()
> > public should not be prefixed with '$'
> > you are missing your open '{' for this method
> >>
> >
> > 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();
> >
> > ?>
> >
>
> 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();
>
> ?>