Re: [PHP] How can I do the opposite of property_exists(), maybe a create_property() in PHP5?
2009/2/3 Daevid Vincent dae...@daevid.com: On Wed, 2009-02-04 at 08:58 +1100, Chris wrote: the question is what is __set() doing, if it's throwing an exception for undefined properties then obviously it with 'blow up'. But why should __set() even be called if I'm accessing the property directly? This seems stupid. $this-oraclecustomerid = 1122; should NOT be the same as $this-set_oraclecustomerid(1122); The second one I agree should call __set(), but the first one should NOT be triggering __call() or __set() Yes it should. http://www.php.net/manual/en/language.oop5.overloading.php#language.oop5.overloading.members __set() is run when writing data to inaccessible members. if it's a protected/private variable, it'll call __set. If it's a variable that doesn't exist, it'll call __set. Let me rephrase that. I see now that it is designed that way, but I think the logic is erroneous. While I'm sure this argument/discussion is all for naught now, I believe that a straight assignment of a value to a variable, SHOULD NOT do any behind the scenes magic __set(). It should just do it. Otherwise, what's the point of being able to set a property/variable both ways? One gives no benefit over the other and as illustrated decreases flexibility. It appears it will work if I change my property to public, but I don't want them exposed like that. *sigh* Bottom line is there should be a create_property($name, $value = null, $type = 'protected') function/method that I can call to do what I'm trying to do. I assume unset($this-foo); works. So therefore, I can check for existence of a property, and consequently remove a property, but I cannot create a property. In PHP class properties must be defined before being used. The __set and __get magic methods allow you to arbitrarily create properties, and IMHO is the most flexible way to do it. If it just worked as you want it to you would not be able to create rules around what properties can be created or what values they can hold. I use those magic methods to implement validation in my model classes. If $this-x simply created x I would have to implement it using accessor functions which IMHO is much less intuitive. When deciding whether to call __set or __get PHP looks at whether you can access the property in the current context, so if you declare a private or protected property you should be able to set its value from within the class without involving the magic methods. Hope that makes things a bit clearer. -Stuart -- 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 the opposite of property_exists(), maybe a creat_property() in PHP5?
Edmund Hertle schreef: 2009/2/3 Daevid Vincent dae...@daevid.com Is there a way to create a new property via PHP 5.2.4? I get a hash back from an authentication server. I'm not guaranteed that someone in another department won't add new key/values to the returned hash/array. I'm trying to work around that part gracefully so that the code doesn't blow up on a customer in such an event. The main try/catch will suppress errors already, but I thought it would be nice to be able to handle this stuff automatically rather than constantly updating a User.class.php file all the time. creating new property this-oraclecustomerid with 1122 but when I try to set the value with the $this-$pkey = $value; It triggers __call() which then triggers __set() which throws my BadProperty exception. How come $this-$pkey = $value isn't creating/setting a property? Or how do I do something like create_property($this, $pkey); so that I can then set it via $this-oraclecustomerid = 1122 or $this-set_oraclecustomerid(1122) ??? ?php function load_from_user_data($user_data) { //now loop through the rest of the user_data array and assign via a set_foo() method foreach ($user_data as $key = $value) { //try { $pkey = strtolower($key); //[dv] this is sort of a hack to automatically create a new property/variable // for 'new' hashes key/values we may not know about. // It's really designed to supress errors and they really should be added to this User.class.php properly. if ( !property_exists($this, $pkey) ) { echo creating new property this-$pkey with $valuebr\n; $this-$pkey = $value; //THIS BLOWS UP ON THE __set() echo this-$pkey = .$this-$pkey; } Hey, well, $this-$pkey is wrong syntax. Try $this-pkey = $value there is nothing wrong with $this-$pkey. the question is what is __set() doing, if it's throwing an exception for undefined properties then obviously it with 'blow up'. I would suggest looking into using an array inside the object to store all user data, you can still use some setter methods for user fields that are known at compile time and for the rest you just stuff the extra/unknown fields into the array, something like: class Test { private $data = array(); function loadUserData($data) { foreach ($data as $key = $val) { if (method_exists($this, 'set_'.$key) $this-{'set_'.$key}($val); else $this-data[ $key ] = $val; } } } -eddy else { $class_variable = 'set_'.$pkey; $this-$class_variable($value); unset($user_data[$key]); } } //catch (Exception $e) { //echo $e-getMessage().\n; } } //should new fields be returned in the $user_data that are not accounted for above... if ($_SESSION['DEVELOPMENT'] count($user_data)) { echo !-- Unaccounted for user_data hashes. Please add these into User.class.php:\n; var_dump($user_data); echo --; } //THESE TWO LINES FATAL ERROR ON THE __get(): echo this-oraclecustomerid = .$this-oraclecustomerid; echo this-get_oraclecustomerid() = .$this-get_oraclecustomerid(); } ? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How can I do the opposite of property_exists(), maybe a creat_property() in PHP5?
On Tue, 2009-02-03 at 12:51 +0100, Jochem Maas wrote: Edmund Hertle schreef: 2009/2/3 Daevid Vincent dae...@daevid.com Is there a way to create a new property via PHP 5.2.4? I get a hash back from an authentication server. I'm not guaranteed that someone in another department won't add new key/values to the returned hash/array. I'm trying to work around that part gracefully so that the code doesn't blow up on a customer in such an event. The main try/catch will suppress errors already, but I thought it would be nice to be able to handle this stuff automatically rather than constantly updating a User.class.php file all the time. creating new property this-oraclecustomerid with 1122 but when I try to set the value with the $this-$pkey = $value; It triggers __call() which then triggers __set() which throws my BadProperty exception. How come $this-$pkey = $value isn't creating/setting a property? Or how do I do something like create_property($this, $pkey); so that I can then set it via $this-oraclecustomerid = 1122 or $this-set_oraclecustomerid(1122) ??? ?php function load_from_user_data($user_data) { //now loop through the rest of the user_data array and assign via a set_foo() method foreach ($user_data as $key = $value) { //try { $pkey = strtolower($key); //[dv] this is sort of a hack to automatically create a new property/variable // for 'new' hashes key/values we may not know about. // It's really designed to supress errors and they really should be added to this User.class.php properly. if ( !property_exists($this, $pkey) ) { echo creating new property this-$pkey with $valuebr\n; $this-$pkey = $value; //THIS BLOWS UP ON THE __set() echo this-$pkey = .$this-$pkey; } the question is what is __set() doing, if it's throwing an exception for undefined properties then obviously it with 'blow up'. But why should __set() even be called if I'm accessing the property directly? This seems stupid. $this-oraclecustomerid = 1122; should NOT be the same as $this-set_oraclecustomerid(1122); The second one I agree should call __set(), but the first one should NOT be triggering __call() or __set() else { $class_variable = 'set_'.$pkey; $this-$class_variable($value); unset($user_data[$key]); } } //catch (Exception $e) { //echo $e-getMessage().\n; } } //should new fields be returned in the $user_data that are not accounted for above... if ($_SESSION['DEVELOPMENT'] count($user_data)) { echo !-- Unaccounted for user_data hashes. Please add these into User.class.php:\n; var_dump($user_data); echo --; } //THESE TWO LINES FATAL ERROR ON THE __get(): echo this-oraclecustomerid = .$this-oraclecustomerid; echo this-get_oraclecustomerid() = .$this-get_oraclecustomerid(); } ?
Re: [PHP] How can I do the opposite of property_exists(), maybe a create_property() in PHP5?
On Tue, Feb 3, 2009 at 4:19 PM, Daevid Vincent dae...@daevid.com wrote: On Wed, 2009-02-04 at 08:58 +1100, Chris wrote: the question is what is __set() doing, if it's throwing an exception for undefined properties then obviously it with 'blow up'. But why should __set() even be called if I'm accessing the property directly? This seems stupid. $this-oraclecustomerid = 1122; should NOT be the same as $this-set_oraclecustomerid(1122); The second one I agree should call __set(), but the first one should NOT be triggering __call() or __set() Yes it should. http://www.php.net/manual/en/language.oop5.overloading.php#language.oop5.overloading.members __set() is run when writing data to inaccessible members. if it's a protected/private variable, it'll call __set. If it's a variable that doesn't exist, it'll call __set. Let me rephrase that. I see now that it is designed that way, but I think the logic is erroneous. While I'm sure this argument/discussion is all for naught now, I believe that a straight assignment of a value to a variable, SHOULD NOT do any behind the scenes magic __set(). It should just do it. Otherwise, what's the point of being able to set a property/variable both ways? One gives no benefit over the other and as illustrated decreases flexibility. It appears it will work if I change my property to public, but I don't want them exposed like that. *sigh* Bottom line is there should be a create_property($name, $value = null, $type = 'protected') function/method that I can call to do what I'm trying to do. I assume unset($this-foo); works. So therefore, I can check for existence of a property, and consequently remove a property, but I cannot create a property. wow, obviously you can create properties at runtime. if you want direct access to property assignment, dont define __set() for that class. if you want to override this assignment, then define __set() for that class, pretty simple.. and property creation / assignment is essentially the same thing, since all properties must store a value. when you 'create' a property in php w/o explicitly giving it a value the default value is NULL. i recommend that if you want to keep __set() defined in this class you mentioned, and not have the melt-down b/c you have some check to see if the property exists, you can just define another method, createOrSet($property, $value), something to that effect, which will ignore the step about verifying the property already exists. -nathan
Re: [PHP] How can I do the opposite of property_exists(), maybe a creat_property() in PHP5?
the question is what is __set() doing, if it's throwing an exception for undefined properties then obviously it with 'blow up'. But why should __set() even be called if I'm accessing the property directly? This seems stupid. $this-oraclecustomerid = 1122; should NOT be the same as $this-set_oraclecustomerid(1122); The second one I agree should call __set(), but the first one should NOT be triggering __call() or __set() Yes it should. http://www.php.net/manual/en/language.oop5.overloading.php#language.oop5.overloading.members __set() is run when writing data to inaccessible members. if it's a protected/private variable, it'll call __set. If it's a variable that doesn't exist, it'll call __set. -- 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 the opposite of property_exists(), maybe a create_property() in PHP5?
On Wed, 2009-02-04 at 08:58 +1100, Chris wrote: the question is what is __set() doing, if it's throwing an exception for undefined properties then obviously it with 'blow up'. But why should __set() even be called if I'm accessing the property directly? This seems stupid. $this-oraclecustomerid = 1122; should NOT be the same as $this-set_oraclecustomerid(1122); The second one I agree should call __set(), but the first one should NOT be triggering __call() or __set() Yes it should. http://www.php.net/manual/en/language.oop5.overloading.php#language.oop5.overloading.members __set() is run when writing data to inaccessible members. if it's a protected/private variable, it'll call __set. If it's a variable that doesn't exist, it'll call __set. Let me rephrase that. I see now that it is designed that way, but I think the logic is erroneous. While I'm sure this argument/discussion is all for naught now, I believe that a straight assignment of a value to a variable, SHOULD NOT do any behind the scenes magic __set(). It should just do it. Otherwise, what's the point of being able to set a property/variable both ways? One gives no benefit over the other and as illustrated decreases flexibility. It appears it will work if I change my property to public, but I don't want them exposed like that. *sigh* Bottom line is there should be a create_property($name, $value = null, $type = 'protected') function/method that I can call to do what I'm trying to do. I assume unset($this-foo); works. So therefore, I can check for existence of a property, and consequently remove a property, but I cannot create a property.
Re: [PHP] How can I do the opposite of property_exists(), maybe a creat_property() in PHP5?
On Tue, 2009-02-03 at 08:30 +0100, Edmund Hertle wrote: 2009/2/3 Daevid Vincent dae...@daevid.com Is there a way to create a new property via PHP 5.2.4? I get a hash back from an authentication server. I'm not guaranteed that someone in another department won't add new key/values to the returned hash/array. I'm trying to work around that part gracefully so that the code doesn't blow up on a customer in such an event. The main try/catch will suppress errors already, but I thought it would be nice to be able to handle this stuff automatically rather than constantly updating a User.class.php file all the time. creating new property this-oraclecustomerid with 1122 but when I try to set the value with the $this-$pkey = $value; It triggers __call() which then triggers __set() which throws my BadProperty exception. How come $this-$pkey = $value isn't creating/setting a property? Or how do I do something like create_property($this, $pkey); so that I can then set it via $this-oraclecustomerid = 1122 or $this-set_oraclecustomerid(1122) ??? ?php function load_from_user_data($user_data) { //now loop through the rest of the user_data array and assign via a set_foo() method foreach ($user_data as $key = $value) { //try { $pkey = strtolower($key); //[dv] this is sort of a hack to automatically create a new property/variable // for 'new' hashes key/values we may not know about. // It's really designed to supress errors and they really should be added to this User.class.php properly. if ( !property_exists($this, $pkey) ) { echo creating new property this-$pkey with $valuebr\n; $this-$pkey = $value; //THIS BLOWS UP ON THE __set() echo this-$pkey = .$this-$pkey; } Hey, well, $this-$pkey is wrong syntax. Try $this-pkey = $value No Eddie, it's one of the beautiful, simple and powerful things about PHP. http://www.php.net/manual/en/language.variables.variable.php As I loop over the hash, i am TRYING to create a new class property of the key and assigning it the value. $pkey is basically the hash's $key in mixed case, forced to lowercase. You can do this for variables and for functions/methods too. This is a 'factory'. I've used it for example for parsing an XML file and operating on the data within various 'blocks' by reading the block name=foo value=bar and then executing $$name($value). Thanks for trying though. ;-) else { $class_variable = 'set_'.$pkey; $this-$class_variable($value); unset($user_data[$key]); } } //catch (Exception $e) { //echo $e-getMessage().\n; } } //should new fields be returned in the $user_data that are not accounted for above... if ($_SESSION['DEVELOPMENT'] count($user_data)) { echo !-- Unaccounted for user_data hashes. Please add these into User.class.php:\n; var_dump($user_data); echo --; } //THESE TWO LINES FATAL ERROR ON THE __get(): echo this-oraclecustomerid = .$this-oraclecustomerid; echo this-get_oraclecustomerid() = .$this-get_oraclecustomerid(); } ?
Re: [PHP] How can I do the opposite of property_exists(), maybe a creat_property() in PHP5?
2009/2/3 Daevid Vincent dae...@daevid.com Is there a way to create a new property via PHP 5.2.4? I get a hash back from an authentication server. I'm not guaranteed that someone in another department won't add new key/values to the returned hash/array. I'm trying to work around that part gracefully so that the code doesn't blow up on a customer in such an event. The main try/catch will suppress errors already, but I thought it would be nice to be able to handle this stuff automatically rather than constantly updating a User.class.php file all the time. creating new property this-oraclecustomerid with 1122 but when I try to set the value with the $this-$pkey = $value; It triggers __call() which then triggers __set() which throws my BadProperty exception. How come $this-$pkey = $value isn't creating/setting a property? Or how do I do something like create_property($this, $pkey); so that I can then set it via $this-oraclecustomerid = 1122 or $this-set_oraclecustomerid(1122) ??? ?php function load_from_user_data($user_data) { //now loop through the rest of the user_data array and assign via a set_foo() method foreach ($user_data as $key = $value) { //try { $pkey = strtolower($key); //[dv] this is sort of a hack to automatically create a new property/variable // for 'new' hashes key/values we may not know about. // It's really designed to supress errors and they really should be added to this User.class.php properly. if ( !property_exists($this, $pkey) ) { echo creating new property this-$pkey with $valuebr\n; $this-$pkey = $value; //THIS BLOWS UP ON THE __set() echo this-$pkey = .$this-$pkey; } Hey, well, $this-$pkey is wrong syntax. Try $this-pkey = $value -eddy else { $class_variable = 'set_'.$pkey; $this-$class_variable($value); unset($user_data[$key]); } } //catch (Exception $e) { //echo $e-getMessage().\n; } } //should new fields be returned in the $user_data that are not accounted for above... if ($_SESSION['DEVELOPMENT'] count($user_data)) { echo !-- Unaccounted for user_data hashes. Please add these into User.class.php:\n; var_dump($user_data); echo --; } //THESE TWO LINES FATAL ERROR ON THE __get(): echo this-oraclecustomerid = .$this-oraclecustomerid; echo this-get_oraclecustomerid() = .$this-get_oraclecustomerid(); } ?
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
Re: [PHP] How can i do refresh my web since java script?
Tomás Rodriguez Orta wrote: input name=refrescar type=submit class=btn id=refresh onClick=sendrefresch() value=Refrescar script language=JavaScript type=text/JavaScript !-- function sendrefresch() { How can I do for refresch my web, by the button Refresh? } Why do you want to use javascript? You can create Reload link using: echo 'a href=' . $_SERVER['REQUEST_URI'] . 'Reload/a'; -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How can I do this correctly? Trying to get gd to work...
You must change extension_ dir php.ini. Also Re: [PHP] Question about Php/Mysql. Unsure if it's enabled. Those are enabled by default, also in your php.ini At 03:22 1/8/02 -0500, you wrote: with dl() function. Thanks for your time. Even though I read php.net's support on dl, I still cannot get it to work, so I figured I'd ask (if I may) The syntax was: ? dl(libdg.so) ? ERROR: Warning: Unable to load dynamic library './libdgso' - ./libdgso: cannot open shared object file: No such file or directory in /home/sites/site7/web/php/page.php on line 2 Line 5 is of course dl(libgd.so) Which .so file should I choose to use and is there a specific path I must type? /usr/lib/libgd.so.1.8.3 /usr/lib/libgd.so.1 /usr/lib/libgd.so Thanks. I just want to try to load gd using this method so I can become more familiar with it.. Joel Sincerely, João P. Bragança -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
RE: [PHP] how can I do this !!
i have txt file have this words -- bla bla bla [phpcode] ? echo 'hello word'; ? [/phpcode] bla bla bla --- now I want to convert the code that are between [phpcode] and [/phpcode] to colorized code .. How I can do that Copy the file to .phps and access that page via the webserver? Chris
Re: [PHP] how can I do this !!
What do you mean, colorized code? At 02:26 PM 10/25/2001, Alawi wrote: i have txt file have this words -- bla bla bla [phpcode] ? echo 'hello word'; ? [/phpcode] bla bla bla --- now I want to convert the code that are between [phpcode] and [/phpcode] to colorized code .. How I can do that Sliante, Richard S. Crawford http://www.mossroot.com mailto:[EMAIL PROTECTED] AIM: Buffalo2K ICQ: 11646404 Y!: rscrawford It is only with the heart that we see rightly; what is essential is invisible to the eye. --Antoine de Saint Exupéry Push the button, Max! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
RE: [PHP] how can I do this !!
show_source(basename($PHP_SELF)); At 02:26 PM 10/25/2001, Alawi wrote: i have txt file have this words -- bla bla bla [phpcode] ? echo 'hello word'; ? [/phpcode] bla bla bla --- now I want to convert the code that are between [phpcode] and [/phpcode] to colorized code .. How I can do that Sliante, Richard S. Crawford http://www.mossroot.com mailto:[EMAIL PROTECTED] AIM: Buffalo2K ICQ: 11646404 Y!: rscrawford It is only with the heart that we see rightly; what is essential is invisible to the eye. --Antoine de Saint Exupéry -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] how can I do this !! color text in html
This should work. You can also use html code to use a style sheet ref. ? ECHO FONT COLOR='#2378A0' SIZE='3'Hello Word/FONT; ? i have txt file have this words -- bla bla bla [phpcode] ? echo 'hello word'; ? [/phpcode] bla bla bla --- now I want to convert the code that are between [phpcode] and [/phpcode] to colorized code .. How I can do that -- _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/ Michael Seely 408-777-9949 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] How can I do this?? :((
"Sandeep Hundal" [EMAIL PROTECTED] wrote: Now what I want to do is have another query which asks the second table the number of comments per each section id, for that section, (and I think QUERY2 does this fine). But I need the answers to integrate with the information pulled from first query. How can I intergrate the results from 2 queries into one basically! $query1 = " SELECT entry FROM diary WHERE month='$month' ORDER BY id desc"; $result1 = mysql_query($query1); $query2 = "SELECT count(*) AS replies FROM sexsurvey_boys WHERE section='diary' GROUP BY sectionid ORDER BY sectionid desc"; $result2 = mysql_query($query2); There's no common fields b/w the two queries so it's not clear how you want to merge the two queries. If you wanted all results from one table and matching results from a second table based on a common field in both tables I'd suggest a LEFT JOIN (plenty of documentation on mysql.com and rest of internet). Looks like this probably won't help in your case so you might want to look at temporary tables, which will allow you to save the results from one or both queries into a table and then run a query using the temporary tables. Or you may be able to organize your PHP code to run the second query after each row of the first query is returned and integrate the results within PHP, but you could end up running a ton of queries that way. -- Steve Werby COO 24-7 Computer Services, LLC Tel: 804.817.2470 http://www.247computing.com/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]