jeudi 27 juillet 2017

PHP object composition practice

I have a Parent Class Model Like this:

namespace vendor\appname\core;

class Model
{
    protected $db;
    protected $session;
    protected $cookie;
    protected $record;

    public function __construct()  
    {
        $this->db = new Db;
        $this->session = new Session;
        $this->cookie  = new Cookie;
        $this->record  = new Record;
    }
}

here I have my child models where i can extend the base model and use there objects

namespace vendor\appname\models;

use vendor\appname\core;  

class Login extends Model
{
      // here i can use simply like so 
      $this->db->prepare() // and so on 
      $this->session->set() // and so on so fourth
}

So if i have 100 class should i do the same thing "object composition " is this a good practice ??? what about the registry pattern or factory ? is it possible to implement it here ? or dependency injection

Aucun commentaire:

Enregistrer un commentaire