What does this code mean? Is this how you declare a pointer in php?
$this->entryId = $entryId;
What does this code mean? Is this how you declare a pointer in php?
$this->entryId = $entryId;
entryId is an instance property of the current class ($this) And $entryId is a local variable
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.)
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.
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 pointer 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;
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.)