Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: object is NULL right after creation

Tags:

php

We have very strange errors occasionally popping up in our php logs: Trying to get property of non-object.

This exact error seems to be caused by the access to the member $shortName in the following if statement:

class MyLocaleWrapper extends SomeOtherClass {
    …
    protected static $system = NULL;
    public static function getSystemLocale() {
        if (self::$system === NULL) {
            self::$system = new self();
            debug(self::$system);
            self::$system->rfcName = SYSTEM_LOCALE_RFCNAME;
            self::$system->shortName = strtolower(Locale::getRegion(self::$system->rfcName));
            if (self::$system->shortName == '') {
                self::$system->shortName = strtolower(self::$system->rfcName);
            }
            …

# in another file:
class SomeOtherClass {
    …
    public function __construct() {
        # Some documentation about features that have been
        # removed from the constructor, but no real code in here.
        return NULL;
    }
    …

# in yet another file:
MyLocaleWrapper::getSystemLocale();

if I dump self::$system into a log file, I see that it is NULL - right after being constructed with the keyword new.

The most interesting part is that this file is included in each and every request to our page, so it gets executed ~ 10 times per second. But occasionally it just fails without anyone touching the code (or even the server).

Has anyone else ever experienced such behavior in PHP?

like image 567
soulmerge Avatar asked Oct 21 '10 16:10

soulmerge


2 Answers

Thanks for all the updates. (see extensive comments above on original post).

Unfortunately, I'm as stumped as you are -- everything looks fine with that code.

Either

  1. you've found some rather obscure bug in php, or...
  2. the symptoms are tricking you into thinking the problem is in one place, when it's actually somewhere else, or...
  3. we're all missing something in that code that should be obvious to a bunch of seasoned php developers. ;-)

If it were my production system to deal with, I'd probably comment out the return NULL in the constructor, and let it run in production for a while. That return shouldn't cause any problems, but it's the only strange thing I can see here.

Sorry I can't help more than that. Please come back and let us know if you figure it out.

like image 173
Lee Avatar answered Oct 17 '22 14:10

Lee


We finally found out we were running into php bug #50027. After setting the php.ini-variable zend.enable_gc to false, the error vanished.

like image 34
soulmerge Avatar answered Oct 17 '22 14:10

soulmerge