Re: [PHP] php/mysql object id question..
Jasper Bryant-Greene schrieb: > Oliver Grätz wrote: > >>3. Yes. One can abuse exceptions to return something in a constructor. >> Just another argument against exceptions ;-) OK, it's unorthodox, >> if you absolutely need to do that, do it and tell nobody *g*. > > This is not "abusing" exceptions. If you throw an exception then the > expected behaviour should be that the following code should not be > executed [1]. If you throw the exception for error purpose it's OK, but if you throw the exception just for the purpose of returning a value then it's abuse. That's what I meant. > Throwing an exception inside a constructor will prevent the object from > being created. It will not allow you to "return something" of your > choice -- I haven't tested but I would expect that the variable you were > setting as the object would either remain unset or would be set to NULL. Tested. non-object. At least this is as expected ;-) > I'm not too sure why you said "just another argument against exceptions" > (apart from perhaps a lack of understanding) as exceptions are a very > useful feature in any language. Sorry I didn't mark this as personal opinion (just like all the rest). Exceptions make good programming more difficult without need. Raymond Chen wrote this about exceptions: http://blogs.msdn.com/oldnewthing/archive/2005/1/14.aspx AllOLLi This time it will surely run. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] php/mysql object id question..
Oliver Grätz wrote: 3. Yes. One can abuse exceptions to return something in a constructor. Just another argument against exceptions ;-) OK, it's unorthodox, if you absolutely need to do that, do it and tell nobody *g*. This is not "abusing" exceptions. If you throw an exception then the expected behaviour should be that the following code should not be executed [1]. Throwing an exception inside a constructor will prevent the object from being created. It will not allow you to "return something" of your choice -- I haven't tested but I would expect that the variable you were setting as the object would either remain unset or would be set to NULL. IMHO, this is exactly how it should be, definitely not "unorthodox", and I'm not too sure why you said "just another argument against exceptions" (apart from perhaps a lack of understanding) as exceptions are a very useful feature in any language. [1] http://php.net/exceptions -- Jasper Bryant-Greene Freelance web developer http://jasper.bryant-greene.name/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] php/mysql object id question..
bruce schrieb: > but then, i'm starting to realize that there's probably a great deal of > 'opensource' code that's in use that hasn't been thoroughly vetted. 1. Constructors cannot explicitly return anything because they always return the new object. 2. OpenSource project and the code in them: 2.1 Most of the projects are grown over time. Over a long time. Do you know that moment when you look at code you wrote two years ago and find it disgusting? 2.2 Large projects have large programmer communities and therefore programmers with varying expertise. 2.3 It's PHP. It' PHP! The language a gorilla could learn. 3. Yes. One can abuse exceptions to return something in a constructor. Just another argument against exceptions ;-) OK, it's unorthodox, if you absolutely need to do that, do it and tell nobody *g*. AllOLLi Eric: "Hello." Nadia: "Hi!" Eric: "I'm.. Eric.. Weiss.. 38.. single." [Alias 402] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] php/mysql object id question..
it's not my understanding/use of classes.. my issue is that i was blindly using code that's been used by a lot of people, but that it obviously hadn't been thoroughly checked!! but then, i'm starting to realize that there's probably a great deal of 'opensource' code that's in use that hasn't been thoroughly vetted. when i used to be a software engineer, there's no way in hell that my teams would have offered some of this up for consumption.. -bruce lloks like i'm going to need to start remembering/using that which i long ago tried to forget!! -Original Message- From: Edward Vermillion [mailto:[EMAIL PROTECTED] Sent: Wednesday, September 14, 2005 2:46 PM To: [EMAIL PROTECTED] Cc: php-general@lists.php.net Subject: Re: [PHP] php/mysql object id question.. bruce wrote: > stephen, > > you're correct regarding what's going on... i had taken some code used in > phpBB, and blindly slammed it into my app to test out their db class... i > had assumed that it worked > > guess what!!! > > in their constructor, they have the 'return false' arrggghh!!! a quick look > at google, and it appears that you can't return any val from a constructor. > in fact, the 'object id' that's being returned appears to simply be (as you > stated) the instance of the class that was created... as opposed to a return > val... > > thoughts/comments/etc... > > -bruce > Sounds like a good thing to me. What would happen to your code if the constructor returned false? How would you handle it? And why would you want it to in the first place? The return from a = new Class(); should be an object as you're assigning it to a variable that is supposed to represent the object. If your doing something in the constructor that might return false, then set a property in the class to false and check that. IE for a database class if you set up your connection in the constructor then do a if(!mysql_connect...){ $this->connected = false; } and then check $db->connected after a $db = new DB(); or whatever your doing. Don't get me wrong, I had a lot of return false;'s in my constructors before I found out you can't do that, then started to wonder why I would want to. ;) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] php/mysql object id question..
bruce wrote: in their constructor, they have the 'return false' arrggghh!!! a quick look at google, and it appears that you can't return any val from a constructor. in fact, the 'object id' that's being returned appears to simply be (as you stated) the instance of the class that was created... as opposed to a return val... If you want to stop the object from being created for any reason (e.g. connection to database failed) and you're using PHP 5, I believe that you can throw an exception in the constructor. From http://php.net/language.oop5.basic : "An object will always be assigned when creating a new object unless the object has a constructor defined that throws an exception on error." -- Jasper Bryant-Greene Freelance web developer http://jasper.bryant-greene.name/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] php/mysql object id question..
bruce wrote: stephen, you're correct regarding what's going on... i had taken some code used in phpBB, and blindly slammed it into my app to test out their db class... i had assumed that it worked guess what!!! in their constructor, they have the 'return false' arrggghh!!! a quick look at google, and it appears that you can't return any val from a constructor. in fact, the 'object id' that's being returned appears to simply be (as you stated) the instance of the class that was created... as opposed to a return val... thoughts/comments/etc... -bruce Sounds like a good thing to me. What would happen to your code if the constructor returned false? How would you handle it? And why would you want it to in the first place? The return from a = new Class(); should be an object as you're assigning it to a variable that is supposed to represent the object. If your doing something in the constructor that might return false, then set a property in the class to false and check that. IE for a database class if you set up your connection in the constructor then do a if(!mysql_connect...){ $this->connected = false; } and then check $db->connected after a $db = new DB(); or whatever your doing. Don't get me wrong, I had a lot of return false;'s in my constructors before I found out you can't do that, then started to wonder why I would want to. ;) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] php/mysql object id question..
stephen, you're correct regarding what's going on... i had taken some code used in phpBB, and blindly slammed it into my app to test out their db class... i had assumed that it worked guess what!!! in their constructor, they have the 'return false' arrggghh!!! a quick look at google, and it appears that you can't return any val from a constructor. in fact, the 'object id' that's being returned appears to simply be (as you stated) the instance of the class that was created... as opposed to a return val... thoughts/comments/etc... -bruce -Original Message- From: Stephen Leaf [mailto:[EMAIL PROTECTED] Sent: Wednesday, September 14, 2005 1:51 PM To: php-general@lists.php.net Subject: Re: [PHP] php/mysql object id question.. On Wednesday 14 September 2005 03:42 pm, bruce wrote: > hi... > > i have the following psuedo code... > > i'm showing the pertinent parts, and eliminating the rest... > > -- > class sql > { > > function sql(...) > { > return false > > mysql_ > > mysql_ > } > > } > > $db = new sql(...) The new sql() is returning an object because that is what your asking for. now if you did a $db->sql() that'd return false. yes you did put a return false in the constructor but an object is what is being created and thus what is being returned. In my opinion a return anything within the constructor shouldn't be allowed. This might be the behavior in newer versions but I don't know. newer syntax is: class sql { function __construct (...) { ... } } truthfully when you do the $db = new sql(...); you are not running that function.. you are instantiating a new instance of sql which is invoking the object's constructor. > > echo "db" = .$db; > > > $db comes back as an object id.. even when i force a 'return false'. it > appears that no matter what i do, the class constructor returns an object > id!!! the weird thing is that it gets to the 'return false' and then still > seems to return the 'object id' i've also replaced 'false' with other > values to see if it made a diff.. it didn't which is good... i would have > really hit the roof then!! > > so.. why is this behavior occuring. > > any ideas as to why? > > or, am i just too tired right now! > > thanks > > bruce > > ps.. i could use the $db, object ID, and try to see if it actually access > the db, in order to determine if it actually exists. but i shouldn't have > to do that... the class should return false!!! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] php/mysql object id question..
On Wednesday 14 September 2005 03:42 pm, bruce wrote: > hi... > > i have the following psuedo code... > > i'm showing the pertinent parts, and eliminating the rest... > > -- > class sql > { > > function sql(...) > { > return false > > mysql_ > > mysql_ > } > > } > > $db = new sql(...) The new sql() is returning an object because that is what your asking for. now if you did a $db->sql() that'd return false. yes you did put a return false in the constructor but an object is what is being created and thus what is being returned. In my opinion a return anything within the constructor shouldn't be allowed. This might be the behavior in newer versions but I don't know. newer syntax is: class sql { function __construct (...) { ... } } truthfully when you do the $db = new sql(...); you are not running that function.. you are instantiating a new instance of sql which is invoking the object's constructor. > > echo "db" = .$db; > > > $db comes back as an object id.. even when i force a 'return false'. it > appears that no matter what i do, the class constructor returns an object > id!!! the weird thing is that it gets to the 'return false' and then still > seems to return the 'object id' i've also replaced 'false' with other > values to see if it made a diff.. it didn't which is good... i would have > really hit the roof then!! > > so.. why is this behavior occuring. > > any ideas as to why? > > or, am i just too tired right now! > > thanks > > bruce > > ps.. i could use the $db, object ID, and try to see if it actually access > the db, in order to determine if it actually exists. but i shouldn't have > to do that... the class should return false!!! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php