Jump to content

[SOLVED] Using autoload with child classes?


maexus

Recommended Posts

I've written a little Loader class that has a custom autoloader class. (See below). This works great for base classes and have had no issues with child classes. I have run in to an issue though.

 

I have a Settings class and a child of that called Settings_INI. (See both below). When I try to run the following code, it uses the function from Settings rather than Settings_INI. When I comment out the function in Settings, it gives me the following error.

 

The code I'm trying to run:

 

<?php
require_once 'loader.php';

$settings = Settings_INI::init();
echo $settings->jawa_author;

?>

 

The error:

 

Fatal error: Call to undefined method Settings::load_and_map() in /Users/kyleetilley/htdocs/jawa/0.01/standard/settings.class.php on line 9

 

Here is "settings.class.php":

 

<?php
class Settings {
	public $settings = array();

	private static $instance = NULL;
	private function __clone(){}

	private function __construct(){
		$this->load_and_map('jawa');
		$this->load_and_map('project');
		$this->load_and_map('db');
	}

	static public function init(){
		if(is_null(self::$instance)){
			self::$instance = new self();
		}
		return self::$instance;
	}

	/*public function load_and_map($file){
		if(file_exists("settings/{$file}.ini")){
			$this->settings[$file] = parse_ini_file("settings/{$file}.ini");
		}else{
			echo "settings/{$file}.ini".' didnt load<br>';
		}

	}*/

	public function get($variable){
		$variable = explode('.', $variable);
		if(is_array($variable)){
			return (isset($this->settings[$variable[0]][$variable[1]])) ? $this->settings[$variable[0]][$variable[1]] : false;
		}else{
			return (isset($this->settings[$variable])) ? $this->settings[$variable] : false;
		}			
	}

	public function __get($variable){
		$variable = explode('_', $variable);
		if(is_array($variable)){
			return (isset($this->settings[$variable[0]][$variable[1]])) ? $this->settings[$variable[0]][$variable[1]] : false;
		}else{
			return (isset($this->settings[$variable])) ? $this->settings[$variable] : false;
		}
	}
}
?>

 

and "settings_ini.class.php":

 

<?php
class Settings_INI extends Settings {
	public function load_and_map($file){
		if(file_exists("settings/{$file}.ini")){
			$this->settings[$file] = parse_ini_file("settings/{$file}.ini");
		}else{
			echo "settings/{$file}.ini".' didnt load<br>';
		}

	}
}
?>

 

and finally, my autoloader class:

 

<?php
/*
Author: Kylee Tilley <[email protected]>, <http://kyleetilley.name>
*/

class Loader {
private $base_directory;
private $paths;

public function __construct(){
	$this->base_directory = dirname(__FILE__);
	$this->paths = array("standard", "project", "plugins");

	foreach($this->paths as $path){
		$files = glob("{$this->base_directory}/{$path}/*.inc.php");
		foreach($files as $file){
			if(file_exists($file)){
				require_once($file);
			}
		}	
	}
}

public function autoload($class){
	$class = strtolower($class); #may not need this
	foreach($this->paths as $path){
		$class_file = "{$this->base_directory}/{$path}/{$class}.class.php";

		if(file_exists($class_file)){
			require_once($class_file);
		}		
	}
}

}
$jawa_loader = new Loader();
spl_autoload_register(array($jawa_loader, 'autoload'));
?>

 

I'm just unsure why it's not seeing the function in the child class. Any help or suggestions? If anything is unclear, please let me know. I typed this up in a bit of a hurry.

I thought of that. I removed them from parent constructor method all together and put them directly to test.php:

 

<?php
require_once 'loader.php';

$settings = Settings_INI::init();
$settings->load_and_map('jawa');
$settings->load_and_map('project');
$settings->load_and_map('db');
echo $settings->jawa_author;

?>

 

and getting the error:

 

Fatal error: Call to undefined method Settings::load_and_map() in /Users/kyleetilley/htdocs/jawa/0.01/test.php on line 5

 

I imagine it's something small I'm missing, I just can't see it.

I'm working on a small framework for my projects. I include loader.php and that looks for any *.inc.php (if any) I have in either the folders "standard", "project" or "plugins". The autoloader method does that same for the objects I create.

 

Not sure what you mean by

 

Now that you have your problem fixed.. why so much code to replace

 

<?php
require_once( dirname(__FILE__) . "/path/to/script.php");
?>

 

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.