Alex Rivera | Logout

Method Chains PHP OOP

Asked 2010-02-21T19:17:02.607
12

Commonly, in a lot of frameworks, you can find examples of creating a query using the query builder. Often you will see:

$query->select('field');
$query->from('entity');

However, in some frameworks you can also do it like this

$object->select('field')
       ->from('table')   
       ->where( new Object_Evaluate('x') )
       ->limit(1) 
       ->order('x', 'ASC');

How do you actually do this kinds of chains?

Edit
Report

1 Answer

2
class c
{
  function select(...)
  {
    ...
    return $this;
  }
  function from(...)
  {
    ...
    return $this;
  }
  ...
}

$object = new c;
answered 2010-02-21T19:19:19.473

Your Answer