13 Replies - 618 Views - Last Post: 07 May 2013 - 03:56 PM Rate Topic: -----

#1 DespicableMe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 07-May 13

WHat is return $this in oops?

Posted 07 May 2013 - 04:42 AM

Dear folks, i just entered into magento programming(Module development) and then on very first of it's programming part about how to make a magento module i encountered into a proble which is something like return *this(Fluent interface).For example:-

?php
class User
{
    protected $name;
    protected $surname;
    protected $password;

    public function setName($name)
    {
        $this->name = $name;
        return $this;
    }

    public function setSurname($surname)
    {
        $this->surname = $surname;
        return $this;
    }

    public function setPassword($password)
    {
        if (!$password) {
            throw new InvalidArgumentException("Password shouldn't be empty");
        }
        $this->password = $password;
        return $this;
    }
}



What exactly it is doing and why we use it. SOrry, i really sucks in oops.

Is This A Good Question/Topic? 0
  • +

Replies To: WHat is return $this in oops?

#2 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3392
  • View blog
  • Posts: 9,589
  • Joined: 08-June 10

Re: WHat is return $this in oops?

Posted 07 May 2013 - 04:45 AM

$this stands for "the current object". return $this; is mainly used for Chaining.
Was This Post Helpful? 2
  • +
  • -

#3 DespicableMe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 07-May 13

Re: WHat is return $this in oops?

Posted 07 May 2013 - 04:48 AM

View PostDormilich, on 07 May 2013 - 04:45 AM, said:

$this stands for "the current object". return $this; is mainly used for Chaining.


I know about $this means current object but if we will return current object then how shall we able to get the value of the function or vairables defined inside the class?
Method chaining something like this $object->myFunction()->myFunction1(); //Am i right?
Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3392
  • View blog
  • Posts: 9,589
  • Joined: 08-June 10

Re: WHat is return $this in oops?

Posted 07 May 2013 - 04:52 AM

View PostDespicableMe, on 07 May 2013 - 01:48 PM, said:

but if we will return current object then how shall we able to get the value of the function or vairables defined inside the class?

you donít apply Chaining to getters, itís as simple as that. as you can easily see in your example, return $this only occurs in setters (well, not that you have getters in there ...)
Was This Post Helpful? 1
  • +
  • -

#5 DespicableMe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 07-May 13

Re: WHat is return $this in oops?

Posted 07 May 2013 - 04:54 AM

If we will use it for setters then why exactly, we use this kinds of coding. Sorry, i am still confused in it..

This post has been edited by Dormilich: 07 May 2013 - 04:54 AM

Was This Post Helpful? 0
  • +
  • -

#6 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3392
  • View blog
  • Posts: 9,589
  • Joined: 08-June 10

Re: WHat is return $this in oops?

Posted 07 May 2013 - 04:55 AM

because it makes coding more simple?
Was This Post Helpful? 0
  • +
  • -

#7 DespicableMe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 07-May 13

Re: WHat is return $this in oops?

Posted 07 May 2013 - 05:00 AM

HMm okays, last thing i want to ask which is that if we use return $this then we are not getting any thing eg we are not displaying any output. What exactly we are doing is returning the class's object which is currently holding it or accessing the properties.

This post has been edited by Dormilich: 07 May 2013 - 05:01 AM

Was This Post Helpful? 0
  • +
  • -

#8 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3392
  • View blog
  • Posts: 9,589
  • Joined: 08-June 10

Re: WHat is return $this in oops?

Posted 07 May 2013 - 05:03 AM

which would you prefer?
$service->get("foo")->filter(OBJ_TYPE_FOOBAR)
    ->do()
    ->something()
    ->withIt()
;

or
$service->get("foo")->filter(OBJ_TYPE_FOOBAR)->do();
$service->get("foo")->filter(OBJ_TYPE_FOOBAR)->something();
$service->get("foo")->filter(OBJ_TYPE_FOOBAR)->withIt();

and now assume that ->get("foo") is a resource intense lookup process ...

Quote

if we use return $this then we are not getting any thing

otherwise the method would be a getter and not subject to chaining.
Was This Post Helpful? 0
  • +
  • -

#9 DespicableMe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 07-May 13

Re: WHat is return $this in oops?

Posted 07 May 2013 - 05:03 AM

View PostDormilich, on 07 May 2013 - 05:01 AM, said:

which would you prefer?
$service->get("foo")->filter(OBJ_TYPE_FOOBAR)
    ->do()
    ->something()
    ->withIt()
;

or
$service->get("foo")->filter(OBJ_TYPE_FOOBAR)->do();
$service->get("foo")->filter(OBJ_TYPE_FOOBAR)->something();
$service->get("foo")->filter(OBJ_TYPE_FOOBAR)->withIt();

and now assume that ->get("foo") is a resource intense lookup process ...


I would prefer first one...
Was This Post Helpful? 0
  • +
  • -

#10 lordofduct  Icon User is offline

  • I'm a cheeseburger
  • member icon


Reputation: 2506
  • View blog
  • Posts: 4,615
  • Joined: 24-September 10

Re: WHat is return $this in oops?

Posted 07 May 2013 - 01:30 PM

It's a cheap way of getting cascading or 'with' feature in a language that doesn't directly support it syntactically (like PHP).

Personally I don't like it because it comes with the requirement of designing it into your methods and classes. And if you miss one function, or a function returns something, it breaks the chain. Where as cascading or 'with' features it doesn't matter what.

Of course because PHP lacks that... this is really your only choice.
Was This Post Helpful? 0
  • +
  • -

#11 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3392
  • View blog
  • Posts: 9,589
  • Joined: 08-June 10

Re: WHat is return $this in oops?

Posted 07 May 2013 - 01:33 PM

I have never really missed with() in JS. making a reference is usually much faster there.
Was This Post Helpful? 0
  • +
  • -

#12 lordofduct  Icon User is offline

  • I'm a cheeseburger
  • member icon


Reputation: 2506
  • View blog
  • Posts: 4,615
  • Joined: 24-September 10

Re: WHat is return $this in oops?

Posted 07 May 2013 - 02:20 PM

if you want chaining like this though, with works very nicely.

Your argument against not missing with is a suitable argument against chaining.

(just so we're clear, I'm talking about the 'With' statement in VB, or cascading from SmallTalk which is similarish)

This post has been edited by lordofduct: 07 May 2013 - 02:23 PM

Was This Post Helpful? 0
  • +
  • -

#13 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3392
  • View blog
  • Posts: 9,589
  • Joined: 08-June 10

Re: WHat is return $this in oops?

Posted 07 May 2013 - 02:29 PM

since I do not know neither VB nor SmallTalk, I donít know which (dis)advantages with has there. Anyways, in case of JS the disadvantages far outweight the advantages.
Was This Post Helpful? 0
  • +
  • -

#14 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3635
  • View blog
  • Posts: 5,756
  • Joined: 08-June 10

Re: WHat is return $this in oops?

Posted 07 May 2013 - 03:56 PM

View PostDormilich, on 07 May 2013 - 12:03 PM, said:

which would you prefer?

To be fair, Dormi, your second snippet is the worst possible implementation of that you could have come up with. Nobody in their right mind would do that, whether chaining is available or not. Putting the object reference into a variable and executing the functions on that is the obvious way to handle that situation, and there is hardly any difference in syntax between doing that with a reasonably named variable and a chain. At least nothing that would warrant the effort and potential for unnecessary errors that comes with adding chaining support to classes.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1