Oreborous
Oreborous

Reputation: 332

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.

Upvotes: 1

Views: 1466

Answers (2)

arkascha
arkascha

Reputation: 42915

I just surprised myself by proving that this is indeed possible:

<?php
class Test {
    private static $myself;
    public function do() {
        echo isset($this) ? 'd' : 's';
        if (!isset(self::$myself)) {
            self::$myself = new self;
        }
        return self::$myself;
    }
    public static function done() {
        echo PHP_EOL;
    }
}
Test::do()->do()->do()->done();
$myTest = new Test;
$myTest->do()->do()->do()->done();
$myTest::do()->do()->do()->done();
$myTest::do()->do()::do()->done();
Test::do()->do()->do()->done();

The output is:

sdd
ddd
sdd
sds
sdd

But it is unbelievable bad style in my eyes...

Upvotes: 2

Mike
Mike

Reputation: 24383

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 

Upvotes: 0

Related Questions