MasterACE14 Posted June 16, 2009 Share Posted June 16, 2009 I haven't done OOP in awhile, and moving 2 working functions into a class isn't working correctly. The 2 functions when not in the class work fine, and the page displays... Hello World When the 2 functions are methods within a class, the page displays nothing. here's the class. <?php class Modules { public $dirname; // Select Modules function selectModules() { $this->dirname = GETPATH_MODULES; $dir = opendir($this->dirname); $file_list = ''; while(false !== ($file = readdir($dir))) { if(($file != ".") and ($file != "..")) { if(is_dir($this->dirname . '/' . $file)) { $file_list .= $this->selectModules($this->dirname . DS . $file); } if(substr($file,"-4") == ".php") { if($file != "loader.php") { $file_list .= $this->dirname.DS.$file.","; $file_list = str_replace("/",DS,$file_list); } } else { // all other file formats } } } closedir($dir); return $file_list; } // Load Modules function loadModules() { $modules = selectModules($this->dirname); $modules = explode(",",$modules); $modulesCount = (count($modules)-1); for($i=0;$i<$modulesCount;$i++) { require_once($modules[$i]); } return true; } } $modules = new Modules(); $modules->selectModules(); $modules->loadModules(); ?> any help is greatly appreciated. Thanks, Ace Quote Link to comment https://forums.phpfreaks.com/topic/162371-solved-oop-include-modules/ Share on other sites More sharing options...
Zane Posted June 16, 2009 Share Posted June 16, 2009 you have to echo/print something in order for it to display....probably why it displays nothing Quote Link to comment https://forums.phpfreaks.com/topic/162371-solved-oop-include-modules/#findComment-856995 Share on other sites More sharing options...
trq Posted June 16, 2009 Share Posted June 16, 2009 You never set the dirname property to anything. Quote Link to comment https://forums.phpfreaks.com/topic/162371-solved-oop-include-modules/#findComment-857023 Share on other sites More sharing options...
trq Posted June 16, 2009 Share Posted June 16, 2009 Sorry, ignore me. Quote Link to comment https://forums.phpfreaks.com/topic/162371-solved-oop-include-modules/#findComment-857024 Share on other sites More sharing options...
MasterACE14 Posted June 16, 2009 Author Share Posted June 16, 2009 GETPATH_MODULES is defined before the class, and I'm using it within the class, can that be done? or does it need to be defined within the class? and would $this->dirname used within loadModules() work? or do I need to pass that to loadModules() through a argument? Quote Link to comment https://forums.phpfreaks.com/topic/162371-solved-oop-include-modules/#findComment-857030 Share on other sites More sharing options...
trq Posted June 16, 2009 Share Posted June 16, 2009 Constants are global so yes it can be done. As I said, ignore me. You'll need to throw some debug statements in your code to try and track down where it is failing. Quote Link to comment https://forums.phpfreaks.com/topic/162371-solved-oop-include-modules/#findComment-857032 Share on other sites More sharing options...
trq Posted June 16, 2009 Share Posted June 16, 2009 Actually, looking at your code, its a little all over the place. Firstly, you should probably make this assignment ($this->dirname = GETPATH_MODULES;) within a __construct. Next thing, your loadModules() method call selectModules incorrectly, it should be.... $modules = $this->selectModules(); Quote Link to comment https://forums.phpfreaks.com/topic/162371-solved-oop-include-modules/#findComment-857035 Share on other sites More sharing options...
MasterACE14 Posted June 16, 2009 Author Share Posted June 16, 2009 updated code... class Modules { public $dirname; function __construct() { $this->dirname = GETPATH_MODULES; } // Select Modules function selectModules() { $dir = opendir($this->dirname); $file_list = ''; while(false !== ($file = readdir($dir))) { if(($file != ".") and ($file != "..")) { if(is_dir($this->dirname . '/' . $file)) { $file_list .= $this->selectModules($this->dirname . DS . $file); } if(substr($file,"-4") == ".php") { if($file != "loader.php") { $file_list .= $this->dirname.DS.$file.","; $file_list = str_replace("/",DS,$file_list); } } else { // all other file formats } } } closedir($dir); return $file_list; } // Load Modules function loadModules() { $modules = $this->selectModules(); $modules = explode(",",$modules); $modulesCount = (count($modules)-1); for($i=0;$i<$modulesCount;$i++) { require_once($modules[$i]); } return true; } } $modules = new Modules(); $modules->selectModules(); $modules->loadModules(); Quote Link to comment https://forums.phpfreaks.com/topic/162371-solved-oop-include-modules/#findComment-857036 Share on other sites More sharing options...
trq Posted June 16, 2009 Share Posted June 16, 2009 And? Have you tried any debuging? Quote Link to comment https://forums.phpfreaks.com/topic/162371-solved-oop-include-modules/#findComment-857038 Share on other sites More sharing options...
MasterACE14 Posted June 16, 2009 Author Share Posted June 16, 2009 I've tried echo'ing and dieing at different parts in and out of the class. if I have... echo GETPATH_MODULES; // load modules /* $modules = new Modules(); $modules->selectModules(); $modules->loadModules(); */ ...with the class call commented out it displays the file path held in GETPATH_MODULES but if I uncomment the class call, the page is blank. Even though GETPATH_MODULES is before the class call? Quote Link to comment https://forums.phpfreaks.com/topic/162371-solved-oop-include-modules/#findComment-857042 Share on other sites More sharing options...
trq Posted June 16, 2009 Share Posted June 16, 2009 You are defining that constant using define? Have you got error reporting set sufficiently to see errors? Quote Link to comment https://forums.phpfreaks.com/topic/162371-solved-oop-include-modules/#findComment-857045 Share on other sites More sharing options...
MasterACE14 Posted June 16, 2009 Author Share Posted June 16, 2009 yep using define() and error reporting is set to E_ALL if I do... include GETPATH_MODULES."\helloworld\helloworld.php"; // load modules //$modules = new Modules(); //$modules->selectModules(); //$modules->loadModules(); it's working fine, don't know why it's failing with the module class. Very strange. Quote Link to comment https://forums.phpfreaks.com/topic/162371-solved-oop-include-modules/#findComment-857046 Share on other sites More sharing options...
MasterACE14 Posted June 16, 2009 Author Share Posted June 16, 2009 I've found the problem why the class wasn't working. I was passing a argument in selectModules(); when it was being called within itself. When selectModules() didn't require an argument. After adding an argument to selectModules() it is now throwing warnings instead of nothing. Hello World Warning: readdir(): supplied argument is not a valid Directory resource in C:\wamp\www\GETenginebeta\modules\loader.php on line 28 Warning: closedir(): supplied argument is not a valid Directory resource in C:\wamp\www\GETenginebeta\modules\loader.php on line 44 Warning: readdir(): supplied argument is not a valid Directory resource in C:\wamp\www\GETenginebeta\modules\loader.php on line 28 Warning: closedir(): supplied argument is not a valid Directory resource in C:\wamp\www\GETenginebeta\modules\loader.php on line 44 current code: class Modules { public $dirname; function __construct() { $this->dirname = GETPATH_MODULES; } // Select Modules function selectModules($dirname=null) { $this->dirname = $dirname; $dir = opendir($this->dirname); $file_list = ''; while(false !== ($file = readdir($dir))) { if(($file != ".") and ($file != "..")) { if(is_dir($this->dirname . '/' . $file)) { $file_list .= $this->selectModules($this->dirname . DS . $file); } if(substr($file,"-4") == ".php") { if($file != "loader.php") { $file_list .= $this->dirname.DS.$file.","; $file_list = str_replace("/",DS,$file_list); } } else { // all other file formats } } } closedir($dir); return $file_list; } // Load Modules function loadModules() { $modules = $this->selectModules(); $modules = explode(",",$modules); $modulesCount = (count($modules)-1); for($i=0;$i<$modulesCount;$i++) { require_once($modules[$i]); } return true; } } include GETPATH_MODULES."\helloworld\helloworld.php"; // load modules $modules = new Modules(); $modules->selectModules(); $modules->loadModules(); ---------------------- EDIT --------------------------------- I got it working now. added this to the start of selectModules(); and the class is now working correctly. if(!isset($dirname)) { $this->dirname = GETPATH_MODULES; } else { $this->dirname = $dirname; } Thanks for all your help everyone! Quote Link to comment https://forums.phpfreaks.com/topic/162371-solved-oop-include-modules/#findComment-857064 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.