Parse error: syntax error, unexpected 'new' (T_NEW) in .../test6_2.php on line 20
The error message you are encountering is unambiguous: PHP has encountered a token it did not expect at line 20. The specific token flagged is T_NEW. In the provided source code snippet from PHPBench.com, the offending line is:
$obj =& new SomeClass();
This specific syntax is invalid in modern PHP versions (5.4 and later). The issue stems from a misunderstanding of how assignment operators interact with the new keyword regarding variable variables or references in this context. You cannot use the short assignment operator (=&) directly immediately after the new keyword to create a reference to an object instance in the way one might with a simple variable.
The line attempts to do two things simultaneously: instantiate a new instance of SomeClass and assign it to the variable $obj by reference. While creating references to objects was a common pattern in older PHP versions (pre-5.4) or specific contexts, the syntax $var =& new ClassName() is deprecated and effectively removed from the language specification because it behaves inconsistently with how references are resolved.
How the Code Fails
When the parser reaches new SomeClass(), it creates an object instance. Immediately after, it expects a standard assignment operator (=) or the end of the statement. The sequence =& implies "assign by reference." In PHP 5.4+, the language engine no longer supports assigning a reference to the result of a constructor call directly using this syntax.
If you attempt to run this code in a modern environment, the parser throws a fatal error because it sees the = operator followed by an ampersand (&) where only a simple assignment was allowed. The comment in the original source suggests a desire to create a reference so that subsequent calls could look like new $obj() or similar manipulations, but this is not how object instantiation works. You cannot instantiate an object from a variable reference using new unless that variable is a string containing the class name (which is a different syntax entirely).
The Correct Approach to References
If your goal was simply to instantiate multiple objects and store them in a single variable for later use, standard assignment is sufficient. In PHP, object variables are already passed by reference by default when used as arguments or stored in arrays. There is rarely a need to explicitly declare a reference for an object instance unless you are dealing with specific low-level extension requirements or legacy compatibility code.
To fix the syntax error and achieve the intended behavior of instantiating 1000 objects, use standard assignment:
class SomeClass {
function f() {
// Implementation
}
}
function Test6_2() {
$i = 0;
global $i; // Assuming $i is shared scope per original intent
$t = microtime(true);
while($i < 1000) {
// Standard assignment creates a new instance
$obj = new SomeClass();
// If you need to store this for later, push it to an array
// $instances[] = $obj;
++$i;
}
usleep(100);
return (microtime(true) - $t);
}
If the original author intended to create a reference specifically to manipulate the object pointer later, you must assign the object first, then apply the & operator if necessary:
$obj = new SomeClass();
// Only do this if you strictly need a reference for a specific reason
$ref = &$obj;
However, in 99% of benchmarking or general application scenarios, explicit reference declaration for object instances is unnecessary clutter. The &= syntax was likely an artifact of very old PHP practices that the language evolution removed to enforce cleaner memory management and prevent ambiguity.
Benchmarking Considerations
The source material also includes a usleep(100) call. This is a critical detail for micro-benchmarking in PHP. Without a non-zero sleep, the script execution time might be so negligible (microseconds or less) that the operating system's scheduler rounds it down to 0, resulting in inaccurate performance data. The usleep ensures a measurable delta between the start and end timestamps.
When analyzing the performance of object instantiation, remember that creating thousands of objects in a loop is generally expensive. If this code is part of a larger application, consider pooling objects or using dependency injection frameworks to avoid repeated instantiation overhead. In the Laravel ecosystem, for instance, service containers handle object creation efficiently, and you rarely need to manually instantiate classes inside tight loops unless profiling specifically for memory allocation costs.
The fix is straightforward: remove the & from the assignment operator on line 20. The code will then parse correctly, instantiate the objects as intended, and provide valid timing data without throwing a syntax error.