Thursday 3 March 2016

PHP : How to access to an object variable whose name is another variable of the object

Hello,

If we suppose that:

<?php
class test_class
{
       private $variable_name = 'id';
       private $id  = 0;
}

How to acces to variable 'id' inside an instance? :

<?php

class test_class
{
       private $variable_name = 'id';
       private $id  = 0;

       function test() {
             echo $this->$this->variable_name;  // ERROR!!!!
             echo $this->{$this->variable_name};
       }
}

$object = new test_class();

$object->test();

Best regards!

No comments:

Post a Comment