Infected.Shadow Posted December 1, 2009 Share Posted December 1, 2009 I'm currently in the process of coding a module sys for my site. So far I have it detecting and loading everything perfectly. Though I've recently hit a brick wall. <?php class module { var $moduleList; var $functionList; var $modCount; var $path = 'modules/'; function module() { $this->loadModules(); } // emd module()) private function loadModules() { $handle = @opendir($this->path) or die('Unable to open '.$this->path); $this->modCount = 0; while($file = readdir($handle)) { // !!!!! Why is this working? !!!!! // !is_dir($file) is detecting directories when it should not be... // further investigation needed later. Perhaps a better way to check. // ***slight fix by reverting back to just single file modules instead of modules in directories if(!is_dir($file)) { $temp = explode('.', $file); $this->moduleList[$this->modCount] = $temp[0]; $this->modCount++; } } closedir($handle); } // end loadModule()) // This function is for testing purposes only. To be moved to administrator panel in the future... public function listModules() { echo '<strong>'.$this->modCount.' modules loaded...</strong><br />'; for($i = 0; $i < $this->modCount; $i++) { echo $this->moduleList[$i].'<br />'; } } // end listModules() public function getModCount() { return $this->modCount; } // end getModCount() // *** Clean this up in the future public function load($module) { include($this->path.$this->moduleList[$module].'.php'); $l = new ${$this->moduleList[$module]()}; //doesn't work $k = new test(); //works (temp testing) } // end load() } // end class module ?> My problem is in the load() function. $k works fine in creating the object, where as $l is producing: Fatal error: Call to undefined function test() in C:\www\htdocs\modulesys\module.php on line 60 I'm trying to make it so that you only need to create the object and the rest is handled in the constructor of the class. I've tested variable variables with calling functions and such, and don't see why it shouldn't work here. Am I wrong in this thinking or did I just happen to mess up some where? edit: In case it helps, here are the other two files... index.php <?php include('module.php'); $m = new module(); $m->listModules(); //implement module on/off later for($i = 0; $i < $m->getModCount(); $i++) { $m->load($i); } ?> modules/test.php <?php class test extends module { function test() { $this->display(); } function display() { echo 'Test!'; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/183602-variable-variables-and-objects/ Share on other sites More sharing options...
premiso Posted December 1, 2009 Share Posted December 1, 2009 $l = new $this->moduleList[$module](); That will work, basically using the { } wanted to look for a function, doing it like above looks for a class. Quote Link to comment https://forums.phpfreaks.com/topic/183602-variable-variables-and-objects/#findComment-969081 Share on other sites More sharing options...
Infected.Shadow Posted December 1, 2009 Author Share Posted December 1, 2009 $l = new $this->moduleList[$module](); That will work, basically using the { } wanted to look for a function, doing it like above looks for a class. Ah thank you so much. I'm still kind of new to variable variables with how they work at times. Much thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/183602-variable-variables-and-objects/#findComment-969084 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.