2

Is there any possibility to create a new object by a string that is returned by a function or method in PHP? Like some escape method I do not know?

Non-working example:

new ($class->method())();
new "stringliteral"();
new ($class = $class->method())();
5
  • Why would you need to use a string literal? Why write new "stringliteral"() instead of new stringliteral()? Commented Oct 23, 2015 at 9:48
  • 2
    See php.net/manual/en/language.namespaces.dynamic.php for the ways to work with class and namespace names dynamically. It looks like they have to be in variables. Commented Oct 23, 2015 at 9:50
  • Why do you have to do it directly? Is your server running low on variables? Commented Oct 23, 2015 at 10:08
  • I do not have to do it directly. The question has a more 'informative' character. Commented Oct 26, 2015 at 10:14
  • Would be nice if you'd accept Barmar's answer: stackoverflow.com/a/33299656/1402176 Commented Oct 12, 2018 at 8:42

3 Answers 3

6

You can do it using ReflectionClass

function makeRef($className) {
    return new ReflectionClass($className);
}
$obj = makeRef($class->method())->newInstance();

In PHP 5.4+ you can turn this into a one-liner:

$obj = (new ReflectionClass($class->method()))->newInstance();
Sign up to request clarification or add additional context in comments.

4 Comments

Or if you prefer one-liners $object = (new ReflectionClass($class->method()))->newInstance();. I don't see why one wouldn't just use a variable tho?
@toster-cx I tried doing that but got a parse error. Maybe because I'm using PHP 5.3, I suspect it works in PHP 5.4.
Nice thought! Forgot Reflection. Thanks.
Although I do not know the syntax you are using. But since I use a static method now, I can use: (new ReflectionMethod('class', 'method'))->invoke(null, 'param1')
0

Do you want dynamic class creation? here is a read : http://docstore.mik.ua/orelly/webprog/pcook/ch07_13.htm

While it is OK to invoke dynamic function, doing it for class seems to be a stunt.

1 Comment

no. my question is, if it is possible to create a class directly from a string literal or return value without putting it into a variable before.
-1

Yes it is possible. Assign the string to a variable then create object using that variable.

Ex.

<?php
class stringliteral{
  function __construct()
  {
    echo"called";
  }
}
function dumyFunc()
{
  return 'stringliteral';
}
$str=dumyFunc();
if(class_exists($str))
$obj=new $str();
?>

10 Comments

Please read the heading - "directly". I know that I can use a variable.
Errrr Fatal error: Class 'stringliteral' not found Think before you jump new instantiates an existing class!!
Now it will solve your requirements. check it once again
This doesn't answer the question.
@ManiMuthuPandi: I suggest you delete your answer, because it absolutely doesn't answer my question.
|

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.