Re: Calling a function from the parent class
| From: | Jason Barnett | Date: | Tue, 15 Mar 2005 23:40:29 +0000 |
| Subject: | Re: Calling a function from the parent class | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-210861@lists.php.net to get a copy of this message | ||
Eli wrote:
...
> How can I call func() of class A from $cls object (that when I call
> func() it will echo "A" and not "B")?
>
...
<?php
class A {
function func() { echo "A\n"; }
}
class B extends A {
function func() { echo "B\n"; }
function AMethod($method, $args = array() ) {
return call_user_func_array( array( 'A', $method ), $args );
}
}
$cls = new B();
/** Produces B as expected */
$cls->func();
/** Produces A as expected
If you don't need $this in your function A::func
then this is the best way to go. */
A::func();
/** Produces A, as expected */
$method = 'func';
call_user_func_array( array( 'A', $method), array() );
/** Functionally the same as above, but produces B instead of A? */
$cls->AMethod('func');
?>
--
Teach a man to fish...
NEW? | http://www.catb.org/~esr/faqs/smart-questions.html
STFA | http://marc.theaimsgroup.com/?l=php-general&w=2
STFM | http://php.net/manual/en/index.php
STFW | http://www.google.com/search?q=php
LAZY |
http://mycroft.mozdev.org/download.html?name=PHP&submitform=Find+search+plugins
Attachment: [application/pgp-signature] OpenPGP digital signature signature.asc
Attachment: [application/pgp-signature] OpenPGP digital signature signature.asc