One more try at this. I’ve already had my previous question down voted.
I am loading a class through a class that is never instantiated via a static method. The class I am loading however is supposed to be instantiated but I suspect that execution never gets that far. There is nothing static in this class, but php keeps throwing:
**Fatal error: Using $this when not in object context**
Here is the class in it’s entirety:
class Config{
public $_config;
public function load()
{
$modules = array();
$modules = scandir(MOD);
$n = count($modules);
for($i=0;$i<$n;$i++)
{
if($modules[$i]==='.'||$modules[$i]==='..'||strpos($modules[$i],'.'))
{ unset($modules[$i]); }
}
$modules = array_values($modules);
$m = count($modules);
for($i=0;$i<$m;$i++)
{
$mod = $modules[$i];
$this->_config[$mod] = array();// **Error thrown here.**
$cfgfiles = scandir(MOD.DS.$mod.DS.'config');
$num = count($cfgfiles);
for($i=0;$i<$num;$i++)
{
if($cfgfiles[$i]==='.'||$cfgfiles[$i]==='..')
{ unset($cfgfiles[$i]); }
}
$cfgfiles = array_values($cfgfiles);
$k = count($cfgfiles);
for($j=0;$j<$k;$j++)
{
include(MOD.DS.$mod.DS.'config'.DS.$cfgfiles[$j]);
$modcfg = substr($cfgfiles[$j], 0, -4);
$this->_config[$mod][$modcfg] = array();
$this->_config[$mod][$modcfg] = $cfg;
}
}
return $this->_config;
}
}
More information outlining my problem can be found here:
Ok, as requested, here is the code that calls this class and yes, it is a static function. This is what I was wondering about, but the fact that the function in the other class was static did not seem to have any effect on the loading of other classes, only this one.
public static function init()
{
$farray = scandir(SYS);
for ($i=0;$i<count($farray);$i++)
{ if(strpos($farray[$i],'.php'))
{
$item = $farray[$i];
require (SYS.DS.$item);
$className = substr($item, 0, -4);
self::$_names[] = $className;
$obj = new $className();
self::$_objs[] = $obj;
self::$_classes = array_combine(self::$_names, self::$_objs);
}
}
}
public static function getClass($class)
{
if(isset(self::$_classes[$class]))
{ return self::$_classes[$class]; }
else
{ return false; }
}
Ok, I added a debug_backtrace() at the line just before the error($this->_config[$mod]=array()) and this is what I get, but I do not understand what it means.
array(2)
{ [0]=> array(6)
{ ["file"]=> string(25) "C:\xampp\htdocs\framework\fw.php"
["line"]=> int(45)
["function"]=> string(4) "load"
["class"]=> string(6) "Config"
["type"]=> string(2) "::"
["args"]=> array(0) { } }
[1]=> array(6)
{ ["file"]=> string(26) "C:\xampp\htdocs\index.php"
["line"]=> int(31)
["function"]=> string(4) "init"
["class"]=> string(2) "Fw"
["type"]=> string(2) "::"
["args"]=> array(0) { } } }
Ok, I’ve done everyone who responded to this question a dis-service. It turns out that I was indeed inadvertently making a static method call to Config::load in Fw::init. Because I had made so many revisions and worked on it until I was bleary eyed I unknowingly ended up with two different version of the same class, one on my server and one in my editor.
I am now going off to learn how to use a source control application.
The most likely thing going on here is that you’re calling the
Config::load()method statically. This type of code will result in the error you quoted in your question:You can fix it by instantiating an object and calling the
load()method on the instance. Like this: