John Stamoutsos John Stamoutsos - 5 months ago 12
PHP Question

PHP how can make an function like this? first()->callSecond();?

I try to make an function like this below how can do that, any example?

$myClass->first()->callSecond();

Answer

You just need to return object all the time. It's called fluent interface. It can be self or other object.

<php
class A
{
    public function first()
    {
        // Do something

        return $this;
    }
    public function callSecond()
    {
        // Do somewthing else

        return $this;
    }
}

$a = (new A())->first()->callSecond();
Comments