PrepAway - Latest Free Exam Questions & Answers

what statement is correct?

Given the following two functions, what statement is correct?
<code>
functiondynamicNew($name) {
returnnew $name;
}
functionreflectionNew($name) {
$r = new ReflectionClass($name);
return$r->newInstanceArgs();
}
</code>

PrepAway - Latest Free Exam Questions & Answers

A.
Both functions do the same

B.
dynamicNew() results in a parse error, reflectionNew() works

2 Comments on “what statement is correct?

  1. Walter Neto says:

    With php5.6, the script

    newInstanceArgs();
    }

    var_dump(dynamicNew(“A”));

    var_dump(reflectionNew(“A”));

    ?>

    outputs:

    object(A)#1 (1) {
    [“n”]=>
    int(10)
    }
    object(A)#2 (1) {
    [“n”]=>
    int(10)
    }

    so I’d say “Both functions do the same”, am I wrong?




    0



    0
  2. Walter Neto says:

    With php5.6, the script


    error_reporting(E_ALL);

    class A
    {
    public $n = 10;
    }

    function dynamicNew($name) {
    return new $name;
    }

    function reflectionNew($name) {
    $r = new ReflectionClass($name);
    return $r->newInstanceArgs();
    }

    var_dump(dynamicNew(“A”));

    var_dump(reflectionNew(“A”));

    outputs:

    object(A)#1 (1) {
    [“n”]=>
    int(10)
    }
    object(A)#2 (1) {
    [“n”]=>
    int(10)
    }

    so I’d say “Both functions do the same”, am I wrong?




    0



    0

Leave a Reply

Your email address will not be published. Required fields are marked *