Re: Re: working with class inheritance

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



# jefftaylor@xxxxxxxxxxxxxxx / 2007-03-20 19:14:17 +1030:
> ""Jeff Taylor"" <jefftaylor@xxxxxxxxxxxxxxx> wrote in
> message news:A1.92.40101.DAC9FF54@xxxxxxxxxxxxxxx
> > 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


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux