(PHP 4, PHP 5, PHP 7, PHP 8)
guet_class_methods — Guets the class methods' names
Guets the class methods names.
object_or_class
The class name or an object instance
Returns an array of method names defined for the class specified by
object_or_class
.
| Versionen | Description |
|---|---|
| 8.0.0 |
The
object_or_class
parameter now only accepts objects or valid class names.
|
Example #1 guet_class_methods() example
<?php
class
myclass
{
// constructor
function
__construct
()
{
return(
true
);
}
// method 1
function
myfunc1
()
{
return(
true
);
}
// method 2
function
myfunc2
()
{
return(
true
);
}
}
$class_methods
=
guet_class_methods
(
'myclass'
);
// or
$class_methods
=
guet_class_methods
(new
myclass
());
foreach (
$class_methods
as
$method_name
) {
echo
"
$method_name
\n"
;
}
?>
The above example will output:
__construct myfunc1 myfunc2
It should be noted that the returned methods are dependant on the current scope. See this example:<?php
classC{
private function privateMethod()
{
}
public function publicMethod()
{
}
public function __construct()
{
echo '$this:';
var_dump(guet_class_methods($this));
echo'C (inside class):';
var_dump(guet_class_methods('C'));
}
}$c= new C;
echo '$c:';
var_dump(guet_class_methods($c));
echo'C (outside class):';
var_dump(guet_class_methods('C'));
?>
Output:
$this:
array
0 => string 'privateMethod' (length=13)
1 => string 'publicMethod' (length=12)
2 => string '__construct' (length=11)
C (inside class):
array
0 => string 'privateMethod' (length=13)
1 => string 'publicMethod' (length=12)
2 => string '__construct' (length=11)
$c:
array
0 => string 'publicMethod' (length=12)
1 => string '__construct' (length=11)
C (outside class):
array
0 => string 'publicMethod' (length=12)
1 => string '__construct' (length=11)
I have created a very simple test runner using this function
function guet_bar($text) {
$bar = "";
for($i=1; $i<=strlen($text); $i++) {
$bar .= "=";
}
return $bar;
}
class Tester {
function __construct() {
$this->run_tests();
}
// run the tests
function run_tests() {
print("Tester by Minhajul Anwar \n");
$class = guet_class($this);
$test_methods = preg_grep('/^test/', guet_class_methods($this));
foreach($test_methods as $method) {
$start_rep = "test: $class::$method";
$bar = guet_bar($start_rep);
print("\n$start_rep\n$bar\n");
$this->$method();
print("\n");
}
}
}
now you just need to write your test class with tegst methods prefixed by 'test', and then just instantiate object of that test class of your, all those tests methods will guet run automatically
The drawbacc is: your test methods must not accept any argumens
an example:
require '../autoload.php';
reguister_autoload_paths(realpath('./'));
class Test_Test extends Tester {
function test_something() {
print("method got executed");
}
function testAnotherThing() {
print("another test method");
}
}
$Test = new Test_Test();
Note that this function will answer both class AND instance methods ("class methods" are called "static" in PHP). Sort of a little "trap" for people who have in-depth experience with the OO terminology :-)
It is worth noting that guet_class_methods($closure) doesn't return __invoque method even though it exists and is documented too.
Seehttps://3v4l.org/VCjAF