So I understand that references aren't pointers: http://php.net/manual/en/language.references.arent.php
Question is, is it possible to work with pointers in php?
Given the following example I would guess that's what we do when working with objects:
class Entity
{
public $attr;
}
class Filter
{
public function filter(Entity $entity)
{
$entity->attr = trim($entity->attr);
}
}
$entity = new Entity;
$entity->attr = ' foo ';
$filter = new Filter;
$filter->filter($entity);
echo $entity->attr; // > 'foo', no white space
Is the example above working with pointers behind the sceen or is it still swapping memory, as when working with references?
Is the following:
class Entity
{
public $attr;
}
$entity = new Entity;
$entity->attr = 1;
$entity->attr = 2;
Something like this in C :
int* attr;
*attr = 1;
*attr = 2;
When you pass an object as an argument to your function, the variable itself is copied but the properties it has point to those of the original object, ie:
function test(Something $bar)
{
$bar->y = 'a'; // will update $foo->y
$bar = 123; // will not update $foo
}
$foo = new Something;
$foo->y = 'b';
test($foo);
// $foo->y == 'a';
Inside the function the memory references look a bit like this:
$bar ---+
+---> (object [ y => string('b') ])
$foo ---+
After $bar = 123;
it looks like this:
$bar ---> int(123)
$foo ---> (object [ y => 'b' ])
If you are asking that in this line
$entity->attr = trim($entity->attr);
the $entity
is a new memory or the reference of the old memory. It is the reference of the old memory (or pointer)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.