Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

New by reference not giving warning

Tags:

php

reference

All over the web[1][2][3], it says that since PHP 5.0.0 "assigning the return value of new by reference" gives a E_DEPRECATED or E_STRICT depending on your php version (E_DEPRECATED didn't exist until 5.3, so it was E_STRICT before that).

As such it is my understanding that this code should give such a warning:

error_reporting(E_ALL | E_STRICT);

class A
{
}

$a =& new A();

However, I have tried this on two completely different servers (one running PHP 5.3 and one running PHP 5.2) and neither is actually giving any message! What's going on? Is my understanding incorrect or is something strange going on on those two servers?

(I do also think it is strange that this is deprecated, seeing that $a = null; $b =& $a; $b = new A(); does not do the same as $a = null; $b =& $a; $b =& new A();, but that's only a part of the question if I misunderstood what is deprecated...)

like image 574
Jasper Avatar asked Nov 06 '12 14:11

Jasper


1 Answers

In response to the OP, this comment pointed him in the right direction:

It wouldn't at all surprise me if the problem here lies elsewhere: try setting E_ALL | E_STRICT in your php.ini directly, don't forget to change the php-cli.ini, too, if you're running this code on the command line.
Also double check if the errors aren't hidden by doing an ini_set('display_errors',1);1. If you're running this on a windows box, there have been some bugs with this in the past.

Since the OP also pointed out that the warnings were generated before any code got executed, I had a hunch, that the expected warnings are being raised at compile-time, rather then runtime, so I had another look at the docs. There, I found this big red-box note, which confirmed my suspicions:

Most of E_STRICT errors are evaluated at the compile time thus such errors are not reported in the file where error_reporting is enhanced to include E_STRICT errors (and vice versa).

Since version 5 PHP is effectively a "compiled" language (similar to Java, the code is compiled to Zend Bytecode). When the Zend-engine compiles code with errors that are issued at compiled time, an in-script error_reporting call has no effect on weather or not these errors are reported: the error_reporting call applies only to the runtime errors/warnings.
Perhaps this: error_reporting(E_ALL | E_STRICT | E_COMPILE_ERROR); is worth a look, too

Bottom line:
Set the error reporting in the php.ini files whenever you can.

like image 133
Elias Van Ootegem Avatar answered Oct 04 '22 17:10

Elias Van Ootegem