Analysis error: syntax error, unexpected "new" (T_NEW) at ... / test 6_2.php on line 20

Just trying to save and fix sources from PHPBench.com

and click this error (the site does not work, and the author did not answer questions). This is the source:

<?php // Initial Configuration class SomeClass { function f() { } } $i = 0; //fix for Notice: Undefined variable i error // Test Source function Test6_2() { //global $aHash; //we don't need that in this test global $i; //fix for Notice: Undefined variable i error /* The Test */ $t = microtime(true); while($i < 1000) { $obj =& new SomeClass(); ++$i; } usleep(100); //sleep or you'll return 0 microseconds at every run!!! return (microtime(true) - $t); } ?> 

Is this syntax or not? Correct me if I am wrong, but I think that it creates a link to SomeClass, so we can call new $obj() ... Thanks in advance for your help

+6
source share
1 answer

Objects are always stored by reference in any case. You don't need =& , and, as Charlotte commented, this is deprecated syntax.

Correct me if I am wrong, but I think that it creates a link to SomeClass, so we can call the new $ obj ().

No, this is not true. The new operator always creates an instance of the class, not a reference to the class as a type.

You can instantiate a variable object simply by creating a string variable with the class name and using this.

 $class = "MyClass"; $obj = new $class(); 

Functions such as get_class () or ReflectionClass :: getName () returns the class name as a string. In PHP there is no concept of a “class reference”, as in Java.

The closest thing you think about is ReflectionClass :: newInstance () , but this is an optional way to dynamically create an object. In almost all cases, it is better to use new $class() .

+5
source

All Articles