MySQL_Narb Posted October 10, 2012 Share Posted October 10, 2012 (edited) I've been trying to make a more centralized OOP structure, where one class can ease communication between all other classes (server being the head/main class). Any feedback on this? <?php class server{ public function loadClass($class){ if($this->$class == null){ if(file_exists($class.'.php')){ include($class.'.php'); $this->$class = new $class(); }else{ $this->throwError('class '. $class.' does not exist'); } }else{ $this->throwError($class. 'already loaded.'); } } public function dropClass($class){ $this->$class = null; } public function runMethod($class, $method, array $params = array()){ if($this->isLoaded($class)){ if(count($params) != 0){ $par = ''; $i = 1; foreach($params as $key => $value){ $par .= ($i == count($params)) ? $method : $method.','; $i++; } } $this->$class->$method(($par != null) ? $par : null); }else{ $this->throwError($class .' is not loaded.'); } } protected function isLoaded($class){ return ($this->$class == null) ? false : true; } protected function throwError($error){ die('<font color="red">'. $error .'</font>'); } } ?> Example of usage <?php require('server.php'); $parent = new server(); $parent->loadClass('base'); $parent->runMethod('base', 'hey'); ?> Edited October 10, 2012 by MySQL_Narb Quote Link to comment https://forums.phpfreaks.com/topic/269287-feedback-on-this-oop-design/ Share on other sites More sharing options...
requinix Posted October 10, 2012 Share Posted October 10, 2012 Before I try to critique the design, what need does this fulfill or what problem does this solve that normal code $class = new base(); $class->hey(); does not? Quote Link to comment https://forums.phpfreaks.com/topic/269287-feedback-on-this-oop-design/#findComment-1384112 Share on other sites More sharing options...
ManiacDan Posted October 10, 2012 Share Posted October 10, 2012 Also not reading all this, but if there's a valid answer to Requinix's question: what need does this fulfill or what problem does this solve that __call() does not? Quote Link to comment https://forums.phpfreaks.com/topic/269287-feedback-on-this-oop-design/#findComment-1384113 Share on other sites More sharing options...
DarkerAngel Posted October 10, 2012 Share Posted October 10, 2012 I think the intent is to make seperate class files and make it so that the server will load the file when the class is called rather then needing to require or include this will do it dynamically. IE: you will only need to load server.php in the scope of the program Now I want to call class mysql server.php will see if there is a mysql in current existance if not it will try loading mysql.php and creating a class from that. At least this is what I gathered from my brief reading of the code Interesting concept but not sure if I would actually use it, and if I did I would probably mod it to just auto link and return the class I'm planning on using. Quote Link to comment https://forums.phpfreaks.com/topic/269287-feedback-on-this-oop-design/#findComment-1384115 Share on other sites More sharing options...
requinix Posted October 10, 2012 Share Posted October 10, 2012 Good point: if this class is meant to solve the problem of autoloading classes, PHP already supports that out-of-the-box (but please use the SPL function, not __autoload()). Quote Link to comment https://forums.phpfreaks.com/topic/269287-feedback-on-this-oop-design/#findComment-1384116 Share on other sites More sharing options...
DarkerAngel Posted October 10, 2012 Share Posted October 10, 2012 Good point: if this class is meant to solve the problem of autoloading classes, PHP already supports that out-of-the-box (but please use the SPL function, not __autoload()). Now that is something I might actually use since I normally structure my programs in the same way; as in I usually store classes in the same subfolder with the same basic extension, although I have not got into the habit of naming my files directly after the class. IE: my database class is usually db.**class_extention** not database.**class_extention** Quote Link to comment https://forums.phpfreaks.com/topic/269287-feedback-on-this-oop-design/#findComment-1384121 Share on other sites More sharing options...
MySQL_Narb Posted October 11, 2012 Author Share Posted October 11, 2012 I think the intent is to make seperate class files and make it so that the server will load the file when the class is called rather then needing to require or include this will do it dynamically. IE: you will only need to load server.php in the scope of the program Now I want to call class mysql server.php will see if there is a mysql in current existance if not it will try loading mysql.php and creating a class from that. At least this is what I gathered from my brief reading of the code Interesting concept but not sure if I would actually use it, and if I did I would probably mod it to just auto link and return the class I'm planning on using. The exact reasoning behind this. Easier control among everything. Quote Link to comment https://forums.phpfreaks.com/topic/269287-feedback-on-this-oop-design/#findComment-1384368 Share on other sites More sharing options...
DarkerAngel Posted October 11, 2012 Share Posted October 11, 2012 The exact reasoning behind this. Easier control among everything. Yeah but you should really do some research in requinix's post about spl_autoload() functionality in PHP (5+) it already provides the basic functionality you are trying to code and does it in a more pleasant way by being able to return the class into an object variable rather than trying to run one class through another, (the only thing I would have modded in your original writeup.) Quote Link to comment https://forums.phpfreaks.com/topic/269287-feedback-on-this-oop-design/#findComment-1384391 Share on other sites More sharing options...
ignace Posted October 11, 2012 Share Posted October 11, 2012 (edited) spl_autoload(function($className) { $className = ltrim($className, '\\'); $fileName = ''; $namespace = ''; if ($lastNsPos = strripos($className, '\\')) { $namespace = substr($className, 0, $lastNsPos); $className = substr($className, $lastNsPos + 1); $fileName = str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR; } $fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php'; require $fileName; }); This is what I would use in small scripts. Any other I use Composer and it solves the autoloading for me. "autoload":{ "psr-0":{ "mylib": "src/" } } require 'vendor/autoload.php'; Edited October 11, 2012 by ignace Quote Link to comment https://forums.phpfreaks.com/topic/269287-feedback-on-this-oop-design/#findComment-1384429 Share on other sites More sharing options...
requinix Posted October 11, 2012 Share Posted October 11, 2012 By the way folks, it's spl_autoload_register() that registers the callback functions. Quote Link to comment https://forums.phpfreaks.com/topic/269287-feedback-on-this-oop-design/#findComment-1384438 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.