travelerBT Posted April 26, 2007 Share Posted April 26, 2007 I just want to verify something. $name = new test(); is the same as: $name = &test(); Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/48840-in-front-of-a-function/ Share on other sites More sharing options...
redbullmarky Posted April 26, 2007 Share Posted April 26, 2007 wrong place. "PHPFreaks.com Questions, Comments, & Suggestion" is for stuff related to PHPFeaks itself... moving to OOP help as & generally used more in that area... as for your question, $name = new test(); will instantiate the class called test. $name = &test() as far as I know is pretty useless. the & operator in PHP is used to reference something (as opposed to copying it). eg: <?php $a = 'Hello'; $b = $a; // copy - so create a totally new var $c = &$a; // reference $a = 'Goodbye'; echo "$a, $b, $c"; // will output: Goodbye, Hello, Goodbye ?> make sense? Quote Link to comment https://forums.phpfreaks.com/topic/48840-in-front-of-a-function/#findComment-239393 Share on other sites More sharing options...
per1os Posted April 26, 2007 Share Posted April 26, 2007 redbull is right. I think the more technical reference is called pointer. http://en.wikipedia.org/wiki/Pointer That describes how C and C++ use pointers (no better language to learn pointers in). Quote Link to comment https://forums.phpfreaks.com/topic/48840-in-front-of-a-function/#findComment-239412 Share on other sites More sharing options...
Jenk Posted April 26, 2007 Share Posted April 26, 2007 as for your question, $name = new test(); will instantiate the class called test. $name = &test() as far as I know is pretty useless. php4 does not pass objects by reference, so it is mandatory to use =&, unless you want copies of your objects everywhere. Quote Link to comment https://forums.phpfreaks.com/topic/48840-in-front-of-a-function/#findComment-239446 Share on other sites More sharing options...
utexas_pjm Posted April 27, 2007 Share Posted April 27, 2007 I just want to verify something. $name = new test(); is the same as: $name = &test(); Thanks! <?php $name = new test(); ?> Instantiates an object. If you are trying to call a function called test(), the above code will throw an error. <?php $name = &test(); ?> Returns a reference to whatever is being returned by the test function. These are two completely different things. Best, Patrick Quote Link to comment https://forums.phpfreaks.com/topic/48840-in-front-of-a-function/#findComment-239490 Share on other sites More sharing options...
boo_lolly Posted April 27, 2007 Share Posted April 27, 2007 so, having the & symbol in front of a variable acts as a pointer. does this mean: <?php $a = 'Hello, Earth'; $b = &$a; $a = 'Hello, Mars'; echo $a .' | '. $b; ?> output: Hello, Mars | Hello Earth is this correct? Quote Link to comment https://forums.phpfreaks.com/topic/48840-in-front-of-a-function/#findComment-239567 Share on other sites More sharing options...
redbullmarky Posted April 27, 2007 Share Posted April 27, 2007 is this correct? no. the output from that would be 'Hello Mars | Hello Mars' you're setting up $a with 'Earth' as the value, then making $b reference $a - therefore, changing $a will also "change" $b too (and vice versa). to get the output you specified in your example, remove the & . Then, $b will be set up independently of $a with $a's value. Quote Link to comment https://forums.phpfreaks.com/topic/48840-in-front-of-a-function/#findComment-239640 Share on other sites More sharing options...
Jenk Posted April 27, 2007 Share Posted April 27, 2007 I just want to verify something. $name = new test(); is the same as: $name = &test(); Thanks! <?php $name = new test(); ?> Instantiates an object. If you are trying to call a function called test(), the above code will throw an error. <?php $name = &test(); ?> Returns a reference to whatever is being returned by the test function. These are two completely different things. Best, Patrick but what I said still applies. The assignment operators in PHP4 do not pass objects by reference unless you tell them to, with the reference operator. Quote Link to comment https://forums.phpfreaks.com/topic/48840-in-front-of-a-function/#findComment-239643 Share on other sites More sharing options...
utexas_pjm Posted April 27, 2007 Share Posted April 27, 2007 But what I said still applies. The assignment operators in PHP4 do not pass objects by reference unless you tell them to, with the reference operator. Absolutely, I apologize if my comment came across as though I was correcting or dismissing your post. It seemed to me that the OP was trying to say that: $name = new test(); AND $name = &test(); are two ways of doing the same thing, which of course, they are not. Patrick Quote Link to comment https://forums.phpfreaks.com/topic/48840-in-front-of-a-function/#findComment-239742 Share on other sites More sharing options...
travelerBT Posted April 27, 2007 Author Share Posted April 27, 2007 Thanks so much for all the posts. I am new to OOP and trying to learn all this stuff One more question, what is the advantage to having a reference? Quote Link to comment https://forums.phpfreaks.com/topic/48840-in-front-of-a-function/#findComment-239780 Share on other sites More sharing options...
utexas_pjm Posted April 27, 2007 Share Posted April 27, 2007 One more question, what is the advantage to having a reference? In technical terms a reference takes far less memory. A reference is represented internally by a 32/64 bit block of memory that points to memory location on the heap where the object is stored. So consider you have an object in a 32 bit architecture that is 1,000 bits in size, if you have 300 references to the object you are using ~ 1000 + (32*300) = 1000 + 9600 = 10600 bits of memory. If you have 300 copies of the object you are using ~ 30000 bits of memory. Quote Link to comment https://forums.phpfreaks.com/topic/48840-in-front-of-a-function/#findComment-239792 Share on other sites More sharing options...
448191 Posted May 4, 2007 Share Posted May 4, 2007 As far as OOP is concerned, using the operator on php4 objects goes beyond a simple performance gain. If you keep copying an object you'll probably find yourself in a situation where you are operating on an object copy instead of the original, creating unexpected results. By default, php creates copies of all variables that don't use handles (filepointers, database connections - and, in php5, object handles), both when accepting arguments as when returning values from a function (or method). pass by reference: $arg = 0; someFunction($arg); function someFunction(&$arg){ $arg++; //$arg is now 1 } This (call-time pass by reference) is depreciated in php5: someFunction(&$arg); function someFunction($arg){ $arg = 1; //$arg is now 1 } Returning by reference: function &someFunction($arg){ return ++$arg; } //Assign by reference: $arg = &someFunction(0); //$arg is now 1 Note that that last example is pretty useless. having the function return a copy would produce the same result, and there's no performance gain whatsoever. Returning by reference is mostly only useful when using static variables in the function scope. Be careful passing and returning variables by reference. Apart from passing objects in php4, the Zend engine is smart enough to optimize performance on it's own, and the default behavior is usually what you're after (in fact there are some array functions that operate on references that I'd rather have have they wouldn't). So unless you have a good reason to use it; don't. Quote Link to comment https://forums.phpfreaks.com/topic/48840-in-front-of-a-function/#findComment-245150 Share on other sites More sharing options...
448191 Posted May 5, 2007 Share Posted May 5, 2007 For a more thorough explanation of why to be careful with references, refer to this topic: http://www.phpfreaks.com/forums/index.php/topic,139306.msg591626.html Quote Link to comment https://forums.phpfreaks.com/topic/48840-in-front-of-a-function/#findComment-246131 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.