Are there pointers in php?

What does this code mean? Is this how you declare a pointer in php?

$this->entryId = $entryId;

Variable names in PHP start with $ so $entryId is the name of a variable. $this is a special variable in Object Oriented programming in PHP, which is reference to current object. -> is used to access an object member (like properties or methods) in PHP, like the syntax in C++. so your code means this:

Place the value of variable $entryId into the entryId field (or property) of this object.

The & operator in PHP, means pass reference. Here is a example:

$b=2;
$a=$b;
$a=3;
print $a;
print $b;
// output is 32

$b=2;
$a=&$b; // note the & operator
$a=3;
print $a;
print $b;
// output is 33

In the above code, because we used & operator, a reference to where $b is pointing is stored in $a. So $a is actually a reference to $b.

In PHP, arguments are passed by value by default (inspired by C). So when calling a function, when you pass in your values, they are copied by value not by reference. This is the default IN MOST SITUATIONS. However there is a way to have pass by reference behaviour, when defining a function. Example:

function plus_by_reference( &$param ) {
      // what ever you do, will affect the actual parameter outside the function
      $param++;
}

$a=2;
plus_by_reference( $a );
echo $a;
// output is 3

There are many built-in functions that behave like this. Like the sort() function that sorts an array will affect directly on the array and will not return another sorted array.

There is something interesting to note though. Because pass-by-value mode could result in more memory usage, and PHP is an interpreted language (so programs written in PHP are not as fast as compiled programs), to make the code run faster and minimize memory usage, there are some tweaks in the PHP interpreter. One is lazy-copy (I'm not sure about the name). Which means this:

When you are coping a variable into another, PHP will copy a reference to the first variable into the second variable. So your new variable, is actually a reference to the first one until now. The value is not copied yet. But if you try to change any of these variables, PHP will make a copy of the value, and then changes the variable. This way you will have the opportunity to save memory and time, IF YOU DO NOT CHANGE THE VALUE.

So:

$b=3;
$a=$b;
// $a points to $b, equals to $a=&$b
$b=4;
// now PHP will copy 3 into $a, and places 4 into $b

After all this, if you want to place the value of $entryId into 'entryId' property of your object, the above code will do this, and will not copy the value of entryId, until you change any of them, results in less memory usage. If you actually want them both to point to the same value, then use this:

$this->entryId=&$entryId;

No, As others said, "There is no Pointer in PHP." and I add, there is nothing RAM_related in PHP.

And also all answers are clear. But there were points being left out that I could not resist!

There are number of things that acts similar to pointers

  • eval construct (my favorite and also dangerous)
  • $GLOBALS variable
  • Extra '$' sign Before Variables (Like prathk mentioned)
  • References

  • First one

    At first I have to say that PHP is really powerful language, knowing there is a construct named "eval", so you can create your PHP code while running it! (really cool!)

    although there is the danger of PHP_Injection which is far more destructive that SQL_Injection. Beware!

    example:

    Code:

    $a='echo "Hello World.";';
    eval ($a);
    

    Output

    Hello World.

    So instead of using a pointer to act like another Variable, You Can Make A Variable From Scratch !


    Second one

    $GLOBAL variable is pretty useful, You can access all variables by using its keys.

    example:

    Code:

    $three="Hello";$variable=" Amazing ";$names="World";
    $arr = Array("three","variable","names");
    foreach($arr as $VariableName)
        echo $GLOBALS[$VariableName];
    

    Output

    Hello Amazing World

    Note: Other superglobals can do the same trick in smaller scales.


    Third one

    You can add as much as '$'s you want before a variable, If you know what you're doing.

    example:

    Code:

    $a="b";
    $b="c";
    $c="d";
    $d="e";
    $e="f";
    
    echo $a."-";
    echo $$a."-";   //Same as $b
    echo $$$a."-";  //Same as $$b or $c
    echo $$$$a."-"; //Same as $$$b or $$c or $d
    echo $$$$$a;    //Same as $$$$b or $$$c or $$d or $e
    

    Output

    bcdef


    Last one

    Reference are so close to pointers, but you may want to check this link for more clarification.

    example 1:

    Code:

    $a="Hello";
    $b=&$a;
    $b="yello";
    echo $a;
    

    Output

    yello

    example 2:

    Code:

    function junk(&$tion)
    {$GLOBALS['a'] = &$tion;}
    $a="-Hello World<br>";
    $b="-To You As Well";
    echo $a;
    junk($b);
    echo $a;
    

    Output

    -Hello World

    -To You As Well

    Hope It Helps.


    That syntax is a way of accessing a class member. PHP does not have pointers, but it does have references.

    The syntax that you're quoting is basically the same as accessing a member from a pointer to a class in C++ (whereas dot notation is used when it isn't a pointer.)

    链接地址: http://www.djcxy.com/p/59100.html

    上一篇: 双美元符号的PHP

    下一篇: 在PHP中有指针吗?