# [RFC] new foo()->bar()

- Thread: https://externals.io/message/50592
- Original archive: https://news-web.php.net/php.internals/50592
- Started by Felipe Pena on 2010-11-26T19:36:07+00:00
- Messages: 34
- Page 1 of 2: messages 1 to 25 (25 per page, add `?page=N` to the URL)
- Next page: https://externals.io/message/50592.md?page=2
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #50592 — Felipe Pena — 2010-11-26T19:36:07+00:00

URL: https://externals.io/message/50592#50592

Hi all,
I'm here again to presents another proposal, which adds support for
instantiating a class and calling its methods and accessing its properties
on same command.

Example:

<?php

class bar {
  public $x = 'PHP';
}

class foo extends bar {
  public function bar() {
    return $this;
  }
}

var_dump(new foo()->bar()->x); // string(3) "PHP"

?>

Other examples which describes the feature at
http://wiki.php.net/rfc/instance-method-call

Thoughts?

-- 
Regards,
Felipe Pena

---

## #50593 — Ilia Alshanetsky — 2010-11-26T19:51:55+00:00

URL: https://externals.io/message/50592#50593 · In reply to: #50592

+1

Seems like a handy change and the patch is quite manageable.


> Hi all,
> I'm here again to presents another proposal, which adds support for
> instantiating a class and calling its methods and accessing its properties
> on same command.
>
> Example:
>
> <?php
>
> class bar {
>  public $x = 'PHP';
> }
>
> class foo extends bar {
>  public function bar() {
>    return $this;
>  }
> }
>
> var_dump(new foo()->bar()->x); // string(3) "PHP"
>
> ?>
>
> Other examples which describes the feature at
> http://wiki.php.net/rfc/instance-method-call
>
> Thoughts?
>
> --
> Regards,
> Felipe Pena

---

## #50594 — Daniel Brown — 2010-11-26T20:00:33+00:00

URL: https://externals.io/message/50592#50594 · In reply to: #50592

> Hi all,
> I'm here again to presents another proposal, which adds support for
> instantiating a class and calling its methods and accessing its properties
> on same command.
>
> Example:
>
> <?php
>
> class bar {
>  public $x = 'PHP';
> }
>
> class foo extends bar {
>  public function bar() {
>    return $this;
>  }
> }
>
> var_dump(new foo()->bar()->x); // string(3) "PHP"
>
> ?>
>
> Other examples which describes the feature at
> http://wiki.php.net/rfc/instance-method-call
>
> Thoughts?

    I'm all for it, Felipe.  Chaining like that would really come in
handy in several situations of which I can think right off the top of
my head even.

-- 
</Daniel P. Brown>
Network Infrastructure Manager
Documentation, Webmaster Teams
http://www.php.net/

---

## #50608 — Bob Hensley — 2010-11-26T20:07:45+00:00

URL: https://externals.io/message/50592#50608 · In reply to: #50592

> Hi all,
> I'm here again to presents another proposal, which adds support for
> instantiating a class and calling its methods and accessing its properties
> on same command.
>
> Example:
>
> <?php
>
> class bar {
>    public $x = 'PHP';
> }
>
> class foo extends bar {
>    public function bar() {
>      return $this;
>    }
> }
>
> var_dump(new foo()->bar()->x); // string(3) "PHP"
>
> ?>
>
> Other examples which describes the feature at
> http://wiki.php.net/rfc/instance-method-call
>
> Thoughts?
>

I fully support this patch.  This is something PHP has needed for a long 
time.

---

## #50595 — johannes@schlueters.de — 2010-11-26T20:21:43+00:00

URL: https://externals.io/message/50592#50595 · In reply to: #50592

> var_dump(new foo()->bar()->x); // string(3) "PHP"

It has some readability issues. One might assume it is

    new (foo()->bar()->x)

not

    (new foo())->bar()->x

As there is a mandatory space between "new" and its operand and no space
in front of the object operator and we allow non-constant operands to
"new".

So what is

    new $bar->foo();

? If I read the patch correctly this is valid and evaluated as

   (new $bar)->foo();

johannes

---

## #50596 — Ferenc Kovacs — 2010-11-26T20:23:23+00:00

URL: https://externals.io/message/50592#50596 · In reply to: #50592

