emehrkay Posted August 21, 2007 Share Posted August 21, 2007 pretty damn cool. makes me want to rewrite half of my code <?php class test{ public $_str; public function __construct(){ $this->x()->y(); } public function x(){ $this->_str .= 'x'; return $this; } public function y(){ $this->_str .= ' y'; return $this; } } $n = new test(); echo $n->_str; ?> Quote Link to comment https://forums.phpfreaks.com/topic/65902-i-was-not-aware-that-you-can-chain-methods-in-php/ Share on other sites More sharing options...
neylitalo Posted August 21, 2007 Share Posted August 21, 2007 Oh, definitely. You can even do something like this: <?php $object->method()->property->method(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/65902-i-was-not-aware-that-you-can-chain-methods-in-php/#findComment-329625 Share on other sites More sharing options...
redbullmarky Posted August 21, 2007 Share Posted August 21, 2007 i think it's only PHP5, isn't it? Quote Link to comment https://forums.phpfreaks.com/topic/65902-i-was-not-aware-that-you-can-chain-methods-in-php/#findComment-329848 Share on other sites More sharing options...
emehrkay Posted August 21, 2007 Author Share Posted August 21, 2007 Oh, definitely. You can even do something like this: <?php $object->method()->property->method(); ?> very handy, makes me rethink oop in php Quote Link to comment https://forums.phpfreaks.com/topic/65902-i-was-not-aware-that-you-can-chain-methods-in-php/#findComment-329916 Share on other sites More sharing options...
neylitalo Posted August 21, 2007 Share Posted August 21, 2007 i think it's only PHP5, isn't it? You may very well be right, I don't have much PHP 4 experience. Quote Link to comment https://forums.phpfreaks.com/topic/65902-i-was-not-aware-that-you-can-chain-methods-in-php/#findComment-330090 Share on other sites More sharing options...
roopurt18 Posted August 21, 2007 Share Posted August 21, 2007 I'm only saying this for the newer OOP programmers, be very careful when using this kind of syntax. You must be certain that the functions you are chaining return the proper object types or you will run into problems! Quote Link to comment https://forums.phpfreaks.com/topic/65902-i-was-not-aware-that-you-can-chain-methods-in-php/#findComment-330091 Share on other sites More sharing options...
emehrkay Posted August 22, 2007 Author Share Posted August 22, 2007 does it work for static? <?php class test{ public $_str; public static function x(){ $self->_str .= 'x'; return $self; } public static function y(){ $self->_str .= ' y'; return $self; } } $x = test::x()->y(); echo $x->_str; ?> Quote Link to comment https://forums.phpfreaks.com/topic/65902-i-was-not-aware-that-you-can-chain-methods-in-php/#findComment-330531 Share on other sites More sharing options...
neylitalo Posted August 22, 2007 Share Posted August 22, 2007 I rather doubt it, particularly in the way it's designed in the code you wrote. I'm sure you can chain static method calls, but not a mixture of static and non-static method calls in the same object/class. roopurt18's warning applies in this situation - you're expecting different datatypes than you're getting. The problems I see with it: 1) You can't reference or modify non-static properties from within a static method. 2) I don't think $self is what you're looking for - you use $this from within instantiated objects, and the keyword "self" from within static elements. 3) When you use test::x()->y(), you're assuming that an object is returned from test::x(), which isn't possible until test is instantiated. Quote Link to comment https://forums.phpfreaks.com/topic/65902-i-was-not-aware-that-you-can-chain-methods-in-php/#findComment-330634 Share on other sites More sharing options...
Azu Posted August 22, 2007 Share Posted August 22, 2007 Can somebody please explain to me the differance between this OOP chain thing and procedural nesting of functions? Quote Link to comment https://forums.phpfreaks.com/topic/65902-i-was-not-aware-that-you-can-chain-methods-in-php/#findComment-330775 Share on other sites More sharing options...
emehrkay Posted August 22, 2007 Author Share Posted August 22, 2007 I rather doubt it, particularly in the way it's designed in the code you wrote. I'm sure you can chain static method calls, but not a mixture of static and non-static method calls in the same object/class. roopurt18's warning applies in this situation - you're expecting different datatypes than you're getting. The problems I see with it: 1) You can't reference or modify non-static properties from within a static method. 2) I don't think $self is what you're looking for - you use $this from within instantiated objects, and the keyword "self" from within static elements. 3) When you use test::x()->y(), you're assuming that an object is returned from test::x(), which isn't possible until test is instantiated. 1. i thought that all public properties/methods were considered static, if not i could change it to "public static $_str;" 2&3. so returning $self isnt returning an object (i cant test it right now)? can someone run $x = test::x(); echo is_object($x); on the last class that i posted to see what is returned? or better yet, var_dump($x) Quote Link to comment https://forums.phpfreaks.com/topic/65902-i-was-not-aware-that-you-can-chain-methods-in-php/#findComment-330896 Share on other sites More sharing options...
roopurt18 Posted August 22, 2007 Share Posted August 22, 2007 Can somebody please explain to me the differance between this OOP chain thing and procedural nesting of functions? It's pretty simple. Let's say you have a class Manager that can return an instance of your DB object: <?php $db = Manager->getDB(); $db->query($sql); ?> Since you know that the getDB method of Manager will return a DB object, you don't really need the $db variable. <?php Manager->getDB()->query($sql); // Due to operator associativity, the above is executed as if it were: // (Manager->getDB())->query($sql) // The (Manager->getDB()) is representative of the $db var in the // first example ?> You see this syntax more often in Javascript, Java, and C++. For example, if you wanted the body element in Javascript: document.getElementsByTagName("body")[0]->appendChild(someNode); I'm not sure about the implications in Java or Javascript, but if you were going to reuse the returned object multiple times I'd just store it in a variable. Using the first example again: <?php $db = Manager->getDB(); $db->query($sql); echo $db->successMsg(); ?> I consider that a little more efficient (as far as PHP is concerned) than: <?php Manager->getDB()->query($sql); echo Manager->getDB()->successMsg(); ?> The reason is that in the second example here we have to call Manager->getDB twice to get the same value; this is slightly inefficient, but will still run really fast. However, in a language like C++ you don't have this problem. You can return a const reference to the object (yes I'm aware you can return references in PHP) and you can also declare the function as inline. Hope that helps to clarify! Quote Link to comment https://forums.phpfreaks.com/topic/65902-i-was-not-aware-that-you-can-chain-methods-in-php/#findComment-331029 Share on other sites More sharing options...
neylitalo Posted August 22, 2007 Share Posted August 22, 2007 1. i thought that all public properties/methods were considered static, if not i could change it to "public static $_str;" 2&3. so returning $self isnt returning an object (i cant test it right now)? can someone run $x = test::x(); echo is_object($x); on the last class that i posted to see what is returned? or better yet, var_dump($x) Properties/methods in a class are, by default, non-static and require instantiation to be usable. Otherwise the 'static' declaration would be absolutely useless, no? And I was mistaken - test::x() does indeed return an object, but it isn't an object of type test. Rather, it's an object of type "stdClass", which I'm assuming is the default "generic" class that PHP uses when dealing with static class members. object(stdClass)#1 (1) { ["_str"]=> string(1) "x" } Quote Link to comment https://forums.phpfreaks.com/topic/65902-i-was-not-aware-that-you-can-chain-methods-in-php/#findComment-331144 Share on other sites More sharing options...
emehrkay Posted August 22, 2007 Author Share Posted August 22, 2007 thanks neylitalo. I thought they they defaulted static and only threw a warning if you were in strict mode. I thought that the static was introduced so that you know how to code it the correct way, while the incorrect[legacy] way was still available. because you can call methods statically without the static declaration (we do it all the time at work, well they do it, i know better). and from your explanation I now see that it returns the stdClass instance and not an instance of test. cool stuff Quote Link to comment https://forums.phpfreaks.com/topic/65902-i-was-not-aware-that-you-can-chain-methods-in-php/#findComment-331209 Share on other sites More sharing options...
448191 Posted August 22, 2007 Share Posted August 22, 2007 Without trying to sound condescending, this is pretty basic stuff.. Quote Link to comment https://forums.phpfreaks.com/topic/65902-i-was-not-aware-that-you-can-chain-methods-in-php/#findComment-331330 Share on other sites More sharing options...
emehrkay Posted August 22, 2007 Author Share Posted August 22, 2007 Without trying to sound condescending, this is pretty basic stuff.. It is, I use it ALL the time in my js programming for chaining events/effects. However, In all of my php research and reading and coding, I've never seen it mentioned. I just never thought about it and I do not have a background in any fundamental languages where these things are the norm. Quote Link to comment https://forums.phpfreaks.com/topic/65902-i-was-not-aware-that-you-can-chain-methods-in-php/#findComment-331409 Share on other sites More sharing options...
Azu Posted August 23, 2007 Share Posted August 23, 2007 Can somebody please explain to me the differance between this OOP chain thing and procedural nesting of functions? It's pretty simple. Let's say you have a class Manager that can return an instance of your DB object: <?php $db = Manager->getDB(); $db->query($sql); ?> Since you know that the getDB method of Manager will return a DB object, you don't really need the $db variable. <?php Manager->getDB()->query($sql); // Due to operator associativity, the above is executed as if it were: // (Manager->getDB())->query($sql) // The (Manager->getDB()) is representative of the $db var in the // first example ?> You see this syntax more often in Javascript, Java, and C++. For example, if you wanted the body element in Javascript: document.getElementsByTagName("body")[0]->appendChild(someNode); I'm not sure about the implications in Java or Javascript, but if you were going to reuse the returned object multiple times I'd just store it in a variable. Using the first example again: <?php $db = Manager->getDB(); $db->query($sql); echo $db->successMsg(); ?> I consider that a little more efficient (as far as PHP is concerned) than: <?php Manager->getDB()->query($sql); echo Manager->getDB()->successMsg(); ?> The reason is that in the second example here we have to call Manager->getDB twice to get the same value; this is slightly inefficient, but will still run really fast. However, in a language like C++ you don't have this problem. You can return a const reference to the object (yes I'm aware you can return references in PHP) and you can also declare the function as inline. Hope that helps to clarify! Thanks but I don't understand sorry. How exactly is this different from nesting functions in procedural? Like echo(mysql_result(mysql_query("select text from table"),0)); Quote Link to comment https://forums.phpfreaks.com/topic/65902-i-was-not-aware-that-you-can-chain-methods-in-php/#findComment-332335 Share on other sites More sharing options...
448191 Posted August 23, 2007 Share Posted August 23, 2007 Using return values as arguments is conceptually and functionally different from object chaining. Quote Link to comment https://forums.phpfreaks.com/topic/65902-i-was-not-aware-that-you-can-chain-methods-in-php/#findComment-332340 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.