I’m just curious as to if creating an object on the fly is possible in PHP. I thought I’d seen it done before. Of course I can just assign it to a variable but just wondering if this possible.
new className()->someMethod();
Of course this throws a syntax error, so obviously it’s not done like that (if it’s even possible).
Should I just assign it to a variable, as I really have no problems with doing that I was just curious?
Just some further details. Static methods aren’t really an option as the class I was trying to do this for was PHPs ReflectionMethod class.
This only works if you are using a singleton-pattern for instanciating the object. If you are not aware of how to implement the singleton-pattern, you’ll have to search around the web. But this way it would work:
EDIT
As stated by zerkms a factory-method would also be possible: