0

Recently I started looking more seriously into classes and OOP in general in PHP. I'm trying to create a simple site while taking advantage of classes as much as possible but there have been some questions that have been stuck in my mind.

More specifically I'd like to know how would a 'proper' site structure look like with classes and methods that deal with users, pages, menus, etc. Currently I'm using this wrapper to get around MySQLi more effectively and I can't seem to decide how should I implement this wrapper with other objects - do I create a new class for users, menus, etc. separately and just pass the database variable through the class every time I create an instance?

$db = new Mysqlidb (DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);
$user = new User ($db);

Or do I just extend the SQL class as "models" like the wrapper's author has pointed out here.

Also in general, what should remain a class, what should I handle with only functions? How should I handle things with my MySQL class?

Is it a good practice to handle every single menu item/user/page as an object?

These are just some of the things confusing my mind right now. There are quite a few examples on the web about OOP but I still haven't found a decent one to explain the questions I asked above.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Martin J
  • 63
  • 1
  • 4
  • 1
    You should learn to use frameworks, more especially Symfony2. It takes time, but it helps you to structure your project. – Pierre Emmanuel Lallemant Jul 29 '15 at 13:49
  • I'd just recommend using PDO - it's a nice suite of battle tested, built-in classes that's going to do almost everything you need anyway. Even if you want to return your data as pre-populated objects (models) you can use `PDOStatement::fetchAll(PDO::FETCH_CLASS, 'Foo');` as illustrated on [example #4](http://php.net/manual/en/pdostatement.fetchall.php). For more high-level functionality you can go down the ORM route - look at Doctrine etc. – Darragh Enright Jul 29 '15 at 13:57
  • there are simpler and more lightweight frameworks to start with. I do suggest Slim 3 and Lumen (from laravel) to get a feeling about Controllers (google for MVP pattern) – STT LCU Jul 29 '15 at 14:00
  • This question is too broad to be a good fit for this site. You need to start with some research then come here with more specific questions. phpacademy has some great OOP tutorials, look at some frameworks for file structure example, check out some github projects for real life examples. – Dan Jul 29 '15 at 14:02
  • Do some research on Object Oriented Design and different design patterns. This article gives a good primer and is based in php https://scotch.io/bar-talk/s-o-l-i-d-the-first-five-principles-of-object-oriented-design – Josh J Jul 29 '15 at 14:05
  • Thanks Josh, that link was useful. – Martin J Aug 04 '15 at 14:47

1 Answers1

1

if you already familar with mysqlidb, its 2.1 and 2.2 releases are coming with a new dbObject class to implement models with some extra features.

Check out their https://github.com/avbdr/PHP-MySQLi-Database-Class/blob/master/dbObject.md

If you still want to stick with a pure MysqliDb classes you can try to do:

class Model {
    protected $db;

    public function __constuct () {
        $this->db = MysqliDb::getInstance();
    }
    public function __call ($method, $arg) {
        call_user_func_array (array ($this->db, $method), $arg);
        return $this;
    }
}

class user extends Model {
    $this->tbl = 'users';
    public function create ($data) {
         $this->db->insert (...);
    }
    public function admins () {
         return $db->where ("role", "admin")->get($this->tbl);
    }
}

This will give you models like:

$user = new user ();
$q = $user->where('login','root')->admins();

Take a look at dbObject as its pretty simple and I bet it will do a job you need it to do.

As for OOP I would avoid storage of the mostly static content in the database. Just split menus/sidebars/header/etc into separate subviews and you should be fine.

alex b
  • 24
  • 3