ozone Posted July 21, 2009 Share Posted July 21, 2009 Hey again. I faced a problem again. Here we go.. : I have main class "core" in file site.php above in this file, is includes, of other future modules (articles, forums etc...) There are classes in included files. So I need somehow to extend main class "core" with other 5 or 10 classes in included files, so I can work properly... It is kind a CMS mini version for me. I tried - no good :-\ //including all classes in include folder foreach(glob("*.class.php") as $class_filename) { require_once($class_filename); } //main web site control and output class class website { function __construct(){ //get variables from link $pre_link = explode('//', $_SERVER['REDIRECT_URL']); $link = explode('/', $pre_link[1]); //remove .html from the end foreach ($link as $sub_link => $address){ $pat = "/html/i"; if(preg_match($pat, $address)){ $link[$sub_link] = str_replace('.html', '', $address); } //$link[0] = website.com //$link[1] = first_variable between "/".. //last variable most often index or link for SEO $link['last'] = $link[count($link)]; } //deside module of throw error } This is part of core.php file, where is constructor, at this time class contains no more functions... I need advice how to extend main class without calling extenders, becouse there will be 10 classes who extends main "core" class. Thanks in advance... EDITED BY akitchin: just fixing code tags. Quote Link to comment https://forums.phpfreaks.com/topic/166851-need-extended-class-working-by-calling-itself-instead-of-extender-classes/ Share on other sites More sharing options...
ignace Posted July 21, 2009 Share Posted July 21, 2009 Here is a better version: http://pastebin.com/f12cbc64c. It assumes this directory structure: modules - yourModuleName - controllerName - actionName.php - index.php // every controllerName directory must contain a index.php or it will direct you to /default/error/error(.php) - default - error error.php - index index.php index.php //code from http://pastebin.com/f12cbc64c A starter's edition can be downloaded here: http://rapidshare.com/files/256867366/babysteps.zip (Don't mind the name it's part of a project of mine) Quote Link to comment https://forums.phpfreaks.com/topic/166851-need-extended-class-working-by-calling-itself-instead-of-extender-classes/#findComment-879793 Share on other sites More sharing options...
ozone Posted July 21, 2009 Author Share Posted July 21, 2009 Thanks ignace for sharing your work. I need only a push to right side.. Your work in very extended and a bit complexed. Is there some simple way ? Quote Link to comment https://forums.phpfreaks.com/topic/166851-need-extended-class-working-by-calling-itself-instead-of-extender-classes/#findComment-879804 Share on other sites More sharing options...
ozone Posted July 22, 2009 Author Share Posted July 22, 2009 Ok, I form question in other way: How to extend all classes in one class (classes are in separate files with "className.class.php") Quote Link to comment https://forums.phpfreaks.com/topic/166851-need-extended-class-working-by-calling-itself-instead-of-extender-classes/#findComment-880228 Share on other sites More sharing options...
trq Posted July 22, 2009 Share Posted July 22, 2009 What exactly is your question? Quote Link to comment https://forums.phpfreaks.com/topic/166851-need-extended-class-working-by-calling-itself-instead-of-extender-classes/#findComment-880229 Share on other sites More sharing options...
ozone Posted July 22, 2009 Author Share Posted July 22, 2009 I want to make fully automatic module system; Just upload php class file to core directory and core class would find and extend main "core" class with class found in module file... Quote Link to comment https://forums.phpfreaks.com/topic/166851-need-extended-class-working-by-calling-itself-instead-of-extender-classes/#findComment-880234 Share on other sites More sharing options...
trq Posted July 22, 2009 Share Posted July 22, 2009 That doesn't make sense. Classes should only extend classes they extend the functionality of. What makes your uploaded class extend core's functionality? How (if it all) are they related? Quote Link to comment https://forums.phpfreaks.com/topic/166851-need-extended-class-working-by-calling-itself-instead-of-extender-classes/#findComment-880239 Share on other sites More sharing options...
ignace Posted July 22, 2009 Share Posted July 22, 2009 I want to make fully automatic module system; Just upload php class file to core directory and core class would find and extend main "core" class with class found in module file... class Core implements ArrayAccess, Countable, Iterator/*SPL Flavored*/ { protected $_modules = array(); public function loadModules($modulePattern = '*.mod.php') { foreach (glob($modulePattern) as $moduleFile) { require_once($moduleFile); $filename = pathinfo($moduleFile, PATHINFO_FILENAME); $className = ucfirst($filename); if (class_exists($className)) { $object = new $className($this);//TODO: use reflection to identify the parent if ($object instanceof Core) { $this->addModule($object); } } } } public function addModule(Core $module) { $moduleClassName = get_class($module); $this->_modules[$moduleClassName] = $module; } public function moduleIsLoaded($moduleName) { return array_key_exists($moduleName, $this->_modules); } public function getModule($moduleName) { return $this->_modules[$moduleName]; } //TODO: implement ArrayAccess, Countable and Iterator methods } Quote Link to comment https://forums.phpfreaks.com/topic/166851-need-extended-class-working-by-calling-itself-instead-of-extender-classes/#findComment-880309 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.