Re: [PHP] Inherit Methods
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 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Inherit Methods
Jochem Maas wrote: Norbert Wenzel wrote: private $var; get rid of the 'private' here and replace it with 'protected' too stupid. I'm sorry for my question. thank you for opening my eyes. norbert -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Inherit Methods
Norbert Wenzel wrote: Jochem Maas wrote: Norbert Wenzel wrote: private $var; get rid of the 'private' here and replace it with 'protected' too stupid. I'm sorry for my question. the only stupid people are those that don't make an effort - you don't seem to fall into that category. (I don't subscribe to the 'there are no stupid questions philosphy ;-) for instance 'Hey, Norbert - what is your name?', that woudl be a pretty stupid question :-P) thank you for opening my eyes. vanilla sky :-) norbert -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Inherit Methods
Hi ! you have to defined protected $var. This is a example where php5 OO model has a little ambiguities. Thing a few in your problem !, on de child class scope $var it's private then when yo execute printVar(), you aren't executed on parent scope you are calling a copie on child scope, then you don't have access to $var. Only when you use parent scope throw parent::printVar() , you realy calling the parent class instance into your child then print result. Then when you want to have a variable for being used on a public inherit method you have to defined protected On 8/8/05, Norbert Wenzel [EMAIL PROTECTED] 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 { private $var; 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 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Inherit Methods
*parent* keyword does not belong to PHP5 !!! It's there in PHP4 as well! On 8/8/05, Edwin Barrios [EMAIL PROTECTED] wrote: Hi ! you have to defined protected $var. This is a example where php5 OO model has a little ambiguities. Thing a few in your problem !, on de child class scope $var it's private then when yo execute printVar(), you aren't executed on parent scope you are calling a copie on child scope, then you don't have access to $var. Only when you use parent scope throw parent::printVar() , you realy calling the parent class instance into your child then print result. Then when you want to have a variable for being used on a public inherit method you have to defined protected On 8/8/05, Norbert Wenzel [EMAIL PROTECTED] 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 { private $var; 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 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- M.Saleh.E.G 97150-4779817