[PHP] Method chaining off constructors
Is there a reason why you can't do method chaining off of constructors? Consider the following class: class bob { public function __construct() { echo 'Constructor()'; } public function one() { echo '-one()'; return $this; } public function two() { echo '-two()'; return $this; } } This works: $bob = new bob(); $bob-one()-two(); whereas this doesn't. $bob = new bob()-one()-two(); Why? I thought constructors returned the object? thnx, Christoph -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Method chaining off constructors
-Original Message- From: Christoph Boget [mailto:[EMAIL PROTECTED] Sent: Friday, June 06, 2008 9:45 AM To: PHP General Subject: [PHP] Method chaining off constructors Is there a reason why you can't do method chaining off of constructors? Consider the following class: class bob { public function __construct() { echo 'Constructor()'; } public function one() { echo '-one()'; return $this; } public function two() { echo '-two()'; return $this; } } This works: $bob = new bob(); $bob-one()-two(); whereas this doesn't. $bob = new bob()-one()-two(); Why? I thought constructors returned the object? It's been a while since I've played with objects in PHP, but couldn't you just add the line: return $this; ...to the end of your __construct() function? Sorry if this is obtuse of me to say, I just thought maybe the answer was that simple and you're like I am--you've been staring at a tree for so long, racking your brain, that you forget about the forest altogether. :) Todd Boyd Web Programmer -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Method chaining off constructors
Why? I thought constructors returned the object? It's been a while since I've played with objects in PHP, but couldn't you just add the line: return $this; ...to the end of your __construct() function? Sorry if this is obtuse of me to say, I just thought maybe the answer was that simple and you're like I am--you've been staring at a tree for so long, racking your brain, that you forget about the forest altogether. :) The constructor should already be returning $this. thnx, Christoph -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Method chaining off constructors
On Fri, Jun 6, 2008 at 11:00 AM, Christoph Boget [EMAIL PROTECTED] wrote: Why? I thought constructors returned the object? It's been a while since I've played with objects in PHP, but couldn't you just add the line: return $this; ...to the end of your __construct() function? Sorry if this is obtuse of me to say, I just thought maybe the answer was that simple and you're like I am--you've been staring at a tree for so long, racking your brain, that you forget about the forest altogether. :) The constructor should already be returning $this. thnx, Christoph -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php If you want to do this you need to define a function/method that returns an instance for you. So you can say class bob { public static function getInstance() { return new bob(); } public function foo() { } } bob::getInstance()-foo(); I've seen on the internals list where core dev's said it doesn't make sense to chain off a constructor. If you want this behavior then you need to do it off a method. This is just how things work is all it boils down to. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Method chaining off constructors
The 'new' keyword has to apply to the object created in the constructor (and not the return value of any of the follow-up calls.) To establish this precedence, chaining wasn't allowed on constructors. On Fri, Jun 6, 2008 at 11:04 AM, Eric Butera [EMAIL PROTECTED] wrote: On Fri, Jun 6, 2008 at 11:00 AM, Christoph Boget [EMAIL PROTECTED] wrote: Why? I thought constructors returned the object? It's been a while since I've played with objects in PHP, but couldn't you just add the line: return $this; ...to the end of your __construct() function? Sorry if this is obtuse of me to say, I just thought maybe the answer was that simple and you're like I am--you've been staring at a tree for so long, racking your brain, that you forget about the forest altogether. :) The constructor should already be returning $this. thnx, Christoph -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php If you want to do this you need to define a function/method that returns an instance for you. So you can say class bob { public static function getInstance() { return new bob(); } public function foo() { } } bob::getInstance()-foo(); I've seen on the internals list where core dev's said it doesn't make sense to chain off a constructor. If you want this behavior then you need to do it off a method. This is just how things work is all it boils down to. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Method chaining off constructors
The 'new' keyword has to apply to the object created in the constructor (and not the return value of any of the follow-up calls.) To establish this precedence, chaining wasn't allowed on constructors. If precedence was the issue, why doesn't this work, either: (new bob())-one()-two() ? Or, rather, why couldn't that have been taken into consideration? thnx, Christoph -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Method chaining off constructors
On Fri, 2008-06-06 at 11:35 -0400, Christoph Boget wrote: The 'new' keyword has to apply to the object created in the constructor (and not the return value of any of the follow-up calls.) To establish this precedence, chaining wasn't allowed on constructors. If precedence was the issue, why doesn't this work, either: (new bob())-one()-two() ? Or, rather, why couldn't that have been taken into consideration? It seems a bit futile asking on PHP General when this a question for the PHP Internals list. That is where these kinds of decisions are made. Cheers, Rob. -- http://www.interjinn.com Application and Templating Framework for PHP -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] method chaining
Hello, I have a question about php and OO. I want to chain methods together having them performed on the results of the following method. Meaning something like $foo-get(bar)-getBaz(). I get the following error from the script bellow. Parse error: parse error, unexpected T_OBJECT_OPERATOR, expecting ',' or ';' in /usr/local/apache/php/testchain.php on line 47 Anybody know why I can't do this? Anyway to make it it one statement instead of $tmp = $foo-get(bar); $tmp-getBaz(); -- lame --? Is their a config setting or something? Thanks in advance, Terry ?php class Foo { var $vector = array(); function Foo() {} function put($name, $value) { $this-vector[$name] = $value; return $this-vector[$name]; } function get($name) { return $this-vector[$name]; } } class Bar { var $baz = ; function Bar() {} function setBaz($value) { $this-baz = $value; return $this-baz; } function getBaz() { return $this-baz; } } $bar = new Bar(); $bar-setBaz(test succeeded); $foo = new Foo(); $foo-put(bar, $bar); echo $foo-get(bar)-getBaz(), br; ? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] method chaining
At 16:53 11.11.2002, Terry McBride spoke out and said: [snip] Hello, I have a question about php and OO. I want to chain methods together having them performed on the results of the following method. Meaning something like $foo-get(bar)-getBaz(). I get the following error from the script bellow. Parse error: parse error, unexpected T_OBJECT_OPERATOR, expecting ',' or ';' in /usr/local/apache/php/testchain.php on line 47 Anybody know why I can't do this? Anyway to make it it one statement instead of $tmp = $foo-get(bar); $tmp-getBaz(); -- lame --? Is their a config setting or something? [snip] This is simply not supported in PHP... -- O Ernest E. Vogelsinger (\) ICQ #13394035 ^ http://www.vogelsinger.at/
Re: [PHP] method chaining
work around: $obj = $foo-get(bar); $obj-getBaz(); it is a better way as you don't have to re-repeat the function code and re-pass the oblect. -- Maxim Maletsky [EMAIL PROTECTED] Terry McBride [EMAIL PROTECTED] wrote... : Hello, I have a question about php and OO. I want to chain methods together having them performed on the results of the following method. Meaning something like $foo-get(bar)-getBaz(). I get the following error from the script bellow. Parse error: parse error, unexpected T_OBJECT_OPERATOR, expecting ',' or ';' in /usr/local/apache/php/testchain.php on line 47 Anybody know why I can't do this? Anyway to make it it one statement instead of $tmp = $foo-get(bar); $tmp-getBaz(); -- lame --? Is their a config setting or something? Thanks in advance, Terry ?php class Foo { var $vector = array(); function Foo() {} function put($name, $value) { $this-vector[$name] = $value; return $this-vector[$name]; } function get($name) { return $this-vector[$name]; } } class Bar { var $baz = ; function Bar() {} function setBaz($value) { $this-baz = $value; return $this-baz; } function getBaz() { return $this-baz; } } $bar = new Bar(); $bar-setBaz(test succeeded); $foo = new Foo(); $foo-put(bar, $bar); echo $foo-get(bar)-getBaz(), br; ? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] method chaining
If I have understood correctly what you want to do then this will do fine... $foo-${$foo-get(bar)}-getBaz(); / Johan - Original Message - From: Terry McBride [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Monday, November 11, 2002 4:53 PM Subject: [PHP] method chaining Hello, I have a question about php and OO. I want to chain methods together having them performed on the results of the following method. Meaning something like $foo-get(bar)-getBaz(). I get the following error from the script bellow. Parse error: parse error, unexpected T_OBJECT_OPERATOR, expecting ',' or ';' in /usr/local/apache/php/testchain.php on line 47 Anybody know why I can't do this? Anyway to make it it one statement instead of $tmp = $foo-get(bar); $tmp-getBaz(); -- lame --? Is their a config setting or something? Thanks in advance, Terry ?php class Foo { var $vector = array(); function Foo() {} function put($name, $value) { $this-vector[$name] = $value; return $this-vector[$name]; } function get($name) { return $this-vector[$name]; } } class Bar { var $baz = ; function Bar() {} function setBaz($value) { $this-baz = $value; return $this-baz; } function getBaz() { return $this-baz; } } $bar = new Bar(); $bar-setBaz(test succeeded); $foo = new Foo(); $foo-put(bar, $bar); echo $foo-get(bar)-getBaz(), br; ? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] method chaining
At this point, I respectfully disagree. To say it's not supported is one thing but to say it's better or more efficient convincingly requires more. Tell me why :) Who's repeating/repassing? I'm under the impression it would be faster for the interpreter if the statement were chained, also a copy (memory) of the object would be not be required. Oops, I mis-spoke, actually a copy would be required due to the the referencing conventions - perhaps this is not useful anyway given what I really want is... $tmp = $foo-get(bar); // note the $tmp-alterBarSomeWay(); // $tmp-getBaz(); No way to do that in one line. I think the reference conventions are odd. I can pass a variable to a function not knowing it may be referenced and altered (unless I check the signature), but I have to know that what I'm receiving from a function is a reference in order to grab it properly!? I wish I knew why these decisions went the way the did. Terry - Original Message - From: Maxim Maletsky [EMAIL PROTECTED] To: Terry McBride [EMAIL PROTECTED] Cc: [EMAIL PROTECTED] Sent: Monday, November 11, 2002 11:20 AM Subject: Re: [PHP] method chaining work around: $obj = $foo-get(bar); $obj-getBaz(); it is a better way as you don't have to re-repeat the function code and re-pass the oblect. -- Maxim Maletsky [EMAIL PROTECTED] Terry McBride [EMAIL PROTECTED] wrote... : Hello, I have a question about php and OO. I want to chain methods together having them performed on the results of the following method. Meaning something like $foo-get(bar)-getBaz(). I get the following error from the script bellow. Parse error: parse error, unexpected T_OBJECT_OPERATOR, expecting ',' or ';' in /usr/local/apache/php/testchain.php on line 47 Anybody know why I can't do this? Anyway to make it it one statement instead of $tmp = $foo-get(bar); $tmp-getBaz(); -- lame --? Is their a config setting or something? Thanks in advance, Terry ?php class Foo { var $vector = array(); function Foo() {} function put($name, $value) { $this-vector[$name] = $value; return $this-vector[$name]; } function get($name) { return $this-vector[$name]; } } class Bar { var $baz = ; function Bar() {} function setBaz($value) { $this-baz = $value; return $this-baz; } function getBaz() { return $this-baz; } } $bar = new Bar(); $bar-setBaz(test succeeded); $foo = new Foo(); $foo-put(bar, $bar); echo $foo-get(bar)-getBaz(), br; ? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] method chaining
Thanks Johan, I tried your suggestion but got: Fatal error: Call to a member function on a non-object in /usr/local/apache/php/testchain.php on line 47 I think you might have me on the right track though :) I tried this as well: ${$foo-get(bar)}-getBaz(); but got the same error. Terry - Original Message - From: Johan Ohlin [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Monday, November 11, 2002 11:45 AM Subject: Re: [PHP] method chaining If I have understood correctly what you want to do then this will do fine... $foo-${$foo-get(bar)}-getBaz(); / Johan - Original Message - From: Terry McBride [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Monday, November 11, 2002 4:53 PM Subject: [PHP] method chaining Hello, I have a question about php and OO. I want to chain methods together having them performed on the results of the following method. Meaning something like $foo-get(bar)-getBaz(). I get the following error from the script bellow. Parse error: parse error, unexpected T_OBJECT_OPERATOR, expecting ',' or ';' in /usr/local/apache/php/testchain.php on line 47 Anybody know why I can't do this? Anyway to make it it one statement instead of $tmp = $foo-get(bar); $tmp-getBaz(); -- lame --? Is their a config setting or something? Thanks in advance, Terry ?php class Foo { var $vector = array(); function Foo() {} function put($name, $value) { $this-vector[$name] = $value; return $this-vector[$name]; } function get($name) { return $this-vector[$name]; } } class Bar { var $baz = ; function Bar() {} function setBaz($value) { $this-baz = $value; return $this-baz; } function getBaz() { return $this-baz; } } $bar = new Bar(); $bar-setBaz(test succeeded); $foo = new Foo(); $foo-put(bar, $bar); echo $foo-get(bar)-getBaz(), br; ? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] method chaining
Noted. You would have to change the signature of the function to function getBar(). The statement still applies - even part of the point. I'm really just wondering why things are the way they are. Maybe I'm thinking php is supposed to be more OO than it is. Who wants to deal with copies of objects so much that you have to specify if you want to work with the actual object (by referencing)? Why not $foo = clone($object) if you want a copy? Why? Why? Why? : p Perhaps I'm just hoping to plant seeds for future versions (yeah right!). T - Original Message - From: Ernest E Vogelsinger [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Monday, November 11, 2002 12:02 PM Subject: Re: [PHP] method chaining At 17:57 11.11.2002, Terry McBride spoke out and said: [snip] $tmp = $foo-get(bar); // note the $tmp-alterBarSomeWay(); // $tmp-getBaz(); No way to do that in one line. I think the reference conventions are odd. I can pass a variable to a function not knowing it may be referenced and altered (unless I check the signature), but I have to know that what I'm receiving from a function is a reference in order to grab it properly!? [snip] Note that both parties need to know the reference... assuming function foo::get($id) {return $something;} it will not help (as to my knowledge) when you say $tmp = $foo-get('baz'); The function must be coded as function foo::get($id) {return $something;} to return a reference. AND you have to accept the reference, or you'd get a copy. -- O Ernest E. Vogelsinger (\) ICQ #13394035 ^ http://www.vogelsinger.at/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] method chaining
-Original Message- From: Terry McBride [mailto:terry;boxitllc.com] Sent: 11 November 2002 15:53 I have a question about php and OO. I want to chain methods together having them performed on the results of the following method. Meaning something like $foo-get(bar)-getBaz(). I get the following error from the script bellow. Parse error: parse error, unexpected T_OBJECT_OPERATOR, expecting ',' or ';' in /usr/local/apache/php/testchain.php on line 47 Anybody know why I can't do this? Anyway to make it it one statement instead of $tmp = $foo-get(bar); $tmp-getBaz(); -- lame --? Is their a config setting or something? As already noted, this doesn't work currently -- however, as I understand it, Cheers! Mike - Mike Ford, Electronic Information Services Adviser, Learning Support Services, Learning Information Services, JG125, James Graham Building, Leeds Metropolitan University, Beckett Park, LEEDS, LS6 3QS, United Kingdom Email: [EMAIL PROTECTED] Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211 it does work in Zend Engine 2, and so should make an appearance in PHP 5. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] method chaining
Apologies for the previous truncated version of this -- dunno what happened there!! -Original Message- From: Terry McBride [mailto:terry;boxitllc.com] Sent: 11 November 2002 15:53 I have a question about php and OO. I want to chain methods together having them performed on the results of the following method. Meaning something like $foo-get(bar)-getBaz(). I get the following error from the script bellow. Parse error: parse error, unexpected T_OBJECT_OPERATOR, expecting ',' or ';' in /usr/local/apache/php/testchain.php on line 47 Anybody know why I can't do this? Anyway to make it it one statement instead of $tmp = $foo-get(bar); $tmp-getBaz(); -- lame --? Is their a config setting or something? As already noted, this doesn't work currently -- however, as I understand it, it does work in Zend Engine 2 and so should make an appearance in PHP 5. Cheers! Mike - Mike Ford, Electronic Information Services Adviser, Learning Support Services, Learning Information Services, JG125, James Graham Building, Leeds Metropolitan University, Beckett Park, LEEDS, LS6 3QS, United Kingdom Email: [EMAIL PROTECTED] Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211 it does work in Zend Engine 2, and so should make an appearance in PHP 5. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] method chaining
- Original Message - From: Ford, Mike [LSS] [EMAIL PROTECTED] To: 'Terry McBride' [EMAIL PROTECTED]; [EMAIL PROTECTED] Sent: Monday, November 11, 2002 1:33 PM Subject: RE: [PHP] method chaining Apologies for the previous truncated version of this -- dunno what happened there!! -Original Message- From: Terry McBride [mailto:terry;boxitllc.com] Sent: 11 November 2002 15:53 I have a question about php and OO. I want to chain methods together having them performed on the results of the following method. Meaning something like $foo-get(bar)-getBaz(). I get the following error from the script bellow. Parse error: parse error, unexpected T_OBJECT_OPERATOR, expecting ',' or ';' in /usr/local/apache/php/testchain.php on line 47 Anybody know why I can't do this? Anyway to make it it one statement instead of $tmp = $foo-get(bar); $tmp-getBaz(); -- lame --? Is their a config setting or something? As already noted, this doesn't work currently -- however, as I understand it, it does work in Zend Engine 2 and so should make an appearance in PHP 5. Cheers! Mike - Mike Ford, Electronic Information Services Adviser, Learning Support Services, Learning Information Services, JG125, James Graham Building, Leeds Metropolitan University, Beckett Park, LEEDS, LS6 3QS, United Kingdom Email: [EMAIL PROTECTED] Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211 it does work in Zend Engine 2, and so should make an appearance in PHP 5. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] method chaining
Thanks! Yep, this is good. Thanks for letting me know. http://www.zend.com/engine2/ZendEngine-2.0.pdf It's all been done b4 -- already been brought up :p Terry - Original Message - From: Ford, Mike [LSS] [EMAIL PROTECTED] To: 'Terry McBride' [EMAIL PROTECTED]; [EMAIL PROTECTED] Sent: Monday, November 11, 2002 1:33 PM Subject: RE: [PHP] method chaining Apologies for the previous truncated version of this -- dunno what happened there!! -Original Message- From: Terry McBride [mailto:terry;boxitllc.com] Sent: 11 November 2002 15:53 I have a question about php and OO. I want to chain methods together having them performed on the results of the following method. Meaning something like $foo-get(bar)-getBaz(). I get the following error from the script bellow. Parse error: parse error, unexpected T_OBJECT_OPERATOR, expecting ',' or ';' in /usr/local/apache/php/testchain.php on line 47 Anybody know why I can't do this? Anyway to make it it one statement instead of $tmp = $foo-get(bar); $tmp-getBaz(); -- lame --? Is their a config setting or something? As already noted, this doesn't work currently -- however, as I understand it, it does work in Zend Engine 2 and so should make an appearance in PHP 5. Cheers! Mike - Mike Ford, Electronic Information Services Adviser, Learning Support Services, Learning Information Services, JG125, James Graham Building, Leeds Metropolitan University, Beckett Park, LEEDS, LS6 3QS, United Kingdom Email: [EMAIL PROTECTED] Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211 it does work in Zend Engine 2, and so should make an appearance in PHP 5. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php