Spurious PHP undeclared static property

爷,独闯天下 提交于 2019-12-11 06:25:13

问题


Running Debian Squeeze servers with PHP 5.3.x (happens with both Apache and nginx) I'm seeing random occurrences of the fatal error undeclared static property. Often this refers to the following code:

class aliro extends aliroBase {
        private static $instance = null;

        public static function getInstance () {
            return self::$instance instanceof self ? self::$instance : (self::$instance = new self());
        }
...

and states that aliro::$instance is undeclared on the last line shown above (not counting the close curly bracket). Yet the variable is quite plainly declared just a couple of lines earlier. (The exact error message is "PHP Fatal error: Access to undeclared static property: aliro::$instance in /var/www/apic/public_html/aliro.php on line 91").

This code is executed for every request to the server, which is currently running about 1,250 requests per hour. But the error appears to occur once an hour.

Simultaneously, syslog may show a suhosin error message:

Oct 22 11:29:45 apic suhosin[20764]: ALERT-SIMULATION - 
canary mismatch on efree() - heap overflow detected at 0x2ec9118

or a kernel error, such as:

Oct 22 10:29:43 apic kernel: [83469.382141] php[9479] general protection 
ip:6a8161 sp:7fffa270ec20 error:0 in php5[400000+6f9000]

or

Oct 22 09:29:45 apic kernel: [79871.245018] php[705]: segfault at 27f2298 
ip 00007f24229e640e sp 00007fff13aca388 error 6 in 
libc-2.11.2.so[7f2422968000+158000]

Cron is running every five minutes to trigger munin-node, but the errors only occur once an hour. What could be causing this?

Being non-repeatable, it isn't clear what can be done, although the errors are creating a risk of data corruption. Any suggestions?


回答1:


This code isn't (at least shouldn't be the issue). I would check for other part of the code, where you might refer to aliro::$instance from outside aliro class. $instance being private would trigger your exact error thrown (undeclared static property) if you try to touch it outside of the class. I would change it to protected (I also hate private properties, they rarely NEED to be private instead of protected) - that would give better error message (that you are trying to access property that is not public), also I think it should give you the file and line it occures.




回答2:


I would check in the class being extended.

When it calls self::$instance = new self(), it will call the constructor of the base class.

I note that $instance is declared as private, so if the base class tries to do anything with that variable, it may throw an error.

Try changing $instance to protected instead of private; if that's the issue, doing that should resolve it.



来源:https://stackoverflow.com/questions/7861151/spurious-php-undeclared-static-property

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!