Re: Calling a function from the parent class

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

 



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: signature.asc
Description: OpenPGP digital signature


[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