PHP 5.3.2: Deprecated method of instantiating an o

2019-09-06 11:33发布

问题:

Getting a white screen of death, so decided to remote debug an application that I suspect is instantiating an object using a now unsupported method:

$type['content_object'] = new $type['handler_class']();

Is this still legitimate?

回答1:

Assuming $type['handler_class'] is a string containing the name of a class, then it's fine, according to the manual:

If a string containing the name of a class is used with new, a new instance of that class will be created.

<?php
    $instance = new SimpleClass();

    // This can also be done with a variable:
    $className = 'Foo';
    $instance = new $className(); // Foo()
?>