2

i want to turn a simple string like that "response->dict->words" into a variable name that i can actually work with. I will give an example now. Lets assume the value of $response->dict->words is 67.

Example:

$var = "response->dict->words"

echo $$var; /* PRINT THE VALUE 67 FROM $response->dict->words*/

As you may notice i put an extra dollar sign before the $var because this should work, but it doesn't.

Can anyone help me with this?

1
  • 3
    Here you should use eval() instead of variable variables or something like that, but you'd better restructure your logic. There should be no need to make a string -> object call. Commented Jul 25, 2014 at 14:07

2 Answers 2

5
class ClassOne {
    public function test() {
        return 'test';
    }
}

class ClassTwo {
    public function test2() {
        return 'test2';
    }
}

$one = new ClassOne();
$two = new ClassTwo();

$objects = array('one', 'two');
$methods = array('test', 'test2');

for ($i = 0; $i < count($objects); $i++) {
    echo ${$objects[$i]}->$methods[$i]();
}

You can store classnames or method names as strings and later use them, or even store variable names, like here ${$objects} (variable variables), but you cannot store whole logic.

To evaluate whole logic, you have to use eval(), which is most probably bad idea

$var = "response->dict->words"
eval("?> <?php echo $".$var.";");

You can split your string and make the call as below:

class Response {
    public $dict;

    public function __construct() {
        $this->dict = new stdClass();
        $this->dict->words = 'words test';
    }
}

$response = new Response();

$var = 'response->dict->words';

$elements = explode('->', $var);

echo ${$elements[0]}->$elements[1]->$elements[2];

Results into words test

Or, if you don't know the level of nesting the object call, you can perform the call in a foreach loop. When the loop exits, the last call will be available after it:

class Response {
    public $dict;

    public function __construct() {
        $this->dict = new stdClass();
        $this->dict->words = new stdClass();
        $this->dict->words->final = 'test chained string';
    }
}

$response = new Response();

$var = 'response->dict->words->final';

$elements = explode('->', $var);

foreach ($elements as $key => $element) {
    if ($key == 0) {
        $call = ${$element};
        continue;
    }
    $call = $call->$element;
}

echo $call;

Results into: test chained string

Sign up to request clarification or add additional context in comments.

1 Comment

i still didnt get the use of $call = $call->$element
-4

There is a better way, why don't you cache the variable like

     $var = $response->dict->words;

1 Comment

because he does not have a variable, but a string

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.