You are here: Re: [PHP] Inherit Methods « PHP « IT news, forums, messages
Re: [PHP] Inherit Methods

Posted by Jochem Maas on 10/04/74 11:23

Norbert Wenzel wrote:
> Is it possible to run inherited methods in the scope of the child class?
> In my case I have an abstract class called 'Company' and some child
> classes. There is a non abstract function in 'Company' which prints
> '$this->phoneList'. That function should be the same to all child
> classes, without rewritting it in every class.
>
> I call the printing method via the child class like
> $childObject->printPhoneList();
> The call seems to be handed over to the parent class 'Company' which is
> fine. But the $this points to the phoneList of the abstract parent
> class. So the phoneList in the abstract class seems to be unset, since i
> have set the phone list in the child class.
>
> Here's a short example, showing what I mean:
>
> <?php
>
> abstract class AbstractClass {

^ seeing as you are using the abstract keyword you must be using php5.
which supports the 'parent' keyword but that is not what you want right now...


>
> private $var;

get rid of the 'private' here and replace it with 'protected'

>
> public function printVar() {
> echo('var: ' . $this->var . '<br>');
> }
>
> }
>
> class ConcreteClass extends AbstractClass {
>
> public function __construct($var) {
> $this->var = $var;
> }
>
> public function printVarChild() {
> echo('var (child): ' . $this->var . '<br>');
> }
>
> }
>
> $cl = new ConcreteClass(15);
> $cl->printVar();
> $cl->printVarChild();
> ?>
>
> Output is:
> var:
> var (child): 15
>
>
> Has anyone an idea how to print the $var of the child, without copying
> the method in every child class?
>
> thanks in advance,
> Norbert
>

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация