meWantToLearn
meWantToLearn

Reputation: 1700

php methods binded together

I have seen in ORMS or Frameworks, that you can have methods attached together

Example,

$user->select('id')->where('web');

So, how can where method attached to select method?

Upvotes: 0

Views: 42

Answers (2)

BenM
BenM

Reputation: 53198

The method select() simply returns the current instance from itself. For example:

public function select($column)
{
    // Do some cool stuff;
    return $this;
}

If the object also has a method called where(), this is now available using the chained syntax you've indicated.

Upvotes: 0

Barmar
Barmar

Reputation: 781028

That expression is equivalent to:

$temp = $user->select('id');
$temp->where('web');

It's no different from combining multiple arithmetic operations in a mathematical expression: the result of one sub-expression is used as a parameter to the adjacent one. In this case, select() returns a class object that has a where() method.

Upvotes: 1

Related Questions