> Hi all,
> I'm here again to presents another proposal, which adds support for
> instantiating a class and calling its methods and accessing its properties
> on same command.
>
> Example:
>
> <?php
>
> class bar {
>  public $x = 'PHP';
> }
>
> class foo extends bar {
>  public function bar() {
>    return $this;
>  }
> }
>
> var_dump(new foo()->bar()->x); // string(3) "PHP"
>
> ?>
>
> Other examples which describes the feature at
> http://wiki.php.net/rfc/instance-method-call
>
> Thoughts?
>
> --
> Regards,
> Felipe Pena
>


I like it, and it is a good pair with the new array dereferencing, so
hopefully I don't have to create temp variables anymore \o/

Tyrael

---

## #50597 — Peter Lind — 2010-11-26T20:25:32+00:00

URL: https://externals.io/message/50592#50597 · In reply to: #50592

> Hi all,
> I'm here again to presents another proposal, which adds support for
> instantiating a class and calling its methods and accessing its properties
> on same command.
>
> Example:
>
> <?php
>
> class bar {
>  public $x = 'PHP';
> }
>
> class foo extends bar {
>  public function bar() {
>    return $this;
>  }
> }
>
> var_dump(new foo()->bar()->x); // string(3) "PHP"
>
> ?>
>
> Other examples which describes the feature at
> http://wiki.php.net/rfc/instance-method-call
>
> Thoughts?

It seems fairly handy and I've been in situations where I wanted to do
something like that - in fact, I use factories to achieve something
similar.
 However, the more I use it, the more it feels like introducing code
smells into my code. You're essentially instantiating an object only
to immediately throw it away. That means you don't actually need the
object at all, you should probably be looking for static methods or
class properties. Trying to avoid statics by introducing a way to
instantiate and throw away objects in the same statement feels a lot
like reinventing OOP while adding overhead.

Anyway, just a personal observation. I generally favour the way that
PHP allows you to dig your own grave (i.e. I love the freedom of the
language), so as a developer I would probably favour this as well,
though I find it mainly a way to introduce hacks.

Regards
Peter

-- 
<hype>
WWW: plphp.dk / plind.dk
LinkedIn: plind
BeWelcome/Couchsurfing: Fake51
Twitter: kafe15
</hype

---

## #50598 — Ferenc Kovacs — 2010-11-26T20:37:32+00:00

URL: https://externals.io/message/50592#50598 · In reply to: #50597

> 
> > […]
> properties
> > […]
>
> It seems fairly handy and I've been in situations where I wanted to do
> something like that - in fact, I use factories to achieve something
> similar.
>  However, the more I use it, the more it feels like introducing code
> smells into my code. You're essentially instantiating an object only
> to immediately throw it away. That means you don't actually need the
> object at all, you should probably be looking for static methods or
> class properties. Trying to avoid statics by introducing a way to
> instantiate and throw away objects in the same statement feels a lot
> like reinventing OOP while adding overhead.
>
> Anyway, just a personal observation. I generally favour the way that
> PHP allows you to dig your own grave (i.e. I love the freedom of the
> language), so as a developer I would probably favour this as well,
> though I find it mainly a way to introduce hacks.
>
>
1, I have to use a non-trivial library or "module" for a simple task, and I
don't want to write 20 line of code, and introduce 4 helper variable.
2. I want to get from point 1 to point 5 but I'm not interested in the steps
in-between (classical method chaining), but sadly one of the steps requires
object instantiation.

Tyrael

---

## #50599 — Peter Lind — 2010-11-26T20:46:25+00:00

URL: https://externals.io/message/50592#50599 · In reply to: #50598

>
>
> 
> > […]
>
> 1, I have to use a non-trivial library or "module" for a simple task, and I
> don't want to write 20 line of code, and introduce 4 helper variable.

If it's a one-off, then I really don't see the problem. If you're
facing it again, write a facade.

> 2. I want to get from point 1 to point 5 but I'm not interested in the steps
> in-between (classical method chaining), but sadly one of the steps requires
> object instantiation.

If it's your code, then why are you not simplifying it? What's the
point of writing code that you have to go through in five steps? Why
not write a wrapper method?
 The reasons presented sounds quite like "I want to be able write
hacks easier" rather than "I want to fix an actual problem". I.e.
there are solutions for this already that use OOP principles.

That said, this fix may very well address other situations :)

Regards
Peter

-- 
<hype>
WWW: plphp.dk / plind.dk
LinkedIn: plind
BeWelcome/Couchsurfing: Fake51
Twitter: kafe15
</hype

---

## #50600 — Felipe Pena — 2010-11-26T20:51:35+00:00

