__call: invoke class method dynamically : Reflection Invoke « Class « PHP






__call: invoke class method dynamically


<?php
class A {
    function getA( ) {
        return array( "A", "B" );
    }

    function setA( $country, $district ) {
        return "$country, $district\n";
    }

}

class Helper {
    private $classA;

    function __construct( A $classA ) {
        $this->classA = $classA;
    }

    function __call( $method, $args ) {
        if ( method_exists( $this->classA, $method ) ) {
            return call_user_func_array(array( $this->classA, $method ), $args );
        }
    }
}

$tool= new Helper( new A() );
print_r( $tool->setA( 'UK', 'BN' ) );
?>

           
       








Related examples in the same category

1.call_user_func: invoke class method and pass parameters