Re: [PHP] How can I do this -- method chaining
Nathan Nobbe wrote: On Jan 29, 2008 7:27 PM, Stut [EMAIL PROTECTED] wrote: Personally I'd use a static method in this instance. thats what i recommended. If you need to create an instance of the class you can do so in the static method and that way it will get destroyed when the function is done. Otherwise the object scope is far larger than it needs to be, which IMHO is an unnecessary waste of resources and certainly less aesthetic. lost you on this part .. whether you create an instance in client code by calling new or encapsulate the call to new in a simple factory method there will still be only one instance of the class, and it will still be in scope once the method is finished executing, because all it does is return an instance of the class its a member of. maybe you mean something other than what i posted earlier when you say static method? You posted a singleton pattern. That means that from the moment you call the static method until the end of the script that object exists. That's probably fine for web-based scripts that don't run for long, but I live in a world where classes often get used in unexpected ways so I tend to write code that's efficient without relying on the environment it's running in to clean it up. This was your code... ?php class Test { public static function getInstance() { return new Test(); } public function doSomething() { echo __METHOD__ . PHP_EOL; } } Test::getInstance()-doSomething(); ? This would be my implementation... ?php class Test { public static function doSomething() { $o = new Test(); $o-_doSomething(); } protected function _doSomething() { // I'm assuming this method is fairly complex, and involves // more than just this method, otherwise there is no point // in creating an instance of the class, just use a static // method. } } Test::doSomething(); ? Of course this is just based on what the OP said they wanted to do. If there is no reason to create an instance of the object then don't do it. It's fairly likely that I'd actually just use a static method here, but it depends on what it's actually doing. But as I said earlier, each to their own. -Stut -- http://stut.net/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How can I do this -- method chaining
Stut schreef: Nathan Nobbe wrote: On Jan 29, 2008 7:27 PM, Stut [EMAIL PROTECTED] wrote: Personally I'd use a static method in this instance. thats what i recommended. If you need to create an instance of the class you can do so in the static method and that way it will get destroyed when the function is done. Otherwise the object scope is far larger than it needs to be, which IMHO is an unnecessary waste of resources and certainly less aesthetic. lost you on this part .. whether you create an instance in client code by calling new or encapsulate the call to new in a simple factory method there will still be only one instance of the class, and it will still be in scope once the method is finished executing, because all it does is return an instance of the class its a member of. maybe you mean something other than what i posted earlier when you say static method? You posted a singleton pattern. huh? the OPs getInstance() method returns a new object on each call, hardly a singleton is it? That means that from the moment you call the static method until the end of the script that object exists. That's probably fine for web-based scripts that don't run for long, but I live in a world where classes often get used in unexpected ways so I tend to write code that's efficient without relying on the environment it's running in to clean it up. are you saying that the OPs getInstance() method causes each new instance to hang around inside memory because php doesn't know that it's no longer referenced, even when it's used like so: Test::getInstance()-doSomething(); and that your alternative does allow php to clean up the memory? This was your code... ?php class Test { public static function getInstance() { return new Test(); } public function doSomething() { echo __METHOD__ . PHP_EOL; } } Test::getInstance()-doSomething(); ? This would be my implementation... ?php class Test { public static function doSomething() { $o = new Test(); $o-_doSomething(); } protected function _doSomething() { // I'm assuming this method is fairly complex, and involves // more than just this method, otherwise there is no point // in creating an instance of the class, just use a static // method. } } Test::doSomething(); ? Of course this is just based on what the OP said they wanted to do. If there is no reason to create an instance of the object then don't do it. It's fairly likely that I'd actually just use a static method here, but it depends on what it's actually doing. But as I said earlier, each to their own. -Stut -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How can I do this -- method chaining
Jochem Maas wrote: Stut schreef: Nathan Nobbe wrote: On Jan 29, 2008 7:27 PM, Stut [EMAIL PROTECTED] wrote: Personally I'd use a static method in this instance. thats what i recommended. If you need to create an instance of the class you can do so in the static method and that way it will get destroyed when the function is done. Otherwise the object scope is far larger than it needs to be, which IMHO is an unnecessary waste of resources and certainly less aesthetic. lost you on this part .. whether you create an instance in client code by calling new or encapsulate the call to new in a simple factory method there will still be only one instance of the class, and it will still be in scope once the method is finished executing, because all it does is return an instance of the class its a member of. maybe you mean something other than what i posted earlier when you say static method? You posted a singleton pattern. huh? the OPs getInstance() method returns a new object on each call, hardly a singleton is it? Quite right too. Didn't read it properly. That means that from the moment you call the static method until the end of the script that object exists. That's probably fine for web-based scripts that don't run for long, but I live in a world where classes often get used in unexpected ways so I tend to write code that's efficient without relying on the environment it's running in to clean it up. are you saying that the OPs getInstance() method causes each new instance to hang around inside memory because php doesn't know that it's no longer referenced, even when it's used like so: Test::getInstance()-doSomething(); and that your alternative does allow php to clean up the memory? I could be wrong, I don't know the internals of PHP well enough to be definitive, but I'd rather err on the side of caution than write leaky code. -Stut -- http://stut.net/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How can I do this -- method chaining
Stut schreef: Jochem Maas wrote: Stut schreef: Nathan Nobbe wrote: On Jan 29, 2008 7:27 PM, Stut [EMAIL PROTECTED] wrote: Personally I'd use a static method in this instance. thats what i recommended. If you need to create an instance of the class you can do so in the static method and that way it will get destroyed when the function is done. Otherwise the object scope is far larger than it needs to be, which IMHO is an unnecessary waste of resources and certainly less aesthetic. lost you on this part .. whether you create an instance in client code by calling new or encapsulate the call to new in a simple factory method there will still be only one instance of the class, and it will still be in scope once the method is finished executing, because all it does is return an instance of the class its a member of. maybe you mean something other than what i posted earlier when you say static method? You posted a singleton pattern. huh? the OPs getInstance() method returns a new object on each call, hardly a singleton is it? Quite right too. Didn't read it properly. That means that from the moment you call the static method until the end of the script that object exists. That's probably fine for web-based scripts that don't run for long, but I live in a world where classes often get used in unexpected ways so I tend to write code that's efficient without relying on the environment it's running in to clean it up. are you saying that the OPs getInstance() method causes each new instance to hang around inside memory because php doesn't know that it's no longer referenced, even when it's used like so: Test::getInstance()-doSomething(); and that your alternative does allow php to clean up the memory? I could be wrong, I don't know the internals of PHP well enough to be definitive, but I'd rather err on the side of caution than write leaky code. the way I understand garbage collection as it is right now is that pretty much nothing is cleaned up until the end of the request but that php should be able to see that the ref count is zero in both cases either way. IIUC the yet to be released garbage collection improvements will potentially find/destroy unused zvals sooner (as well as being better in sorting out defunct circular references etc) but that the garbage collection itself uses a certain ammount of cpu cycles and in short running scripts (e.g. most of what we write for the web) it's likely to be better to let php just destroy memory at the end of the request. that said your more cautious approach cannot hurt :-) PS - my apologies if the memory related terminology I've used is somewhat bogus - please put it down to my lack of proper understanding :-/ -Stut -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How can I do this -- method chaining
Nathan Nobbe wrote: Actually, I don't think so. I believe constructors return void, while the 'new' keyword returns a copy of the object. im pretty sure constructors return an object instance: php class Test { function __construct() {} } php var_dump(new Test()); object(Test)#1 (0) { } AFAIK, constructor simply constructs the object, and *new* is the one that binds the reference to the variable on the lhs. So, constructors return nothing. but anyway, how could you even test that __construct() returned void and the new keyword returned a copy of the object? new essentially invokes __construct() and passes along its return value, near as i can tell. Christoph, if you dont want to write a function in the global namespace, as suggested in the article, Eric posted, just add a simple factory method in your class, eg. ?php class Test { public static function getInstance() { return new Test(); } public function doSomething() { echo __METHOD__ . PHP_EOL; } } Test::getInstance()-doSomething(); ? -nathan -- Regards, Anup Shukla -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How can I do this -- method chaining
Anup Shukla schreef: Nathan Nobbe wrote: Actually, I don't think so. I believe constructors return void, while the 'new' keyword returns a copy of the object. im pretty sure constructors return an object instance: php class Test { function __construct() {} } php var_dump(new Test()); object(Test)#1 (0) { } AFAIK, constructor simply constructs the object, and *new* is the one that binds the reference to the variable on the lhs. not exactly - 'new' asks php to initialize an object of the given class, the 'binding' to a variable occurs because of the assignment operator. the __construct() method is called automatically by php after the object structure has been initialized, so primarily nothing is returned because the call to __construct() doesn't happen directly in userland code. at least that's how I understand it. So, constructors return nothing. but anyway, how could you even test that __construct() returned void and the new keyword returned a copy of the object? new essentially invokes __construct() and passes along its return value, near as i can tell. Christoph, if you dont want to write a function in the global namespace, as suggested in the article, Eric posted, just add a simple factory method in your class, eg. ?php class Test { public static function getInstance() { return new Test(); } public function doSomething() { echo __METHOD__ . PHP_EOL; } } Test::getInstance()-doSomething(); ? -nathan -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How can I do this -- method chaining
On Jan 30, 2008 5:56 AM, Stut [EMAIL PROTECTED] wrote: Nathan Nobbe wrote: You posted a singleton pattern. no, what i posted was a simple factory pattern. if you invoke it twice there will be 2 instances of Test in memory, eg. not singleton. $a = Test::getInstance(); $b = Test::getInstance(); That means that from the moment you call the static method until the end of the script that object exists. That's probably fine for web-based scripts that don't run for long, but I live in a world where classes often get used in unexpected ways so I tend to write code that's efficient without relying on the environment it's running in to clean it up. i usually only need to do cleanup in cli scripts that batch large amounts of data. this is my practical experience anyway. This would be my implementation... ?php class Test { public static function doSomething() { $o = new Test(); $o-_doSomething(); } protected function _doSomething() { // I'm assuming this method is fairly complex, and involves // more than just this method, otherwise there is no point // in creating an instance of the class, just use a static // method. } } Test::doSomething(); ? Of course this is just based on what the OP said they wanted to do. If there is no reason to create an instance of the object then don't do it. well you are still creating an instance of the object. the only difference is you are forcing it the ref count to 0 by assigning the instance to a local local variable. It's fairly likely that I'd actually just use a static method here, both your and my code use static methods. it sounds to me like you are using the term 'static method' to mean a static method that has a variable with a reference to an instance of the class that it is a member of. which is obviously a particular use of a static method, and therefore a bad practice imho. not the technique, mind you, the label of 'static method' for the technique. -nathan
Re: [PHP] How can I do this -- method chaining
Nathan Nobbe wrote: On Jan 30, 2008 10:46 AM, Stut [EMAIL PROTECTED] mailto:[EMAIL PROTECTED] wrote: Nathan Nobbe wrote: Actually no, I mean I would *just* use a static method. If there is no reason to instantiate an object, why would you? http://stut.net/ you realize you are instantiating an class in the code you posted, right? from you post: $o = new Test(); if i didnt know any better, id call that an instantiation of the Test class ;) the only thing is you are forcing it out of scope by using a local variable to store the reference to the object. Seriously? You really need to read the emails you're replying to. I gave an example that did what the OP asked for. Then I went on to say that I would probably just use a static method. I never said I wasn't creating an instance in the example I posted. The forcing it out of scope was the crux of my point. However, if Jochem is right then it's kinda pointless with the current implementation of the GC, but may become relevant in the new GC. -Stut -- http://stut.net/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How can I do this -- method chaining
On Jan 30, 2008 10:46 AM, Stut [EMAIL PROTECTED] wrote: Nathan Nobbe wrote: Actually no, I mean I would *just* use a static method. If there is no reason to instantiate an object, why would you? http://stut.net/ you realize you are instantiating an class in the code you posted, right? from you post: $o = new Test(); if i didnt know any better, id call that an instantiation of the Test class ;) the only thing is you are forcing it out of scope by using a local variable to store the reference to the object. -nathan
Re: [PHP] How can I do this -- method chaining
Nathan Nobbe wrote: It's fairly likely that I'd actually just use a static method here, both your and my code use static methods. it sounds to me like you are using the term 'static method' to mean a static method that has a variable with a reference to an instance of the class that it is a member of. which is obviously a particular use of a static method, and therefore a bad practice imho. not the technique, mind you, the label of 'static method' for the technique. Actually no, I mean I would *just* use a static method. If there is no reason to instantiate an object, why would you? -Stut -- http://stut.net/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How can I do this -- method chaining
Stut wrote: Nathan Nobbe wrote: On Jan 30, 2008 10:53 AM, Stut [EMAIL PROTECTED] mailto:[EMAIL PROTECTED] wrote: Nathan Nobbe wrote: I never said I wasn't creating an instance in the example I posted. then what exactly did you mean by this? Actually no, I mean I would *just* use a static method. If there is no reason to instantiate an object, why would you? I meant I would *just* use a static method. Calling a static method does not create an instance of the class. My comments usually follow the rule of Ronseal. What do you think I meant by it? -Stut From your previous email -- Jim Lucas Some men are born to greatness, some achieve greatness, and some have greatness thrust upon them. Twelfth Night, Act II, Scene V by William Shakespeare -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How can I do this -- method chaining
Nathan Nobbe wrote: On Jan 30, 2008 10:53 AM, Stut [EMAIL PROTECTED] mailto:[EMAIL PROTECTED] wrote: Nathan Nobbe wrote: I never said I wasn't creating an instance in the example I posted. then what exactly did you mean by this? Actually no, I mean I would *just* use a static method. If there is no reason to instantiate an object, why would you? I meant I would *just* use a static method. Calling a static method does not create an instance of the class. My comments usually follow the rule of Ronseal. What do you think I meant by it? -Stut -- http://stut.net/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How can I do this -- method chaining
On Jan 30, 2008 10:53 AM, Stut [EMAIL PROTECTED] wrote: Nathan Nobbe wrote: I never said I wasn't creating an instance in the example I posted. then what exactly did you mean by this? Actually no, I mean I would *just* use a static method. If there is no reason to instantiate an object, why would you? -nathan
Re: [PHP] How can I do this -- method chaining
Stut wrote: Nathan Nobbe wrote: On Jan 30, 2008 10:53 AM, Stut [EMAIL PROTECTED] mailto:[EMAIL PROTECTED] wrote: Nathan Nobbe wrote: I never said I wasn't creating an instance in the example I posted. then what exactly did you mean by this? Actually no, I mean I would *just* use a static method. If there is no reason to instantiate an object, why would you? I meant I would *just* use a static method. Calling a static method does not create an instance of the class. My comments usually follow the rule of Ronseal. What do you think I meant by it? -Stut From your previous email ?php class Test { public static function doSomething() { $o = new Test(); The above line IS creating a instance of the class Test Now with proper garbage collection, it should be wiped out when you are done using the static doSomething() method. $o-_doSomething(); You could always include this to remove the instance of class Test unset($o); } protected function _doSomething() { // I'm assuming this method is fairly complex, and involves // more than just this method, otherwise there is no point // in creating an instance of the class, just use a static // method. } } Test::doSomething(); ? -- Jim Lucas Some men are born to greatness, some achieve greatness, and some have greatness thrust upon them. Twelfth Night, Act II, Scene V by William Shakespeare -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How can I do this -- method chaining
On Jan 30, 2008 11:21 AM, Stut [EMAIL PROTECTED] wrote: Calling a static method does not create an instance of the class. there you go again; calling a static method does create an instance of the class if you call new inside of it :P -nathan
Re: [PHP] How can I do this -- method chaining
Jim Lucas wrote: Stut wrote: Nathan Nobbe wrote: On Jan 30, 2008 10:53 AM, Stut [EMAIL PROTECTED] mailto:[EMAIL PROTECTED] wrote: Nathan Nobbe wrote: I never said I wasn't creating an instance in the example I posted. then what exactly did you mean by this? Actually no, I mean I would *just* use a static method. If there is no reason to instantiate an object, why would you? I meant I would *just* use a static method. Calling a static method does not create an instance of the class. My comments usually follow the rule of Ronseal. What do you think I meant by it? -Stut From your previous email ?php class Test { public static function doSomething() { $o = new Test(); The above line IS creating a instance of the class Test Now with proper garbage collection, it should be wiped out when you are done using the static doSomething() method. $o-_doSomething(); You could always include this to remove the instance of class Test unset($o); } protected function _doSomething() { // I'm assuming this method is fairly complex, and involves // more than just this method, otherwise there is no point // in creating an instance of the class, just use a static // method. } } Test::doSomething(); ? I would *just* use a static method *just* *just* *just* *just* *just* *just* *just* *just* *just* No instance. None. Grrr. FFS. -Stut -- http://stut.net/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How can I do this -- method chaining
On Jan 30, 2008 11:31 AM, Stut [EMAIL PROTECTED] wrote: I would *just* use a static method *just* *just* *just* *just* *just* *just* *just* *just* *just* No instance. None. Grrr. here is a mod of the code you posted w/ a var_dump() of the local variable $o; ?php class Test { public static function doSomething() { $o = new Test(); var_dump($o); $o-_doSomething(); } protected function _doSomething() { // I'm assuming this method is fairly complex, and involves // more than just this method, otherwise there is no point // in creating an instance of the class, just use a static // method. } } Test::doSomething(); ? [EMAIL PROTECTED] ~/ticketsDbCode $ php testCode.php object(Test)#1 (0) { } clearly in the act of *just* using a static method, you *just* created an instance of class Test ;) -nathan
Re: [PHP] How can I do this -- method chaining
Nathan Nobbe wrote: On Jan 30, 2008 11:21 AM, Stut [EMAIL PROTECTED] mailto:[EMAIL PROTECTED] wrote: Calling a static method does not create an instance of the class. there you go again; calling a static method does create an instance of the class if you call new inside of it :P FFS, are you just trolling? Note that I actually wrote *just* a static method, and I stated quite clearly that it was what I would do and that the code I posted was providing what the OP wanted. If you can't see the separation then I'm done with trying to convince you. -Stut -- http://stut.net/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How can I do this -- method chaining
Nathan Nobbe wrote: On Jan 30, 2008 11:31 AM, Stut [EMAIL PROTECTED] mailto:[EMAIL PROTECTED] wrote: I would *just* use a static method *just* *just* *just* *just* *just* *just* *just* *just* *just* No instance. None. Grrr. here is a mod of the code you posted w/ a var_dump() of the local variable $o; ?php class Test { public static function doSomething() { $o = new Test(); var_dump($o); $o-_doSomething(); } protected function _doSomething() { // I'm assuming this method is fairly complex, and involves // more than just this method, otherwise there is no point // in creating an instance of the class, just use a static // method. } } Test::doSomething(); ? [EMAIL PROTECTED] ~/ticketsDbCode $ php testCode.php object(Test)#1 (0) { } clearly in the act of *just* using a static method, you *just* created an instance of class Test ;) Ok, I'm going to have to assume you really are as stupid as you seem. If I need to provide an example to demonstrate what I meant I will, but I feel I made it quite clear that my comment regarding what *I* would do did not in any way relate to the code example I had provided above. The example I provided was fulfilling the OP's requirements. This is what *I* would do... ?php class Test { public static function doSomething() { // I'm assuming this method is fairly complex, and involves // more than just this method, otherwise there is no point // in creating an instance of the class, just use a static // method. // See this comment here, this was taken from the // non-static method in the example I posted. This is what // I meant when I say just use a static method. } } Test::doSomething(); ? Look ma, no instance. FYI I'm not at all new to OOP, in general or in PHP, so I am well aware that the example I originally posted created an instance of the class. -Stut -- http://stut.net/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How can I do this -- method chaining
On Jan 30, 2008 11:58 AM, Stut [EMAIL PROTECTED] wrote: Ok, I'm going to have to assume you really are as stupid as you seem. If I need to provide an example to demonstrate what I meant I will, but I feel I made it quite clear that my comment regarding what *I* would do did not in any way relate to the code example I had provided above. The example I provided was fulfilling the OP's requirements. This is what *I* would do... ?php class Test { public static function doSomething() { // I'm assuming this method is fairly complex, and involves // more than just this method, otherwise there is no point // in creating an instance of the class, just use a static // method. // See this comment here, this was taken from the // non-static method in the example I posted. This is what // I meant when I say just use a static method. } } Test::doSomething(); ? Look ma, no instance. well, at least its clear now, what you meant. FYI I'm not at all new to OOP, in general or in PHP, so I am well aware that the example I originally posted created an instance of the class. glad to hear it; no hard feelings i hope.. -nathan
Re: [PHP] How can I do this -- method chaining
Stut wrote: Nathan Nobbe wrote: On Jan 30, 2008 11:31 AM, Stut [EMAIL PROTECTED] mailto:[EMAIL PROTECTED] wrote: I would *just* use a static method *just* *just* *just* *just* *just* *just* *just* *just* *just* No instance. None. Grrr. here is a mod of the code you posted w/ a var_dump() of the local variable $o; ?php class Test { public static function doSomething() { $o = new Test(); var_dump($o); $o-_doSomething(); } protected function _doSomething() { // I'm assuming this method is fairly complex, and involves // more than just this method, otherwise there is no point // in creating an instance of the class, just use a static // method. } } Test::doSomething(); ? [EMAIL PROTECTED] ~/ticketsDbCode $ php testCode.php object(Test)#1 (0) { } clearly in the act of *just* using a static method, you *just* created an instance of class Test ;) Ok, I'm going to have to assume you really are as stupid as you seem. If I need to provide an example to demonstrate what I meant I will, but I feel I made it quite clear that my comment regarding what *I* would do did not in any way relate to the code example I had provided above. The example I provided was fulfilling the OP's requirements. This is what *I* would do... ?php class Test { public static function doSomething() { // I'm assuming this method is fairly complex, and involves // more than just this method, otherwise there is no point // in creating an instance of the class, just use a static // method. // See this comment here, this was taken from the // non-static method in the example I posted. This is what // I meant when I say just use a static method. } } Test::doSomething(); ? Look ma, no instance. Now this is clear. But to point out in the code I quoted, you said that you were going to only use the static method, but you were calling the static method that created an instance of the Test class and then calling the non-static method from the instance of the Test class. Your previous example was not showing us what you were saying. To me it looked like you were confused about how you were calling/creating things. -- Jim Lucas Some men are born to greatness, some achieve greatness, and some have greatness thrust upon them. Twelfth Night, Act II, Scene V by William Shakespeare -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How can I do this -- method chaining
Nathan Nobbe wrote: On Jan 30, 2008 11:58 AM, Stut [EMAIL PROTECTED] mailto:[EMAIL PROTECTED] wrote: Ok, I'm going to have to assume you really are as stupid as you seem. If I need to provide an example to demonstrate what I meant I will, but I feel I made it quite clear that my comment regarding what *I* would do did not in any way relate to the code example I had provided above. The example I provided was fulfilling the OP's requirements. This is what *I* would do... ?php class Test { public static function doSomething() { // I'm assuming this method is fairly complex, and involves // more than just this method, otherwise there is no point // in creating an instance of the class, just use a static // method. // See this comment here, this was taken from the // non-static method in the example I posted. This is what // I meant when I say just use a static method. } } Test::doSomething(); ? Look ma, no instance. well, at least its clear now, what you meant. FYI I'm not at all new to OOP, in general or in PHP, so I am well aware that the example I originally posted created an instance of the class. glad to hear it; no hard feelings i hope.. Indeed. Now, the place where you sleep... is it guarded? ;) -Stut -- http://stut.net/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How can I do this -- method chaining
Indeed. Now, the place where you sleep... is it guarded? well it is, but.. i probly misunderstood some implication in the directions of my virtual fortress and therefore, probly not as well a i suspect ;) -nathan
Re: [PHP] How can I do this -- method chaining
Jim Lucas wrote: Stut wrote: Nathan Nobbe wrote: On Jan 30, 2008 11:31 AM, Stut [EMAIL PROTECTED] mailto:[EMAIL PROTECTED] wrote: I would *just* use a static method *just* *just* *just* *just* *just* *just* *just* *just* *just* No instance. None. Grrr. here is a mod of the code you posted w/ a var_dump() of the local variable $o; ?php class Test { public static function doSomething() { $o = new Test(); var_dump($o); $o-_doSomething(); } protected function _doSomething() { // I'm assuming this method is fairly complex, and involves // more than just this method, otherwise there is no point // in creating an instance of the class, just use a static // method. } } Test::doSomething(); ? [EMAIL PROTECTED] ~/ticketsDbCode $ php testCode.php object(Test)#1 (0) { } clearly in the act of *just* using a static method, you *just* created an instance of class Test ;) Ok, I'm going to have to assume you really are as stupid as you seem. If I need to provide an example to demonstrate what I meant I will, but I feel I made it quite clear that my comment regarding what *I* would do did not in any way relate to the code example I had provided above. The example I provided was fulfilling the OP's requirements. This is what *I* would do... ?php class Test { public static function doSomething() { // I'm assuming this method is fairly complex, and involves // more than just this method, otherwise there is no point // in creating an instance of the class, just use a static // method. // See this comment here, this was taken from the // non-static method in the example I posted. This is what // I meant when I say just use a static method. } } Test::doSomething(); ? Look ma, no instance. Now this is clear. Glad to hear it. But to point out in the code I quoted, you said that you were going to only use the static method, but you were calling the static method that created an instance of the Test class and then calling the non-static method from the instance of the Test class. I thought the comment in that static method explained that I didn't see the point in creating the instance. I thought it was pretty clear, but clearly not. Your previous example was not showing us what you were saying. To me it looked like you were confused about how you were calling/creating things. I was never confused! ;) -Stut -- http://stut.net/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How can I do this -- method chaining
On Wed, January 30, 2008 9:53 am, Stut wrote: The forcing it out of scope was the crux of my point. However, if Jochem is right then it's kinda pointless with the current implementation of the GC, but may become relevant in the new GC. I dunno about the OOP instances getting GC'ed, but PHP *definitely* reclaims memory from arrays and strings as they go out of scope, usually. You can work at something complicated enough to confuse it that it won't reclaim it, but you have to work at it to get there. -- Some people have a gift link here. Know what I want? I want you to buy a CD from some indie artist. http://cdbaby.com/from/lynch Yeah, I get a buck. So? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How can I do this -- method chaining
I dunno about the OOP instances getting GC'ed, but PHP *definitely* reclaims memory from arrays and strings as they go out of scope, usually. Does anyone else find that funny? :) It definitely does it ... usually ;) -- Postgresql php tutorials http://www.designmagick.com/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How can I do this -- method chaining
I believe the constructor returns the object created, with no chance in userland code of altering that fact, over-riding the return value, or any other jiggery-pokery to that effect. New causes the constructor to be called in the first place, and that's about it. The assignment to a variable is done by the assignment operator = and is not required if you don't have any need to actually keep the object around in a variable. -- Some people have a gift link here. Know what I want? I want you to buy a CD from some indie artist. http://cdbaby.com/from/lynch Yeah, I get a buck. So? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How can I do this -- method chaining
On Wed, January 30, 2008 6:19 pm, Chris wrote: I dunno about the OOP instances getting GC'ed, but PHP *definitely* reclaims memory from arrays and strings as they go out of scope, usually. Does anyone else find that funny? :) It definitely does it ... usually ;) Ah well. It definitely does it when it can, but you can confuse it with enough circular references and large enough data structures that it ends up with a giant mess of data it can't GC, because it's just not THAT smart. There are improvements coming in this area, I think, in PHP 6, if I remember the posts to internals correctly. -- Some people have a gift link here. Know what I want? I want you to buy a CD from some indie artist. http://cdbaby.com/from/lynch Yeah, I get a buck. So? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How can I do this -- method chaining
Richard Lynch wrote: On Wed, January 30, 2008 6:19 pm, Chris wrote: I dunno about the OOP instances getting GC'ed, but PHP *definitely* reclaims memory from arrays and strings as they go out of scope, usually. Does anyone else find that funny? :) It definitely does it ... usually ;) Ah well. I was just picking on the phrasing, nothing else :) It definitely does it when it can, but you can confuse it with enough circular references and large enough data structures that it ends up with a giant mess of data it can't GC, because it's just not THAT smart. Yep, they suck pretty hard and can be pretty hard to unravel at times but that's another topic altogether. There are improvements coming in this area, I think, in PHP 6, if I remember the posts to internals correctly. Awesome :) -- Postgresql php tutorials http://www.designmagick.com/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How can I do this -- method chaining
Richard Lynch schreef: I believe the constructor returns the object created, with no chance in userland code of altering that fact, over-riding the return value, or any other jiggery-pokery to that effect. New causes the constructor to be called in the first place, and that's about it. The assignment to a variable is done by the assignment operator = and is not required if you don't have any need to actually keep the object around in a variable. I thought that's what I said. maybe less clearly :-) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How can I do this -- method chaining
On Jan 30, 2008 4:53 PM, Jochem Maas [EMAIL PROTECTED] wrote: Richard Lynch schreef: I believe the constructor returns the object created, with no chance in userland code of altering that fact, over-riding the return value, or any other jiggery-pokery to that effect. New causes the constructor to be called in the first place, and that's about it. The assignment to a variable is done by the assignment operator = and is not required if you don't have any need to actually keep the object around in a variable. I thought that's what I said. maybe less clearly :-) I don't think constructors return the object: ?php class foo { private $bar; public function __construct($bar) { echo In constructor\n; $this-bar = $bar; } } $x = new foo(...); var_dump($x-__construct()); # NULL ? -- -Casey -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How can I do this -- method chaining
On Jan 30, 2008 11:29 PM, Casey [EMAIL PROTECTED] wrote: I don't think constructors return the object: im starting to think this as well. what for example happens when there is not __construct() method ? class Test { private $blah = ''; } here $blah is part of a new instance, before __construct() would get called, if it was defined. this makes me think php creates an internal object, and optionally lets __construct() alter it if defined. i think somebody may have already said that, but i didnt feel like wading through the previous posts for it.. -nathan -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How can I do this -- method chaining
Nathan Nobbe schreef: On Jan 29, 2008 3:02 PM, Stut [EMAIL PROTECTED] wrote: Why? What exactly do you think you're saving by not putting the instance in a variable? I can't think of one good reason to do this. its an esthetic thing; and besides the simple factory method is an easy workaround to achieve it. as the article that, Eric, posted mentioned, other languages have such support; ie javascript: function Test() {} Test.prototype = { doSomething : function() { alert('hello'); } } ^^ prototypal not class-based inheritance, orange meet apple. new Test().doSomething(); besides which this is a dereferenced call and not method chaining, if you want method chaining in JS you'll have to do extra work (i.e. use 'return this;') different strokes or something. this is along the lines of the whole returnAnArray()['someIndex'] thing, fortunately in this case, theres a workaround in userspace ;) -nathan -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How can I do this -- method chaining
On Jan 29, 2008 3:26 PM, Jochem Maas [EMAIL PROTECTED] wrote: Nathan Nobbe schreef: On Jan 29, 2008 3:02 PM, Stut [EMAIL PROTECTED] wrote: Why? What exactly do you think you're saving by not putting the instance in a variable? I can't think of one good reason to do this. its an esthetic thing; and besides the simple factory method is an easy workaround to achieve it. as the article that, Eric, posted mentioned, other languages have such support; ie javascript: function Test() {} Test.prototype = { doSomething : function() { alert('hello'); } } ^^ prototypal not class-based inheritance, orange meet apple. i never said it was :P besides which this is a dereferenced call and not method chaining, if you want method chaining in JS you'll have to do extra work (i.e. use 'return this;') different strokes or something. this example was to illustrate that in other languages, such as javascript, an object method can be invoked directly from an instance returned by a call to new (which is really what were talking about in this thread). whereas, in php, as weve seen today, youll have to implement a workaround. -nathan
Re: [PHP] How can I do this -- method chaining
On Jan 29, 2008 1:53 PM, Christoph Boget [EMAIL PROTECTED] wrote: Constructors return the object, correct? If so, how can I do this: class Bob { private $blah; _construct( $blah ) { $this-blah = $blah; } public getBlah() { return $this-blah; } } echo Bob( 'Hello!' )-getBlah(); When I try that, I get the message Undefined function Bob. I've also tried echo new Bob( 'Hello!' )-getBlah(); echo (new Bob( 'Hello!' ))-getBlah(); but PHP didn't like either of those at all. Is it just not possible what I'm trying to do? I'm using PHP5.2.1 thnx, Chris -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php http://www.travisswicegood.com/index.php/2007/10/26/fluent_api_here_i_come -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How can I do this -- method chaining
On Jan 29, 2008 1:53 PM, Christoph Boget [EMAIL PROTECTED] wrote: Constructors return the object, correct? Actually, I don't think so. I believe constructors return void, while the 'new' keyword returns a copy of the object. Andrew -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How can I do this -- method chaining
On Tue, 2008-01-29 at 14:17 -0500, Eric Butera wrote: http://www.travisswicegood.com/index.php/2007/10/26/fluent_api_here_i_come Looks like a repurpose of one of my posts: http://fsiu.uwc.ac.za/index.php?module=blogaction=viewsinglepostid=gen9Srv59Nme5_7092_1182404204 --Paul All Email originating from UWC is covered by disclaimer http://www.uwc.ac.za/portal/public/portal_services/disclaimer.htm -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How can I do this -- method chaining
Christoph Boget wrote: Constructors return the object, correct? If so, how can I do this: class Bob { private $blah; _construct( $blah ) { $this-blah = $blah; } public getBlah() { return $this-blah; } } echo Bob( 'Hello!' )-getBlah(); When I try that, I get the message Undefined function Bob. I've also tried echo new Bob( 'Hello!' )-getBlah(); echo (new Bob( 'Hello!' ))-getBlah(); Bob is a class, not a method. You could try this: ?php $obj = new Bob(); $obj-getBlah(); ? It's not method chaining though. -- Richard Heyes http://www.websupportsolutions.co.uk Knowledge Base and Helpdesk software for £299pa hosted for you - no installation, no maintenance, new features automatic and free -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How can I do this -- method chaining
On Jan 29, 2008 2:27 PM, Andrew Ballard [EMAIL PROTECTED] wrote: On Jan 29, 2008 1:53 PM, Christoph Boget [EMAIL PROTECTED] wrote: Constructors return the object, correct? Actually, I don't think so. I believe constructors return void, while the 'new' keyword returns a copy of the object. im pretty sure constructors return an object instance: php class Test { function __construct() {} } php var_dump(new Test()); object(Test)#1 (0) { } but anyway, how could you even test that __construct() returned void and the new keyword returned a copy of the object? new essentially invokes __construct() and passes along its return value, near as i can tell. Christoph, if you dont want to write a function in the global namespace, as suggested in the article, Eric posted, just add a simple factory method in your class, eg. ?php class Test { public static function getInstance() { return new Test(); } public function doSomething() { echo __METHOD__ . PHP_EOL; } } Test::getInstance()-doSomething(); ? -nathan
Re: [PHP] How can I do this -- method chaining
On Jan 29, 2008 2:37 PM, Paul Scott [EMAIL PROTECTED] wrote: Looks like a repurpose of one of my posts: http://fsiu.uwc.ac.za/index.php?module=blogaction=viewsinglepostid=gen9Srv59Nme5_7092_1182404204 actually, this is slightly different; here we are talking about being able to immediately invoke a method off the call to the constructor, whereas in your post you chain calls after storing the instance in a variable in the call to the constructor. -nathan
Re: [PHP] How can I do this -- method chaining
On Jan 29, 2008 2:37 PM, Paul Scott [EMAIL PROTECTED] wrote: Looks like a repurpose of one of my posts: http://fsiu.uwc.ac.za/index.php?module=blogaction=viewsinglepostid=gen9Srv59Nme5_7092_1182404204 actually, this is slightly different; here we are talking about being able to immediately invoke a method off the call to the constructor, whereas in your post you chain calls after storing the instance in a variable in the call to the constructor. Right, and that's what I was trying to avoid, if possible. thnx, Chris Never miss a thing. Make Yahoo your home page. http://www.yahoo.com/r/hs -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How can I do this -- method chaining
Christoph Boget schreef: Constructors return the object, correct? If so, how can I do this: class Bob { private $blah; _construct( $blah ) { $this-blah = $blah; } public getBlah() { return $this-blah; } } echo Bob( 'Hello!' )-getBlah(); When I try that, I get the message Undefined function Bob. I've also tried echo new Bob( 'Hello!' )-getBlah(); echo (new Bob( 'Hello!' ))-getBlah(); but PHP didn't like either of those at all. Is it just not possible what I'm trying to do? class Foo { private $x; private function __construct($x) { $this-x = $x; } static function init($x) { return new self($x); } function double() { $this-x *= 2; return $this; } function triple() { $this-x *= 3; return $this; } function output() { echo $this-x, \n; } } Foo::init(2)-double()-triple()-output(); you can't chain of the constructor as Andrew explained. you may wish to return object clones to chain with as opposed to the same object - the example below is fairly bogus but it mgiht be helpful to you (btw run the code to see what it actually does as opposed to what you think it should do ... hey it caught me out and I wrote it!): class Foo2 { private $x; private function __construct($x) { $this-x = $x; } static function init($x) { return new self($x); } function double() { $this-x *= 2; return clone $this; } function triple() { $this-x *= 3; return clone $this; } function output() { echo $this-x, \n; } } $a = Foo2::init(2); $b = $a-double()-triple(); $a-output(); $b-output(); I'm using PHP5.2.1 thnx, Chris -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How can I do this -- method chaining
On 29 Jan 2008, at 19:43, Christoph Boget [EMAIL PROTECTED] wrote: On Jan 29, 2008 2:37 PM, Paul Scott [EMAIL PROTECTED] wrote: Looks like a repurpose of one of my posts: http://fsiu.uwc.ac.za/index.php?module=blogaction=viewsinglepostid=gen9Srv59Nme5_7092_1182404204 actually, this is slightly different; here we are talking about being able to immediately invoke a method off the call to the constructor, whereas in your post you chain calls after storing the instance in a variable in the call to the constructor. Right, and that's what I was trying to avoid, if possible. Why? What exactly do you think you're saving by not putting the instance in a variable? I can't think of one good reason to do this. -Stut -- http://stut.net/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How can I do this -- method chaining
On Jan 29, 2008 3:02 PM, Stut [EMAIL PROTECTED] wrote: Why? What exactly do you think you're saving by not putting the instance in a variable? I can't think of one good reason to do this. its an esthetic thing; and besides the simple factory method is an easy workaround to achieve it. as the article that, Eric, posted mentioned, other languages have such support; ie javascript: function Test() {} Test.prototype = { doSomething : function() { alert('hello'); } } new Test().doSomething(); this is along the lines of the whole returnAnArray()['someIndex'] thing, fortunately in this case, theres a workaround in userspace ;) -nathan
Re: [PHP] How can I do this -- method chaining
On 29 Jan 2008, at 20:08, Nathan Nobbe [EMAIL PROTECTED] wrote: On Jan 29, 2008 3:02 PM, Stut [EMAIL PROTECTED] wrote: Why? What exactly do you think you're saving by not putting the instance in a variable? I can't think of one good reason to do this. its an esthetic thing; and besides the simple factory method is an easy workaround to achieve it. as the article that, Eric, posted mentioned, other languages have such support; ie javascript: function Test() {} Test.prototype = { doSomething : function() { alert('hello'); } } new Test().doSomething(); this is along the lines of the whole returnAnArray()['someIndex'] thing, fortunately in this case, theres a workaround in userspace ;) I don't see how it's any more aesthetically pleasing, but each to their own I guess. Personally I'd use a static method in this instance. If you need to create an instance of the class you can do so in the static method and that way it will get destroyed when the function is done. Otherwise the object scope is far larger than it needs to be, which IMHO is an unnecessary waste of resources and certainly less aesthetic. -Stut -- http://stut.net/
Re: [PHP] How can I do this -- method chaining
On Jan 29, 2008 7:27 PM, Stut [EMAIL PROTECTED] wrote: Personally I'd use a static method in this instance. thats what i recommended. If you need to create an instance of the class you can do so in the static method and that way it will get destroyed when the function is done. Otherwise the object scope is far larger than it needs to be, which IMHO is an unnecessary waste of resources and certainly less aesthetic. lost you on this part .. whether you create an instance in client code by calling new or encapsulate the call to new in a simple factory method there will still be only one instance of the class, and it will still be in scope once the method is finished executing, because all it does is return an instance of the class its a member of. maybe you mean something other than what i posted earlier when you say static method? -nathan -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php