URL: https://externals.io/message/50592#50600 · In reply to: #50595

2010/11/26 Johannes Schlüter <johannes@schlueters.de>

> 
> > […]
>
> It has some readability issues. One might assume it is
>
>    new (foo()->bar()->x)
>
> not
>
>    (new foo())->bar()->x
>
> As there is a mandatory space between "new" and its operand and no space
> in front of the object operator and we allow non-constant operands to
> "new".
>
> So what is
>
>    new $bar->foo();
>
> ? If I read the patch correctly this is valid and evaluated as
>
>   (new $bar)->foo();
>
> johannes
>
>

new foo()->bar() should be read as: (new foo())->bar().

And using variable:

new $bar->y()->x should be read as: (new ($bar->y)())->x.

<?php

class foo {
public $x = 1;
}

class bar {
public $y = 'foo';
}

$bar = new bar;

var_dump(new $bar->y()->x); // 1

?>

I.e. just as it is nowdays.

-- 
Regards,
Felipe Pena

---

## #50601 — Ferenc Kovacs — 2010-11-26T20:52:10+00:00

URL: https://externals.io/message/50592#50601 · In reply to: #50599

> 
> > […]
> wrote:
> > […]
> I
> > […]
>
> If it's a one-off, then I really don't see the problem. If you're
> facing it again, write a facade.
>
> > […]
> steps
> > […]
> requires
> > […]
>
> If it's your code, then why are you not simplifying it? What's the
> point of writing code that you have to go through in five steps? Why
> not write a wrapper method?
>  The reasons presented sounds quite like "I want to be able write
> hacks easier" rather than "I want to fix an actual problem". I.e.
> there are solutions for this already that use OOP principles.
>
>
Sorry, I don't have the time and/or patience to fix every code out there,
which I might happen to come across in a project. :)



> That said, this fix may very well address other situations :)
>
> sure thing, I just told a(two) use-case from the top of my head.

Tyrael

---

## #50607 — Mike Robinson — 2010-11-26T21:53:10+00:00

URL: https://externals.io/message/50592#50607 · In reply to: #50592

November-26-10 2:36 PM, Felipe Pena writes:

> Hi all,
> I'm here again to presents another proposal, which adds support for
> instantiating a class and calling its methods and accessing its
> properties on same command.
> 
> Example:
> 
> <?php
> 
> class bar {
>   public $x = 'PHP';
> }
> 
> class foo extends bar {
>   public function bar() {
>     return $this;
>   }
> }
> 
> var_dump(new foo()->bar()->x); // string(3) "PHP"
> 
> ?>
> 
> Other examples which describes the feature at
> http://wiki.php.net/rfc/instance-method-call
> 
> Thoughts?

Nice. I have use for this.
Some readability issues may arise but nothing that can't be
overcome with some common sense.

Best Regards,


Mike Robinson

---

## #50609 — Gustavo Lopes — 2010-11-26T22:04:17+00:00

URL: https://externals.io/message/50592#50609 · In reply to: #50597

On Fri, 26 Nov 2010 20:25:32 -0000, Peter Lind <peter.e.lind@gmail.com>  
wrote:

> It seems fairly handy and I've been in situations where I wanted to do
> something like that - in fact, I use factories to achieve something
> similar.
>  However, the more I use it, the more it feels like introducing code
> smells into my code. You're essentially instantiating an object only
> to immediately throw it away.

Not necessarily; you could be calling the method for the collateral  
effects and that method return the object itself. This is not that  
uncommon.

+1

-- 
Gustavo Lopes

---

## #50610 — Peter Lind — 2010-11-26T22:19:07+00:00

URL: https://externals.io/message/50592#50610 · In reply to: #50609

> 
>
>
> It seems fairly handy and I've been in situations where I wanted to do
> something like that - in fact, I use factories to achieve something
> similar.
>  However, the more I use it, the more it feels like introducing code
> smells into my code. You're essentially instantiating an object only
> to immediately throw it away.
>
>
> Not necessarily; you could be calling the method for the collateral effects and that method return the object itself. This is not that uncommon.
>

And I can do that today with a factory pattern, if I want to.
Anyway, I don't want to argue against the feature, as it will just
introduce a slightly shorter version of something we can already do.

-- 
<hype>
WWW: plphp.dk / plind.dk
LinkedIn: plind
BeWelcome/Couchsurfing: Fake51
Twitter: kafe15
</hype

---

## #50611 — Adam Richardson — 2010-11-26T22:40:20+00:00

