maexus Posted June 9, 2008 Share Posted June 9, 2008 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. Link to comment https://forums.phpfreaks.com/topic/109349-solved-using-autoload-with-child-classes/ Share on other sites More sharing options...
keeB Posted June 9, 2008 Share Posted June 9, 2008 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 https://forums.phpfreaks.com/topic/109349-solved-using-autoload-with-child-classes/#findComment-560864 Share on other sites More sharing options...
maexus Posted June 9, 2008 Author Share Posted June 9, 2008 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 https://forums.phpfreaks.com/topic/109349-solved-using-autoload-with-child-classes/#findComment-560867 Share on other sites More sharing options...
maexus Posted June 9, 2008 Author Share Posted June 9, 2008 Figured it out, it has to do with extending singletons. For those interested, check out this link: http://weierophinney.net/matthew/archives/135-Extending-Singletons.html Link to comment https://forums.phpfreaks.com/topic/109349-solved-using-autoload-with-child-classes/#findComment-560876 Share on other sites More sharing options...
keeB Posted June 9, 2008 Share Posted June 9, 2008 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 https://forums.phpfreaks.com/topic/109349-solved-using-autoload-with-child-classes/#findComment-560899 Share on other sites More sharing options...
maexus Posted June 9, 2008 Author Share Posted June 9, 2008 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 https://forums.phpfreaks.com/topic/109349-solved-using-autoload-with-child-classes/#findComment-561104 Share on other sites More sharing options...
keeB Posted June 9, 2008 Share Posted June 9, 2008 I guess I'm just asking why it's so complex. I understood the intention, but I don't quite understand the necessity. Either way, I'm glad it works! Link to comment https://forums.phpfreaks.com/topic/109349-solved-using-autoload-with-child-classes/#findComment-561144 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.