Jump to content

[SOLVED] OOP include modules


MasterACE14

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/162371-solved-oop-include-modules/
Share on other sites

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?

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();

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();

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?

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.

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! :D

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.