Showing posts with label static. Show all posts
Showing posts with label static. Show all posts

Thursday, December 7, 2017

PHP Call Static Function Method Dynamically Based on __callStatic Method



<?php
abstract class ParentClass {
    public static function __callStatic($method, $args) {
        echo "Method=".$method.",Args=".json_encode($args)."\r\n<BR/>";
    }
}

class ChildClass extends ParentClass {
    public static function x1() {
        $args = func_get_args();
        call_user_func_array(
            array(parent::class, __FUNCTION__), $args
        );
    }
}

ChildClass::x1("x1", 10, 20);
ChildClass::x2("x2", 30, 40);


And output would be like:


Method=x1,Args=["x1",10,20] 
Method=x2,Args=["x2",30,40]