April 13, 201214 yr I made a cms in OOP php with a single class, I now want to break up a huge class into smaller classes with more user friendly names. So I have a controller called kernel, it has an array called 'services' with all the model classes in it. $kernel->services['user'] contains methods regarding users... etc so $kernel->services['user']->new($params); creates a new user I have a few more classes, but these are giving me trouble: Problem is, two of the 'services' of the kernel are cms, and database, the database service handles database connections(who knew... ), the cms is supposed to handle errors and have settings(such as display language, footer display text, maximum blog posts per page, etc) that it fetches from a database using the database class. I'm having a lot of trouble using methods from one class inside other classes... Should ALL classes extend the kernel so they share the services array so I can call $kernel->services['database'] inside the user class for example? I don't know if I simply haven't gotten a grasp of what MVC really is, or my project has a sketchy architecture, making me really confused. Kernel class: <?php class kernel{ protected $mode; protected $queue; public $services; // array of all active services public $allServices = array('cms', 'db'); public function __construct($db, $services = '*', $bootMode = 'development', $debug = true){ $this->mode = $bootMode; switch($this->mode){ case 'production': error_reporting(0); break; case 'developement': error_reporting(E_STRICT); break; } include('includes/db.class.php'); $this->services['database'] = new db($db['host'], $db['database'], $db['username'], $db['password']); $this->queue = $services == '*' ? $this->allServices : $services; $this->launch($services); } public function launch($services){ $this->queue = $services; foreach($this->queue as $key => $_service){ if(file_exists('includes/'.$_service.'.class.php')){ include('includes/'.$_service.'.class.php'); } else { echo 'Cannot start requested process :: Class file ',$_service,' unexistent<br/>'; } if(class_exists($_service)){ $this->services[$_service] = new $_service; } } } } ?> (I realise some variables are unused, it's not finished) Edited April 13, 201214 yr by Script©
April 30, 201214 yr I made a cms in OOP php with a single class, I now want to break up a huge class into smaller classes with more user friendly names. So I have a controller called kernel, it has an array called 'services' with all the model classes in it. $kernel->services['user'] contains methods regarding users... etc so $kernel->services['user']->new($params); creates a new user I have a few more classes, but these are giving me trouble: Problem is, two of the 'services' of the kernel are cms, and database, the database service handles database connections(who knew... ), the cms is supposed to handle errors and have settings(such as display language, footer display text, maximum blog posts per page, etc) that it fetches from a database using the database class. I'm having a lot of trouble using methods from one class inside other classes... Should ALL classes extend the kernel so they share the services array so I can call $kernel->services['database'] inside the user class for example? I don't know if I simply haven't gotten a grasp of what MVC really is, or my project has a sketchy architecture, making me really confused. Kernel class: <?php class kernel{ protected $mode; protected $queue; public $services; // array of all active services public $allServices = array('cms', 'db'); public function __construct($db, $services = '*', $bootMode = 'development', $debug = true){ $this->mode = $bootMode; switch($this->mode){ case 'production': error_reporting(0); break; case 'developement': error_reporting(E_STRICT); break; } include('includes/db.class.php'); $this->services['database'] = new db($db['host'], $db['database'], $db['username'], $db['password']); $this->queue = $services == '*' ? $this->allServices : $services; $this->launch($services); } public function launch($services){ $this->queue = $services; foreach($this->queue as $key => $_service){ if(file_exists('includes/'.$_service.'.class.php')){ include('includes/'.$_service.'.class.php'); } else { echo 'Cannot start requested process :: Class file ',$_service,' unexistent<br/>'; } if(class_exists($_service)){ $this->services[$_service] = new $_service; } } } } ?> (I realise some variables are unused, it's not finished) Hi Script. I'm still new to OOP with PHP, but I'll try my best, Could you give an example of what you're doing to call and use a method within your class, from a different class?
April 30, 201214 yr mmm im confused, u made this class yet u dont know how to split it up into separate classes?
April 30, 201214 yr I'm a bit rusty with PHP but these days but this might help you. Pimple Dependancy Injection container is perfect for this - http://pimple.sensiolabs.org/ For example... class Kernel extends Pimple { public function __construct() { // database params $this['dataase']['username'] = 'foo'; $this['dataase']['password'] = 'foo'; $this['dataase']['server'] = '127.0.0.1'; $this['dataase']['table'] = 'foo'; // database object $this['database'] = function () { return new Database($this['database']); }; // etc. } } class Login { private $database = null; public function __construct(Kernel $kernel) { $this->setDatabase($kernel['database']); } private function setDatabase(Database $database) { $this->database = $database; } public function login($username, $password) { $this->database->setQuery('SELECT * FROM `user` WHERE 1=1;'); // ... } } $kernel = new Kernel(); //... $login = new Login($kernel);
Create an account or sign in to comment