URL: https://externals.io/message/50592#50611 · In reply to: #50592

> Hi all,
> I'm here again to presents another proposal, which adds support for
> instantiating a class and calling its methods and accessing its properties
> on same command.
>
> Example:
>
> <?php
>
> class bar {
>  public $x = 'PHP';
> }
>
> class foo extends bar {
>  public function bar() {
>    return $this;
>  }
> }
>
> var_dump(new foo()->bar()->x); // string(3) "PHP"
>
> ?>
>
> Other examples which describes the feature at
> http://wiki.php.net/rfc/instance-method-call
>
> Thoughts?
>
> --
> Regards,
> Felipe Pena
>

Felipe, you're on a roll :)  It's great!

Adam

-- 
Nephtali:  PHP web framework that functions beautifully
http://nephtaliproject.com

---

## #50612 — Pierrick Charron — 2010-11-26T23:45:46+00:00

URL: https://externals.io/message/50592#50612 · In reply to: #50592

+1
Good job felipe



> Hi all,
> I'm here again to presents another proposal, which adds support for
> instantiating a class and calling its methods and accessing its properties
> on same command.
>
> Example:
>
> <?php
>
> class bar {
>  public $x = 'PHP';
> }
>
> class foo extends bar {
>  public function bar() {
>    return $this;
>  }
> }
>
> var_dump(new foo()->bar()->x); // string(3) "PHP"
>
> ?>
>
> Other examples which describes the feature at
> http://wiki.php.net/rfc/instance-method-call
>
> Thoughts?
>
> --
> Regards,
> Felipe Pena

---

## #50613 — Kalle Sommer Nielsen — 2010-11-26T23:48:53+00:00

URL: https://externals.io/message/50592#50613 · In reply to: #50592

Hi Felipe

2010/11/26 Felipe Pena <felipensp@gmail.com>:
> Other examples which describes the feature at
> http://wiki.php.net/rfc/instance-method-call
>
> Thoughts?

huge +1 from me ;-) It might be worth adding function call chaining
with dereferencing and instance method call chaning, like $a =
function(){ return function(){ echo 'Hello'; }; }; $a()();



-- 
regards,

Kalle Sommer Nielsen
kalle@php.net

---

## #50614 — Stas Malyshev — 2010-11-27T00:29:58+00:00

URL: https://externals.io/message/50592#50614 · In reply to: #50613

Hi!

> huge +1 from me ;-) It might be worth adding function call chaining
> with dereferencing and instance method call chaning, like $a =
> function(){ return function(){ echo 'Hello'; }; }; $a()();

See: http://wiki.php.net/rfc/fcallfcall
-- 
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227

---

## #50615 — Pierre Joye — 2010-11-27T00:54:36+00:00

URL: https://externals.io/message/50592#50615 · In reply to: #50592

+1 :)


> Hi all,
> I'm here again to presents another proposal, which adds support for
> instantiating a class and calling its methods and accessing its properties
> on same command.
>
> Example:
>
> <?php
>
> class bar {
>  public $x = 'PHP';
> }
>
> class foo extends bar {
>  public function bar() {
>    return $this;
>  }
> }
>
> var_dump(new foo()->bar()->x); // string(3) "PHP"
>
> ?>
>
> Other examples which describes the feature at
> http://wiki.php.net/rfc/instance-method-call
>
> Thoughts?
>
> --
> Regards,
> Felipe Pena
>



-- 
Pierre

@pierrejoye | http://blog.thepimp.net | http://www.libgd.org

---

## #50617 — Peter Lind — 2010-11-27T05:28:46+00:00

URL: https://externals.io/message/50592#50617 · In reply to: #50609

> 
>
>
> It seems fairly handy and I've been in situations where I wanted to do
> something like that - in fact, I use factories to achieve something
> similar.
>  However, the more I use it, the more it feels like introducing code
> smells into my code. You're essentially instantiating an object only
> to immediately throw it away.
>
>
> Not necessarily; you could be calling the method for the collateral effects and that method return the object itself. This is not that uncommon.
>

And I can do that today with a factory pattern, if I want to.
Anyway, I don't want to argue against the feature, as it will just
introduce a slightly shorter version of something we can already do.

-- 
<hype>
WWW: plphp.dk / plind.dk
LinkedIn: plind
BeWelcome/Couchsurfing: Fake51
Twitter: kafe15
</hype

---

## #50625 — Felipe Pena — 2010-11-27T17:08:30+00:00

