!Concerning PHP5 Only!
If you want to get all methods/functions from a class you can do this with the get_class_methods function.
<?php
$arrMethodNames = get_class_methods ( $ClassNameOrObject);
?>
However the drawback on this function in PHP5 is that you will NOT receive protected and private methods of a class/object if you are calling the method from another class/object context.
If you want to receive all methods of a given Class in another Class you should use the PHP5 Reflection API. The following source allows to retrieve all methods from a derived class in its (abstract) Base Class.
In the example you need to call the base constructor from the derived classes constructor in order to let the base class know the name of the derived class. Use the "__CLASS__" definition for passing the classname of current class to its base class.
<?php
// Base Class - Abstract
abstract class A {
// Array of ReflectionMethod Objects
// is being set upon instanciation
// derived Classes don't need to know about this array
private $arrMethods;
// Constructor which MUST be called from derived Class Constructor
protected function __construct ( $strDerivedClassName ) {
$oRefl = new ReflectionClass ( $strDerivedClassName );
if ( is_object($oRefl) ) {
$this->arrMethods = $oRefl->getMethods();
}
}
// Some abstract function
abstract protected function D ();
// Some private function
private function E() {
}
// method to print all class/object methods
// must be callable from derived classes
// can be protected if only for internal class use
public function PrintAllMethods () {
foreach ( $this->arrMethods as $curReflectionMethod ) {
echo $curReflectionMethod->getName()."<br>";
}
}
}
// Derived Class
class B extends A {
// Constructor for this class
// MUST call Base Constructor
public function __construct () {
// Call Base Constructor
parent::__construct(__CLASS__);
}
// Some public function
public function A () {
}
// some protected function
protected function B () {
}
// some private function
private function C() {
}
// some abstract method from base class implemented
protected function D () {
}
}
// Create new B Object
$b = new B();
// Print all Methods of this Object/Class
$b->PrintAllMethods();
?>
In this example output will be:
__construct
A
B
C
D
E
PrintAllMethods
As you can see these are all methods from class B and also all methods from Class A respectivly in order of their declaration. Note that only one method "__construct" and only one method "D" are being shown. This is due to overloading (__construct) and implementing (D) in PHP.
In this example any further method-handling methods should be implemented in the base class as these will be available in derived classes as well. Just make sure that you use the right access specifiers for these additional methods ( private, protected, public )
BoD