1

lets say I have a class named 'translations'. I have an instance of the class called 'chinese_translations'

$chinese_translations = new translations;

Afterwards, I want to call this instance from a dynamically built string and I have tried several ways:

$part1 = 'chinese_';
$part2 = 'translations';
$instance_of_translations = ${$part1.$part2};
$instance_of_translations->getMsg();//Doesn't work

Also like this:

$part1 = 'chinese_';
$part2 = 'translations';
$instance_of_translations = $part1.$part2;
$$instance_of_translations->getMsg();//Doesn't work

I always get the "Call to a member function getMsg() on a non-object" message. What I'm doing wrong?

*SOME ACTIONS TAKEN AND RESULTS OBTAINED:

//Lets see if the var is in scope:

echo $chinese_translations->getMsg(get_locale());//It works

$instance_of_translations = ${$part1.$part2};//Let's try to build the name dynamically

echo $chinese_translations->getMsg(get_locale()); //Call to a member function getMsg() on a non-object

echo $$chinese_translations->getMsg(get_locale()); //Object of class internal_message could not be converted to string in

var_dump($instance_of_translations);//It throws the following:

//object(internal_message)#1956 (1) { ["message"]=> array(2) { ["es_ES"]=> string(19) "The expected result" ["it_IT"]=> string(19) "The expected result" } }NULL
2
  • 2
    Your first example is working on my computer. Are you sure that the $chinese_translations is in the same scope like the part where you are calling getMsg()? Commented Feb 1, 2016 at 12:24
  • You were right, there was an scope issue!! Commented Feb 1, 2016 at 16:17

3 Answers 3

2

$part1.$part2 is a string. Therefore, $instance_of_translations is the variable name, not the variable itself.

Try this:

$part1 = 'chinese_';
$part2 = 'translations';
$varName = $part1.$part2;
$instance_of_translations = $$varName;
var_dump($instance_of_translations);

It should reveal an object of type translations.

Read more about variable variables.

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

3 Comments

This way I get the following output: "Class name must be a valid object or a string"
Both versions of your code work, mine works too. 3v4l.org/QhHmP Maybe you have a variable scope issue (f.e. $instance_of_translations is a global variable and you try to access it in a function or method.)
There was an scope issue, sorry for your waste of time and thanks for your help!
1

try this:

$className = $part1.$part2;
$instance = new $$className;
$instance->getMsg();

Comments

0

Finally the bracket version did the trick, but there was an scope issue so both answer maybe right. This one:

 $part1 = 'chinese_';
 $part2 = 'translations';
 $instance_of_translations = ${$part1.$part2};
 $instance_of_translations->getMsg();

Is currently working for me.

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.