Very Weird Database class behavior |
[eluser]Eric Cope[/eluser]
When I fetch the result, store it in a local variable, edit the local variable, the variable within the database class is also edited. I don't understand why. Any ideas. Here is the code Code: $this->db->where('companyID', $companyID); Here are the dumps Code: object(CI_DB_mysql_result)#17 (7) {
[eluser]Colin Williams[/eluser]
In PHP 5, all objects are assigned by reference. You would need to clone() the object to "break" the link between them.
[eluser]Eric Cope[/eluser]
That makes sense now. I am not used to that behavior, but in this case it works well for me. Thanks!
[eluser]n0xie[/eluser]
[quote author="Eric Cope" date="1262082117"]That makes sense now. I am not used to that behavior, but in this case it works well for me. Thanks![/quote] As far as I know, most programming languages pass by reference, except low level (or high level depending on your point of view) languages like C/C++
[eluser]Eric Cope[/eluser]
you nailed it - My favorite language is C. I am used to explicit pass by reference. Was it this way in PHP4?
[eluser]n0xie[/eluser]
Aah that explains the misunderstanding. In PHP4, everything was passed by value, including objects. This has changed in PHP5: all objects are now passed by reference. A small sidenote if you are used to C/C++, references in PHP are for all intent and purposes just pointers. Although that's not really true: internally it is an alias to the symbol table. This confuses a lot of people coming from C/C++. If you have time to spare and want some interesting read, I would advice you to take a look at the php manual and especially the comments there (you can read the confusion of some C/C++ programmers between the lines). If you want to know what goes on internally take a look here. Although that was written for PHP4 much of the internal system didn't change much in PHP5, just that the zend engine is 'smarter' now. Btw. if you want a fresh unaltered copy of your object you can always clone it.
[eluser]Eric Cope[/eluser]
[quote author="n0xie" date="1262129666"]Aah that explains the misunderstanding. In PHP4, everything was passed by value, including objects. This has changed in PHP5: all objects are now passed by reference. A small sidenote if you are used to C/C++, references in PHP are for all intent and purposes just pointers. Although that's not really true: internally it is an alias to the symbol table. This confuses a lot of people coming from C/C++. If you have time to spare and want some interesting read, I would advice you to take a look at the php manual and especially the comments there (you can read the confusion of some C/C++ programmers between the lines). If you want to know what goes on internally take a look here. Although that was written for PHP4 much of the internal system didn't change much in PHP5, just that the zend engine is 'smarter' now. Btw. if you want a fresh unaltered copy of your object you can always clone it.[/quote] Thank makes more sense too, I learned PHP on PHP4. Thanks for the links! |
Welcome Guest, Not a member yet? Register Sign In |