4

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?

Edit

A different example:

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;
5
  • When you use objects, all the variables refer to the same object, and it's analogous to using pointers in a language like C or C++. Commented Nov 23, 2013 at 5:36
  • @mario in function arguments Entity $entity wont be a new object it will the same old one called as parameter.. Commented Nov 23, 2013 at 5:39
  • defining type in functions arguments will make them strict but will not create new object. please correct me if I'm'm wrong.. Commented Nov 23, 2013 at 5:40
  • @zzlalani Ah okay. So OP is asking about the implicit object references then. Commented Nov 23, 2013 at 5:41
  • updated the question with a new example. Commented Nov 23, 2013 at 5:49

2 Answers 2

3

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, i.e.:

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' ])
Sign up to request clarification or add additional context in comments.

11 Comments

Good answer, but the question is not so much about the outcome or expected behavior as you explain. The question is about how php work with the memory.
@ErikLandvall This tells exactly how it works with memory ... inside the function $bar->y is basically a pointer to $foo->y, except that there's no visible dereferencing :)
Ok, I find it hard to see in when explained in php. Could you resumble the same code in C. It would help a lot.
The reason I ask for this is because the magic behind PHP makes it hard to understand what really happens.
@ErikLandvall Okay, I've added an ASCII graph of a memory approximation.
|
1

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.