57

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

$this->entryId = $entryId;
TStamper
  • 28,864
  • 10
  • 63
  • 72

9 Answers9

168

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;
Sergey Telshevsky
  • 11,613
  • 6
  • 51
  • 77
farzad
  • 8,387
  • 5
  • 29
  • 40
  • 1
    So are there pointers in PHP like in C++? Variables and reference to variables are ok, but how about pointers? – Koray Tugay Jan 19 '13 at 10:34
  • ok, now we have a reference. is it possible to increase the reference so it will point to the next part in memory? i.e: `$a=array(1,2); $b=&$a[0]; &$b++; //now the $b points to $a[1]` ? this would mean a real work with pointers to me. is such a consctruct or similar possible? – ulkas Mar 01 '13 at 14:50
  • 6
    Objects are always passed by reference! – ComFreek Aug 27 '13 at 15:38
  • Its worth noting that in C++ that the -> operator is actually just syntactic sugar for dereference and access property `(*myObject).property` is equivalent to `myObject->property`. Since php doesn't actually have a pointer in the C++ does (php has [references](http://www.php.net/manual/en/language.references.whatare.php) instead the -> operator in php doesn't mean the exact same thing as in c++. You cannot use (*$myObject).property in place of -> in php. – echochamber Jun 13 '14 at 05:32
29

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

b-c-d-e-f


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.

Community
  • 1
  • 1
aliqandil
  • 1,223
  • 14
  • 26
  • 2
    Thank you very much for that post, real gem. I was lost with multiple `$` though, but I found good explanation what it does -- http://stackoverflow.com/questions/2715654/what-does-mean-in-php – greenoldman Feb 01 '15 at 18:57
  • 1
    Great Explanation! (Y) – DirtyBit Dec 16 '15 at 18:25
  • 2
    the day we see that PHP developpers have never studied C++ ... so don't talk of pointer/reference programming if you never studied C++ because C++ is the langague where those concepts were created/implemented for the 1st and unique time . – reuns Dec 21 '15 at 00:37
11

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.)

Anthony
  • 6,840
  • 1
  • 19
  • 22
5

To answer the second part of your question - there are no pointers in PHP.

When working with objects, you generally pass by reference rather than by value - so in some ways this operates like a pointer, but is generally completely transparent.

This does depend on the version of PHP you are using.

Toby Hede
  • 35,582
  • 27
  • 127
  • 161
  • 1
    Passing objects is still generally by value in PHP. PHP variables never actually have objects as their values - they have references to objects. When you pass an object variable to a function you then have two variables, both of which have the same value, and that value is a reference to an object. The fact that there are references is just about how variables work in PHP, it's not related to passing. You can reassign the variable inside the function without changing the outside variable. I'm ignoring the ancient php version 4 here. – bdsl Jun 03 '18 at 13:34
3

You can simulate pointers to instantiated objects to some degree:

class pointer {
   var $child;

   function pointer(&$child) {
       $this->child = $child;
   }

   public function __call($name, $arguments) {
       return call_user_func_array(
           array($this->child, $name), $arguments);
   }
}

Use like this:

$a = new ClassA();

$p = new pointer($a);

If you pass $p around, it will behave like a C++ pointer regarding method calls (you can't touch object variables directly, but that's evil anyways :) ).

Peter
  • 71
  • 1
2

Yes there is something similar to pointers in PHP but may not match with what exactly happens in c or c++. Following is one of the example.

$a = "test";
$b = "a";
echo $a;
echo $b;
echo $$b;

//output
test
a
test

This illustrates similar concept of pointers in PHP.

prathk
  • 91
  • 1
  • 2
2

entryId is an instance property of the current class ($this) And $entryId is a local variable

Rich
  • 34,878
  • 31
  • 108
  • 151
0

PHP can use something like pointers:

$y=array(&$x);

Now $y acts like a pointer to $x and $y[0] dereferences a pointer.

The value array(&$x) is just a value, so it can be passed to functions, stored in other arrays, copied to other variables, etc. You can even create a pointer to this pointer variable. (Serializing it will break the pointer, however.)

  • 1
    uhhhhhhhhh, no. $ php -r '$x = 1; > $y = array(&$x); > var_dump($y); > $x = 2; > var_dump($y); > $y[0] = 3; > var_dump($x);' //// Output: array(1) { [0]=> &int(1) } array(1) { [0]=> &int(2) } int(3) – Jim Rubenstein Dec 14 '10 at 18:14
0

PHP passes Arrays and Objects by reference (pointers). If you want to pass a normal variable Ex. $var = 'boo'; then use $boo = &$var;.