note 63183 added to function.get-class-methods
| From: | BoD at osu1 dot php dot net | Date: | Wed, 15 Mar 2006 15:09:00 +0000 |
| Subject: | note 63183 added to function.get-class-methods | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-105860@lists.php.net to get a copy of this message | ||
!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
----
Server IP: 217.160.72.57
Probable Submitter: 212.8.217.254 (proxied: 10.10.2.115)
----
X-Spam-Status: No, hits=3.3 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER,
HTML_MESSAGE autolearn=no version=2.64
----
Manual Page -- http://www.php.net/manual/en/function.get-class-methods.php
Edit -- http://master.php.net/note/edit/63183
Del: integrated -- http://master.php.net/note/delete/63183/integrated
Del: useless -- http://master.php.net/note/delete/63183/useless
Del: bad code -- http://master.php.net/note/delete/63183/bad+code
Del: spam -- http://master.php.net/note/delete/63183/spam
Del: non-english -- http://master.php.net/note/delete/63183/non-english
Del: in docs -- http://master.php.net/note/delete/63183/in+docs
Del: other reasons-- http://master.php.net/note/delete/63183
Reject -- http://master.php.net/note/reject/63183
Search -- http://master.php.net/manage/user-notes.php