Speed up development with full-stack environments for every branch.

Learn More

Dynamically invoking methods that a class has using Reflection [PHP]

Forked from Modify a private variable using Reflection.

21 Runs 247 Views 19 Copies
Saved

Saved

gtsolutions 373

gtsolutions
published 4 years ago

<?php

/*
 * PHP Reflection - Dynamically invoking methods that a class has using Reflection
 */

class MyClass {

    public $name;
    private $password;

    function __secret() {
        echo 'This is a "secret" method<br />';
    }

    public function method() {
        echo 'This is a regular method <br />';
    }

}

$object = new MyClass();
$myClassReflection = new ReflectionClass('MyClass');
$methods = $myClassReflection->getMethods();
foreach ($methods as $method)
    $method->invoke($object);
?>
Please login/signup to get access to the terminal.

Your session has timed out.

Dismiss (the page may not function properly).