Jump to content

& in front of a function


travelerBT

Recommended Posts

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?

Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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. 

 

 

Link to comment
Share on other sites

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.

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.