Henrique
Henrique

Reputation: 529

Is there a better alternative for using two $this-> in the same sentence?

If you do:

    $this->$this->name->something();

You will surely get an error.

I've been doing something like:

    $name =& $this->name;
    $this->$name->something();

But is there a better way for doing that? Would it be easier if PHP had some way of doing something like:

    $this->'$this->name'->something();

Thanks!

Upvotes: 0

Views: 58

Answers (2)

Berry Langerak
Berry Langerak

Reputation: 18859

But is there a better way for doing that? Would be easier, if PHP has some way to do, something like: $this->'$this->name'->something();

There is;

$this->{$this->name}->something( );

But... what are you doing? Do you really need this? It seems like an odd construct from where I'm standing.

Upvotes: 3

Alex Howansky
Alex Howansky

Reputation: 53626

If $this->name is an object, then $this->name->something() should work just fine.

Edit: Note also, if your methods return objects, you can just chain up the method calls:

$this->name->something()->somethingElse()->anotherThing();

Upvotes: 2

Related Questions