ID: 29441 Comment by: itDept at ilevelinternet dot com Reported By: php at cyruslesser dot com Status: Bogus Bug Type: Class/Object related Operating System: * PHP Version: 5.* New Comment:
Hello, I found out today that an application that we developed using PHP 4 no longer works properly because of what Cyruslesser pointed out. If enough people are having a problem with this would it be feasible in the next release of PHP 5 that a new php.ini variable exist to tell PHP to either make a copy of an instance of a class or to pass the pointer to the information when one istance of a class is set equal to another variable like in Cyruslesser’s example when $b = $a? Thanks in advance for taking the time to answer my question. Previous Comments: ------------------------------------------------------------------------ [2004-08-31 04:14:29] php at cyruslesser dot com BTW, for anyone else reading this who also had trouble finding the documention, check this URL which is details the changes in the Zend 2 engine: http://www.php.net/zend-engine-2.php Specifically, the solution to my problem was very simple: $new_object = clone $old_object; Although I still feel that always forcing passing objects by reference to functions is a bit silly, I'm willing to live with it for the sake of the many great improvements to PHP5. Thanks. Cyrus ------------------------------------------------------------------------ [2004-08-03 03:38:32] [EMAIL PROTECTED] http://zend.com/manual/migration5.oop.php scroll down to "Objects Cloning" ------------------------------------------------------------------------ [2004-08-03 03:30:50] php at cyruslesser dot com Sorry, that was supposed to read "ARE THERE new functions to aid in the duplication of objects by value?" Cyrus ------------------------------------------------------------------------ [2004-08-03 03:26:24] php at cyruslesser dot com Yes, why must everyone I ask about this point out the blatantly obvious when I ask about this? :) It's passing classes by reference (or pointer if you prefer the C analog) not by value. I get it. Honestly I do. I just don't get why. Please, is there a way I can quickly copy an object? Where is memcpy()? Why can't I call clone() directly? Are the new functions to aid in the duplication of objects by value? I'm assuming that they do exist, and that I just don't know about them. I have faith in you guys... your language is the easiest and most intuitive language I have ever seen, and I love it. But what I do not understand why you have chosen to do this, or how it aids the language. I appreciate that copying a pointer (or reference) to a class is somewhat quicker than copying the entire object itself, but I don't really see how duplicating pointers is more useful that duplicating complete copies of the object. I just don't think that a small time saving (surely it's just a few memcpy() calls) is worth the extra time it now takes when you wish to duplicate an object. Or the problems this is going to cause with PHP4 software running under PHP5. In PHP5, if I have an object containing data retrieved from a database, and I want to copy it, I have to instantiate a new object, and either repeat all the calls to the database to retrieve the data, or copy every member variable individually to the new class. Either way it's slow and tedious. But I'm very hopeful that there is a new object copy function that wasn't in the PHP5 "what's new" documentation. I have to say, even C++ will automatically copy the contents of a class unless you override the assignment operator. Another irksome factor about this whole thing, is the inconsistency that this brings to the language. Previously a string object could be treated identically to a class object. $string1 = "a"; $string2 = $string1; $string2 = "b"; Which of course, will leave $string1 == "a". But if they where class objects, then the behavior suddenly changes, and $string1 is changed. This is going to be very confusing to people who have never learnt a lower-level OO language before. Even in C++ (which I would regard by today’s standard, as a pretty hard language to learn), strings and class objects behave in the same fashion when assigned, or used as arguments to functions. While we're on the topic of functions: In PHP4: function ($object) { /* pass by value */ /* This doesn't change the original object */ $object->change(); } function (&$object) { /* pass by reference */ /* This DOES change the original object */ $object->change(); } That made sense. There are two different ways to pass an object, and you can chose which one you wish to use in the declaration of the function, using '&'. In PHP5, both of those function have the same effect, they both modify the original object which is now always passed by reference. Is this the way it's going to stay? Cyrus Lesser ------------------------------------------------------------------------ [2004-08-02 18:24:11] [EMAIL PROTECTED] You don't copy a class, you copy an instance. And since PHP 5.0.0 you no longer copy by value but instead by object id. ------------------------------------------------------------------------ The remainder of the comments for this report are too long. To view the rest of the comments, please view the bug report online at http://bugs.php.net/29441 -- Edit this bug report at http://bugs.php.net/?id=29441&edit=1