5
function select($select = '*')
    {
        if( is_string($select) ) {
            $select = explode(',', $select) ;
        }

        foreach($select as $s) {
            $s = trim($s) ;

            if($s != '') {
                $this->aSelect[] = $s ;
            }
        }

        return $this ;
    }

In PHP, what is meant by return $this.

3
  • return means what it always means: return a value from a function. $this is the current object, usually the object through which the current member function was called.
    – tdammers
    Commented Aug 26, 2012 at 6:37
  • by returning $this the function return the current object. then what is the purpose of body of the function. we can write the function somthing (){ retuen $this; } Commented Aug 26, 2012 at 6:41
  • I took the liberty to change the title to clarify.
    – tdammers
    Commented Aug 26, 2012 at 7:02

3 Answers 3

16

There are many scenarios in which one might want to return $this from a function, but the most popular one is 'method chaining'.

For example, in an SQL abstraction layer, you may have an object that represents a query, and then call a series of methods on it to extend it. Consider the following code:

$query = $database->select();
$query->from('users');
$query->whereEquals('username', $username);
$query->orderBy('username');
$query->limit(1);
$user = $query->executeSingleRow();

If each of $query's methods returns the modified query object, we can instead write this as:

$user = $database
            ->select()
            ->from('users')
            ->whereEquals('username', $username)
            ->orderBy('username')
            ->limit(1)
            ->executeSingleRow();

The second version is closer to how you'd write an actual SQL query, and it works without introducing the exta $query variable.

3

By returning $this you it makes it easier for the programmer to chain commands. Consider a car object. You could say $car->start()->forward()->left()->forward() on line if you return $this in each function.

There is actually a question about this in stack overflow which could help clarify what method chaining is: https://stackoverflow.com/questions/3724112/php-method-chaining

0

Chian type programming... return $this will return a same object all the time. that make you be ready to call upon it a next function, next function again return same instance such that to call upon it a third function and so on.

see example first

class MY_CLASS(){
    string attrib; 
    func SetMethod(string value){this.attrib=value;}
}

to call Method1 on un-known object, we can write as following.

(new MY_CLASS()).Method1

expend above class

 class MY_CLASS(){
        string attrib; 
       func SetMethod(string value){this.attrib=value;}
       func int GetMethod(){return this.Attrib);}
    }

Now to call it for GetMethod we can't do with un-known object as following new MY_CLASS().SetMethod(30); a = new My_CLASS().GetMethod();

Yah in other languages like C# we can do as following

a=(new MY_CLASS().SetMethod(30)).GetMethod();

to achieve same facilities in php you need to write 'return $this' in function where you think to be use for consegative calls

Hope answer is clear.

Not the answer you're looking for? Browse other questions tagged or ask your own question.