sineverba
sineverba

Reputation: 5172

php "pure" OOP: how design classes?

PREAMBLE: I use F3 Framework (www.fatfreeframework.com).

Let's say I have an abstract class "Controller"

<?php
abstract class Controller
{

    /* F3 */
    protected $f3;
    /* DATABASE SECTION */
    protected $db_name;
    protected $db_host;
    protected $db_username;
    protected $db_password;

    function __construct()
    {
        $f3 = Base::instance();
        $this->f3 = $f3;
    }

    protected function get_db_name()
    {
        return $this->db_name;
    }

    protected function get_db_host()
    {
        return $this->db_host;
    }

    protected function get_db_username()
    {
        return $this->db_username;
    }

    protected function get_db_password()
    {
        return $this->db_password;
    }

    protected function set_db_name($db_name)
    {
        $this->db_name = $db_name;
    }

    protected function set_db_host($db_host)
    {
        $this->db_host = $db_host;
    }

    protected function set_db_username($db_username)
    {
        $this->db_username = $db_username;
    }

    protected function set_db_password($db_password)
    {
        $this->db_password = $db_password;
    }

} // /class Controller

And a FrontendController Class:

<?php
class FrontendController extends Controller
{
    public function get_view()
    {
        $this->set_db_host('localhost');
    }       
}

His work it's manage frontend website (html, print results from database, etc etc etc).

After some day, I need the BackendController class, to manage the news of the site (for example).

1) I need to rewrite the db connections (for example) ? Or I will move them in construct of Controller?

2) In effect, from point 1, I did think to add them in __construct of subclass but ofcourse it override the __construct of abstract Class

3) Tomorrow I want create a ToolboxController Class. Let's say it's scope will be perform some task (e.g. "extract first X characters from a text", "format data according to user settings"), all code pieces that I will/can reuse in other part of app.

In this case I will need to refactor all website changing from:

Controller <= FrontendController

Controller <= BackendController

to

Controller <= ToolboxController <= FrontendController

Controller <= ToolboxController <= BackendController

where <= mean "extends" ?

It seems very very stupid question and easy, but I would understand logic process of add new functions to a born-small website to a large portal.

Upvotes: 0

Views: 173

Answers (1)

Christian Gollhardt
Christian Gollhardt

Reputation: 17024

You do it the Wrong way. The root of your Problem is, you have no Model.

A Model is not a simple class, it as a whole abstraction layer.

  • Domain Object
  • Value Object
  • Database Mapper
  • Service

Read this good answer, to learn about it.

At the end, you Goal should be something like this this

$model = new Model();
$controller = new Controller($model);
$view = new View($model);

The View and the Controller shares the same Model, and they get it. They don't know how to obtain.

Upvotes: 2

Related Questions