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
+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-callThoughts?
--
Regards,
Felipe Pena
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-callThoughts?
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/
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-callThoughts?
I fully support this patch. This is something PHP has needed for a long
time.
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
2010/11/26 Johannes Schlüter johannes@schlueters.de
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
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
Hi,
2010/11/26 Felipe Pena felipensp@gmail.com
2010/11/26 Johannes Schlüter johannes@schlueters.de
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
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
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-callThoughts?
--
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
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-callThoughts?
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
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-callThoughts?
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.
- 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
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-callThoughts?
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.
If it's a one-off, then I really don't see the problem. If you're
facing it again, write a facade.
- 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
On Fri, Nov 26, 2010 at 9:25 PM, Peter Lind peter.e.lind@gmail.com
wrote: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-callThoughts?
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.If it's a one-off, then I really don't see the problem. If you're
facing it again, write a facade.
- 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.
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
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
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
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
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-callThoughts?
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
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-callThoughts?
--
Regards,
Felipe Pena
Felipe, you're on a roll :) It's great!
Adam
--
Nephtali: PHP web framework that functions beautifully
http://nephtaliproject.com
+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-callThoughts?
--
Regards,
Felipe Pena
Hi Felipe
2010/11/26 Felipe Pena felipensp@gmail.com:
Other examples which describes the feature at
http://wiki.php.net/rfc/instance-method-callThoughts?
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
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
+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-callThoughts?
--
Regards,
Felipe Pena
--
Pierre
@pierrejoye | http://blog.thepimp.net | http://www.libgd.org
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
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-callThoughts?
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
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
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
2010/11/29 Felipe Pena felipensp@gmail.com
It also probably makes sense to add array deference chaining e.g. new
Foo()[] (just for language consistency).
Hmm, looks good to me. :)
I've updated the patch with the bracketed version to include the array
dereferecing support:
http://felipe.ath.cx/diff/instance-method-call-3.patch
<?php
class foo extends ArrayObject {
public function __construct($arr) {
parent::__construct($arr);
}
}
$arr = array(1, 2, 3);
$value = (new foo($arr))[1]; // int(2)
?>
Some tests: http://felipe.ath.cx/diff/instance_direct_access_001.phpt
--
Regards,
Felipe Pena
Hi all,
...
Other examples which describes the feature at
http://wiki.php.net/rfc/instance-method-callThoughts?
+1
Finally.
Regards,
Mike
Hi all,
...
Other examples which describes the feature at
http://wiki.php.net/rfc/instance-method-callThoughts?
+1
Finally.
it seems that it didn't made it to the trunk/5.4.
is there anything holding things back? I think Dmitry mentioned
checking for memleaks and such, but I didn't remember seeing anybody
really against the idea.
--
Ferenc Kovács
@Tyr43l - http://tyrael.hu
Hi,
2011/10/7 Ferenc Kovacs tyra3l@gmail.com:
Hi all,
...
Other examples which describes the feature at
http://wiki.php.net/rfc/instance-method-callThoughts?
+1
Finally.
it seems that it didn't made it to the trunk/5.4.
is there anything holding things back? I think Dmitry mentioned
checking for memleaks and such, but I didn't remember seeing anybody
really against the idea.
There is no memleak from what I've tested. I only haven't a way to
prevent the 1 shift/reduce conflict added by the patch, which seems
harmless for our grammar though.
--
Regards,
Felipe Pena
Sent from my iPhone
在 2011-10-17,6:03,Felipe Pena felipensp@gmail.com 写道:
Hi,
2011/10/7 Ferenc Kovacs tyra3l@gmail.com:
Hi all,
...
Other examples which describes the feature at
http://wiki.php.net/rfc/instance-method-callThoughts?
+1
Finally.
it seems that it didn't made it to the trunk/5.4.
is there anything holding things back? I think Dmitry mentioned
checking for memleaks and such, but I didn't remember seeing anybody
really against the idea.There is no memleak from what I've tested. I only haven't a way to
prevent the 1 shift/reduce conflict
yes,that was the problem I encountered too. If we set "%expect 3 " and
let bison deal with it, it could works as expect. But obvious not the
best solution. :)
added by the patch, which seems
harmless for our grammar though.--
Regards,
Felipe Pena
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-callThoughts?
Bump.
What's the current status on this?
It would be nice to this teeny little patch in for 5.4.0 if possible.
--
Regards,
Felipe Pena
Hi!
Other examples which describes the feature at
http://wiki.php.net/rfc/instance-method-callThoughts?
Bump.
What's the current status on this?
It would be nice to this teeny little patch in for 5.4.0 if possible.
I think the brackets one is fine, if all the tests are OK we can have it
in. But I'd like to get it before RC, after RC I don't want to have any
substantial changes.
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227
+1 for this
there is a little worried about that, is changing %%expect okey? (of
course I am not talking about that rewrite all the rules then fix the
r/s conflicts)
thanks
Hi!
Other examples which describes the feature at
http://wiki.php.net/rfc/instance-method-callThoughts?
Bump.
What's the current status on this?
It would be nice to this teeny little patch in for 5.4.0 if possible.
I think the brackets one is fine, if all the tests are OK we can have it in.
But I'd like to get it before RC, after RC I don't want to have any
substantial changes.Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227--
--
Laruence Xinchen Hui
http://www.laruence.com/
2011/11/5 Stas Malyshev smalyshev@sugarcrm.com:
Hi!
Other examples which describes the feature at
http://wiki.php.net/rfc/instance-method-callThoughts?
Bump.
What's the current status on this?
It would be nice to this teeny little patch in for 5.4.0 if possible.
I think the brackets one is fine, if all the tests are OK we can have it in.
But I'd like to get it before RC, after RC I don't want to have any
substantial changes.Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227
Committed!
--
Regards,
Felipe Pena