URL: https://externals.io/message/50592#50625 · In reply to: #50600

Hi,

2010/11/26 Felipe Pena <felipensp@gmail.com>

> 2010/11/26 Johannes Schlüter <johannes@schlueters.de>
>
> 
> > […]
>
> new foo()->bar() should be read as: (new foo())->bar().
>
> And using variable:
>
> new $bar->y()->x should be read as: (new ($bar->y)())->x.
>
> <?php
>
> class foo {
> public $x = 1;
> }
>
> class bar {
> public $y = 'foo';
> }
>
> $bar = new bar;
>
> var_dump(new $bar->y()->x); // 1
>
>  ?>
>
> I.e. just as it is nowdays.
>


Well, if this feature is going to be accept, we could to decide what syntax
to use.
I have created another patch which is the bracketed version of this
presented here.

<?php

class foo {
public $x = 1;
}

class bar {
public $y = 'foo';
}

$x = 'bar';

$bar = new bar;

var_dump((new bar)->y); // foo
var_dump((new $x)->y); // foo
var_dump((new $bar->y)->x); // 1

?>

Thus we do not have the readability issues, as pointed by Johannes.

http://wiki.php.net/rfc/instance-method-call (updated!)


Thanks for the comments.

-- 
Regards,
Felipe Pena

---

## #50678 — Adam Harvey — 2010-11-29T03:27:39+00:00

URL: https://externals.io/message/50592#50678 · In reply to: #50592

> I'm here again to presents another proposal, which adds support for
> instantiating a class and calling its methods and accessing its properties
> on same command.
>
> Thoughts?

Great work, Felipe! +1 for the feature; my very weak preference would
be for the explicitly bracketed version, but I'd be happy with either.

Adam

---

## #50682 — Dmitry Stogov — 2010-11-29T07:52:26+00:00

URL: https://externals.io/message/50592#50682 · In reply to: #50592

Hi Felipe,

I'm wondered it works out of the box with so small patches :)

However, both patches introduce new parser conflicts and it would be 
grate to avoid them.

Also the patches need to be checked for memory leaks in case of 
exceptions thrown from constructor and chained function(s).

It also probably makes sense to add array deference chaining e.g. new 
Foo()[] (just for language consistency).

Thanks. Dmitry.


> Hi all,
> I'm here again to presents another proposal, which adds support for
> instantiating a class and calling its methods and accessing its properties
> on same command.
>
> Example:
>
> <?php
>
> class bar {
>    public $x = 'PHP';
> }
>
> class foo extends bar {
>    public function bar() {
>      return $this;
>    }
> }
>
> var_dump(new foo()->bar()->x); // string(3) "PHP"
>
> ?>
>
> Other examples which describes the feature at
> http://wiki.php.net/rfc/instance-method-call
>
> Thoughts?

---

## #50687 — Felipe Pena — 2010-11-29T11:40:36+00:00

URL: https://externals.io/message/50592#50687 · In reply to: #50682

Hi Dmitry,

2010/11/29 Dmitry Stogov <dmitry@zend.com>

>  Hi Felipe,


> I'm wondered it works out of the box with so small patches :)
>
> However, both patches introduce new parser conflicts and it would be grate
> to avoid them.
>
>
I will check if there is any way to avoid it.


> Also the patches need to be checked for memory leaks in case of exceptions
> thrown from constructor and chained function(s).
>

Yes, I already did several tests to check this.


> It also probably makes sense to add array deference chaining e.g. new
> Foo()[] (just for language consistency).
>
>
Hmm, looks good to me. :)


Thanks.

-- 
Regards,
Felipe Pena

---

## #50695 — Julien Pauli — 2010-11-29T14:59:09+00:00

URL: https://externals.io/message/50592#50695 · In reply to: #50687

Care should be taken in the case of new myClass()->foo() just creates
an object to call a method but a static method would be more efficient
here.

However, well used (fluent interface for exemple) make me think +1 for
that patch.

J.Pauli


> Hi Dmitry,
>
> 2010/11/29 Dmitry Stogov <dmitry@zend.com>
>
> > […]
>
>
> > […]
> I will check if there is any way to avoid it.
>
>
> > […]
>
> Yes, I already did several tests to check this.
>
>
> > […]
> Hmm, looks good to me. :)
>
>
> Thanks.
>
> --
> Regards,
> Felipe Pena

---

Next page: https://externals.io/message/50592.md?page=2
