Liquid Fire Posted May 4, 2007 Share Posted May 4, 2007 I have been told that i should never pass by refernce in C++ becuase is it slower. The zend engine will automaticaly pass by refernce and if i change the pass variable the engine will then make a copy of the variable so it will only change the variable inside that function and not the pass variabel too(as if it were pass by reference. Is this true? Also when declaring a new class should i do: file_object = new CFile(); or file_object =& new CFile(); Quote Link to comment Share on other sites More sharing options...
Ravo Posted May 4, 2007 Share Posted May 4, 2007 declaring a new class is as easy as class classname { function stuff(){ } function stuff2(){ } function stuff3(){ } } and then running $class = new classname(); $class->stuff(); $class->stuff2(); $class->stuff3(); The other part is over my head. Quote Link to comment Share on other sites More sharing options...
btherl Posted May 4, 2007 Share Posted May 4, 2007 Yes, PHP uses "copy on write" for function arguments. If you pass a huge array into a function, it will not be copied unless you modify it, in which case a copy is triggered at the point of modification. Similarly with foreach loops over an array - if you modify the original array within a foreach loop, then a copy is triggered, and all modifications are made on the copy. At the end of the foreach loop, the original array is replaced with the modified version. This allows a foreach loop to run consistently even if the array is modified during the loop. The only thing I use references for in PHP is when I want to pass a large data structure to a function which will modify that data structure, such as a function that will fill in extra data in a large array. I can't help with the class stuff, sorry.. but I'm pretty sure that PHP5 does NOT need references when creating objects. PHP4, maybe. If you don't get a reply, try asking in the OO sub-forum. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.