Search code examples
phpmethodsdynamicstaticchaining

Allow PHP function to be called dynamically and statically


I'm building an application, and I need to be able to chain methods, so I will need to be able to call methods dynamically and statically.

For example:

$results = Class::where('something')
                ->where('something else');

In this case, the method 'where' needs to be called statically and dynamically, and still be allowed to be chained.

I know Laravel has something like this with Eloquent, but I do not know how to implement something like this.


Solution

  • Just out of pure interest I've found a way to do this using __call and __callStatic and setting the methods you want to call to be inaccessible to the scope that is calling it (i.e. set the methods as private or protected).

    Important warning: Using __call or __callStatic can lead to unexpected behavior! First read here to make sure you understand this. The code below dynamically executes the private class methods called from outside the class, so you need to be sure to whitelist specific methods that you want to add this behavior to. There may be other security issues that I have not dealt with, so use at your own risk.

    class MethodTest
    {
    
        private static $obj;
        private $fruit = 'watermelon';
    
        private function handleDynamicCallType($name, $arguments)
        {
            switch ($name) {
                case 'method1':
                case 'method2':
                    $this->{$name}($arguments);
                    break;
                default:
                    throw new Exception("Invalid name");
            }
        }
    
        private function method1() {
            echo $this->fruit . ' ';
        }
    
        private function method2() {
            $arg_list = func_get_args();
            if (!empty($arg_list[0][0])) {
                $this->fruit = $arg_list[0][0];
            }
        }
    
        public function __call($name, $arguments)
        {
            $this->handleDynamicCallType($name, $arguments);
            return $this;
        }
    
        public static function __callStatic($name, $arguments)
        {
            if (!isset(self::$obj)) {
                self::getNewStaticInstance();
            }
            self::$obj->{$name}($arguments);
            return self::$obj;
        }
    
        public static function getNewStaticInstance()
        {
            self::$obj = new MethodTest();
            return self::$obj;
        }
    }
    
    $obj = new MethodTest;
    $obj->method1()::method2('orange')->method1();
    echo PHP_EOL;
    MethodTest::method1()->method2('plum')::method1();
    

    Output:

    watermelon orange 
    orange plum 
    

    However the static object retains its properties after being called previously (notice the two orange strings). If this is undesired, we can force it to reset by calling getNewStaticInstance():

    MethodTest::method1()::method2('plum')::method1()::getNewStaticInstance()::method1();
    

    Output:

    watermelon plum watermelon