I found an extremely useful article that suggests using PHP's ReflectionClass
to introspect what a parent class expects, here: PHP – Reflection Class – Determine Parent Method Signature.
I found no answer providing this tip on Stack Overflow, so I decided to submit this self-answered question with (my own extended version of) the article's sample code.
Use this snippet to learn more about the class you're trying to extend:
<?php
$className = 'PDO'; // the class you're trying to extend
echo "<pre>\n";
echo "CLASS : $className\n\n========\n\n";
$refClass = new ReflectionClass($className);
foreach ($refClass->getMethods() as $refMethod) {
echo "METHOD : " . $refMethod->getName(). "\n\n";
echo "getNumberOfParameters() : " . $refMethod->getNumberOfParameters() . "\n";
echo "getNumberOfRequiredParameters() : " . $refMethod->getNumberOfRequiredParameters() . "\n";
echo "\n";
foreach ($refMethod->getParameters() as $refParameter) {
echo $refParameter . "\n";
}
echo "\n--------\n\n";
}
echo "</pre>\n";
?>
This outputs, for PHP's PDO
class for example:
CLASS : PDO
========
METHOD : __construct
getNumberOfParameters() : 4
getNumberOfRequiredParameters() : 3
Parameter #0 [ $dsn ]
Parameter #1 [ $username ]
Parameter #2 [ $passwd ]
Parameter #3 [ $options ]
--------
METHOD : prepare
getNumberOfParameters() : 2
getNumberOfRequiredParameters() : 1
Parameter #0 [ $statment ]
Parameter #1 [ $options ]
--------
METHOD : beginTransaction
getNumberOfParameters() : 0
getNumberOfRequiredParameters() : 0
[...]
Note how, in the linked article, one sees entries like:
string(37) "Parameter #1 [ <optional> $cache_cb ]"
I did not see similar <optional>
markers in my output (using the author's code verbatim), whether it's because I was using a different version of PHP (PHP 5.3.13) or it's something about the underlying PDO library. Suspecting that my issue might have something to do with optional parameters, I sought an alternative way to find out about them using the getNumberOf*Parameters
methods(—and indeed my suspicion proved correct).
There are plenty of other useful reflection methods if one desires to output more information, of course. For example, method accessibility via ReflectionMethod::isPrivate
, ReflectionMethod::isProtected
, etc. Finally, note that the class ReflectionMethod
extends ReflectionFunctionAbstract
so be sure to check out the parent class's methods too.