HiveBrain v1.2.0
Get Started
← Back to all entries
patternphpMinor

Content management system for teaching PHP OOP to beginners

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
phpsystembeginnersmanagementteachingforcontentoop

Problem

I created this basic content management system to teach object oriented PHP to beginners. However, I am concerned that it violates SOLID principles in App.php and Router.php.

Here's the project on GitHub

App.php:

```

*/
namespace Midori\Cms;

use Midori\Cms;
use erbilen;

/**
* Class App
*
* @package Midori\Cms
*
*/
class App
{

/**
* Holds application wide settings
*
* @var mixed
*/
protected $settings = false;

/**
* The variable that holds database connection.
*
* @var mixed
*/
private $db = false;

/**
* Holds request params, coming from uri.
*
* @var mixed
*/
private $params = array();

/**
* Extracted params for request generation
*
* @var array
*/
private $request;

/**
* The data that comes from forms or classes
*
* @var mixed
*/
private $data = null;

/**
* Function that starts application using config vars
*
* @param
* $config
* @return mixed
*/
public function __construct($config)
{
$this->startSession();
$this->getRequests();
$this->startDB($config);

$this->params = explode("/", $this->request);

$params = $this->params;

$className = __NAMESPACE__ . '\\' . $this->params[1];

$class = new $className($this->db);
$class->getRelatedData($this->getCategories());

if (empty($this->data)) {
if (! isset($params[2]) || ! $params[2]) {
$this->params[2] = 'index';
} else {
if (isset($params[3])) {
$this->data = $class->$params2;
} else {
$this->data = $class->$params[2]();
}
}
} else {
$this->data = call_user_func_array(array(
$class,
$params[2]
), $this->data);

Solution

I think your App class violates the Single Responsibility principle and the Open Closed Principle.

  • SRP -> I think you should create different Creators for each Entity.



  • OCP -> I think you should create more abstract classes in case your project gets bigger, and then create more specific instances for each feature.



I wrote an article about solid principles and PHP a while ago. And you may want to take a look at design patterns. The factory method will help you on your project.

Context

StackExchange Code Review Q#70029, answer score: 2

Revisions (0)

No revisions yet.