3

I have following setup.

index.php

   require_once "common.php";
   ...

common.php

   ...
   $obj = new MyClass;
   require_once "config.php"
   ...

config.php

   ...
   require_once "settings.php";
   ...

settings.php

   $obj->dostuff = true;
   ...

When i open index.php i get: Strict Standards: Creating default object from empty value in settings.php on 3

If i put $obj->dostuff = true; inside config.php it does not produce error message.

Can someone explain why i get this error? I am not asking how to fix it just understand why.

EDIT: My bad i had 2 config.php classes for each part of site and i only changed something in one of them leaving old include order in another now it works fine after it all loads in correct order.

6
  • What are the contents of obj->dostuff? Commented Nov 1, 2012 at 20:56
  • what is in settings.php line 3? Commented Nov 1, 2012 at 20:57
  • My bad it was not function() it was this on line 3 of settings.php $obj->dostuff = true; Commented Nov 1, 2012 at 20:58
  • is there anything in MyClass Commented Nov 1, 2012 at 21:01
  • 1
    I need to put $obj->dostuff = true; into settings.php that's the goal. in config.php wherever i place it i dont see strict standards error. Commented Nov 1, 2012 at 21:12

2 Answers 2

2

It looks a scope issue. In settings.php, the $obj is not accessible. PHP is creating new one from standard class, and giving you a warning. You can confirm it by putting

echo get_class($obj);

in Your settings.php, just after the line that is producing the error. If it echos "StdClass", then that is the case.

Are You sure the $obj is not created within a function/method ?

Sign up to request clarification or add additional context in comments.

1 Comment

$obj is created like i said above with 1 line that run on script load
0

If $obj is meant to be a system wide globally accessible object, you can you use the singleton pattern to access from anywhere:

class MyClass
{
    protected static $_instance;

    static function getInstance()
    {
        if (null === self::$_instance) {
            self::$_instance = new self();
        }
        return self::$_instance;
    }
}

You can then create your methods in this class. To get the object itself simply call:

$obj = MyClass::getInstance();

Additionally, if you just want to call one of its methods but theres no need to return anything:

MyClass::getInstance()->objectMethod();

I find this to be a very efficient way to organize integral singleton based system wide operations.

In practice, my project uses this to get configuration from anywhere in the system:

class syConfig
{
    protected static $_instance;

    private $_config;

    static function getInstance()
    {
        if (null === self::$_instance) {
            self::$_instance = new self();
        }
        return self::$_instance;
    }

    public function load($xmlString)
    {
        $xml = simplexml_load_string($xmlString);
        $this->_config = $xml;
    }

    public function getConfig()
    {
        return $this->_config;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.