1

Is it possible to store a reference to an object's property (class member variable which holds a scalar data such as string or integer) within an object of a different class?

I am trying to have the following two echo statements produce identical results.

<?php

$x = new Type;
$x->name = 'abcd';

echo "x.name=" . $x->name . '<br/>';
echo "x.obj.name=" . $x->obj->value . '<br/>';

class Type
{
    public $obj; //Instance of Property (Property class defined below)
    public $name;

    function __construct()
    {
        $this->obj = new Property($this->name);
    }
}

class Property
{
    public $value;

    function __construct($v)
    {
        $this->value = $v;
    }
}

4 Answers 4

1

$this->obj = new Property($this->name);

Is called at the time of object creation. Which is executed before the assignment. i.e. When you call $x = new Type; The constructor is called and you try to copy 'name' which is empty by then

May be what you want it following, rather than passing the value, pass $this and keep the referance.

<?php


class Type
{
    public $obj; //Instance of Property (Property class defined below)
    public $name;

    function __construct()
    {
        $this->obj = new Property($this);
    }
}

class Property
{
    public $value;

    function __construct($ref)
    {
        $this->value = $ref;
    }
}


$x = new Type;
$x->name = 'abcd';

echo "x.name=" . $x->name . '<br/>';
echo "x.obj.name=" . $x->obj->value->name . '<br/>';
Sign up to request clarification or add additional context in comments.

Comments

1

While at the time of answering this question is 9+ years old, I've encountered a similar issue today and found a way to do what's requested.

In short: you should use references. Here's a a working example (PHP 8):

<?php

class Source
{
    public int $counter = 10;
}

class Consumer
{
    public int $value = 0;

    public function __construct(int &$value)
    {
        $this->value = &$value;
    }
}

$source = new Source();
// Pass property of Source instance to the consumer.
$consumer = new Consumer($source->counter);

assert($consumer->value === 10);
// Changing value in the Source instance.
$source->counter = 15;
// ... and value in the consumer updated as well.
assert($consumer->value === 15);

exit;

So, the answer is yes, it is possible.

Comments

0

You can pass the name value inside the constructor.

$x = new Type('abcd');

Without doing that, your constructor will not know what $this->name is yet. So we use it in the constructor and set the classes property before using it.

function __construct($p_name){
  $this->name = $p_name;
  $this->obj = new Property($this->name);
}

You could just as easily set the value after calling the constructor and then initialize the reference afterwards -

class Type {
  public $obj;
  public $name;

  function setProperty(){
    $this->obj = new Property($this->name);
  }
}

$x = new Type;
$x->name = 'abcd';
$x->setProperty();

echo "x.name=" . $x->name;
echo "x.obj.name=" . $x->obj->value;

Comments

0

This is an old question but just to add to this.

You can have an object with methods and properties inside of another object..

Example

$obj1 = new class1();

$obj2 = new class2($obj1); // you just grabbed $obj1 and stuck it inside $obj2

Now you can use the stored object's methods like so:

$obj2->obj1->method_from_obj1();

or access the stored object's properties like so:

$obj2->obj1->property_of_obj1; 

This is SUPER convenient if you instantiated an object of some API and want to use the API methods inside of your own object.

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.