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 <kyleect@gmail.com>, <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.

Link to comment
Share on other sites

The error is in your Settings __construct() method.

 

$this->load_and_map('jawa');

$this->load_and_map('project');

$this->load_and_map('db');

 

load_and_map is defined in the child class, not the base class.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

 

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

 

I must be missing something. Care to elaborate what you're working on?

Link to comment
Share on other sites

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");
?>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.