Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

OOP PHP messing with my brain

Featured Replies

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... :p), 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 by Script©

  • 3 weeks later...

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... :p), 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?

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

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.