1

So I want to do something like this:

$ob = new $this->other_class_name;

but it fails. How can I do it without storing other_class_name in local variable?

2 Answers 2

2

Save the name in another variable:

$class = $this->other_class_name;
$ob = new $class;
Sign up to request clarification or add additional context in comments.

Comments

1

Try this:

$class = get_class($this->other_class_name);
$ob = new $class;

3 Comments

Isn't $this->other_class_name a string and not an object?
by the info given i thought it was an object of the type 'other_class_name' if it is a string it's even easier
Yes... but if you have already an object, you don't even need to create one from a string. You could use the clone method.

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.