Re: [PHP] Methods for instatiating an object
Rudy Metzger wrote: It is as you say. The problem is normally negligible, but makes a big difference if there is alot done in the constructor of the object (e.g. scanning a huge file for certain strings). Then this will slow things down. This however is solved in PHP5. Thanks to all for your answers. They have been so clear. I think it's interesting to know how that works. Knowing only that it works, some times is not enought. ;) Thanks again, Jordi. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Methods for instatiating an object
On Wed, 2004-05-12 at 12:41, Jordi Canals wrote: > Hi all, > > It is not a big issue, but that is something that I never had clear. > I've been looking at the manual and found no answer, so I will ask with > an example: > > When instantiating an object, I could do it in two different ways: > > A)$object = new MyClass; > B)$object =& new MyClass; > > I cannot understand the exect difference betwen the two methods. Because > there is not yet an object, the second example does not return a > reference to an existing object. > > I think perhaps the diference is: > > 1) In case A, PHP creates a new object and returns a Copy of this new > object, so really I will have the object two instances for the object in > memory ... Only until the garbage collection cleans up the "first" instance. So you _always_ create one instance for the garbage collector only. > > 2) In case B, PHP creates a noew object and returns a reference to this > newly created object. In this case there is only one instance of the object. > > Does it works that way? If not, What is exactly the difference? It is as you say. The problem is normally negligible, but makes a big difference if there is alot done in the constructor of the object (e.g. scanning a huge file for certain strings). Then this will slow things down. This however is solved in PHP5. > > TIA, > Jordi. Cheerio /rudy signature.asc Description: This is a digitally signed message part
Re: [PHP] Methods for instatiating an object
On Wed, 12 May 2004 08:41 pm, Jordi Canals wrote: > A)$object = new MyClass; > B)$object =& new MyClass; > 1) In case A, PHP creates a new object and returns a Copy of this new > object, so really I will have the object two instances for the object in > memory ... > > 2) In case B, PHP creates a noew object and returns a reference to this > newly created object. In this case there is only one instance of the > object. > > Does it works that way? If not, What is exactly the difference? You are quite correct... http://php.net/references --markc -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Methods for instatiating an object
Hi all, It is not a big issue, but that is something that I never had clear. I've been looking at the manual and found no answer, so I will ask with an example: When instantiating an object, I could do it in two different ways: A) $object = new MyClass; B) $object =& new MyClass; I cannot understand the exect difference betwen the two methods. Because there is not yet an object, the second example does not return a reference to an existing object. I think perhaps the diference is: 1) In case A, PHP creates a new object and returns a Copy of this new object, so really I will have the object two instances for the object in memory ... 2) In case B, PHP creates a noew object and returns a reference to this newly created object. In this case there is only one instance of the object. Does it works that way? If not, What is exactly the difference? TIA, Jordi. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php