php - Is instantiating a class using `new $className()` an issue by any means? Should I use it instead of `ReflectionClass::newInstance`? -


there few times, when dealing service container, have instantiate class, full class name configuration file. example, symfony container:

myservice:     class: "vendor\namespace\classname"     arguments: [...] 

now, inside container, i'm left choice: can either instantiate class using following snippet, makes use of php strange feature evaluating class name in runtime:

$service = new $classname(...$evaluatedarguments); 

or can instantiate using reflection:

$reflectionclass = new \reflectionclass($classname); $service = $reflectionclass->newinstance($evaluatedarguments); 

the latter more clear on it's doing, , is, @ moment, preferred method. however, since $classname not user input (is loaded .yaml file works app configuration file), can't find reason not use first snippet other readability.

it looks sketchy, can't think of technical/security reasons not use it, , does save memory (i don't have instantiate \reflectionclass) , far less verbose.

my question: using new $classname issue means?

disclaimer, because know people offtopic: i'm not building service container. not advice me use pimple or other dic instead of addressing question, example didatic purposes.

new $classname() officially supported php syntax instantiating class variable name. period. works. there no caveats. there's no reason invoke overhead of reflectionclass simple object instantiation.


Comments

Popular posts from this blog

javascript - Karma not able to start PhantomJS on Windows - Error: spawn UNKNOWN -

c# - Display ASPX Popup control in RowDeleteing Event (ASPX Gridview) -

Nuget pack csproj using nuspec -