A
A
Afatar2015-11-03 02:53:59
PHP
Afatar, 2015-11-03 02:53:59

Change property of parent class?

Greetings colleagues! I am writing my bike, type MVC. There is a model

<?php
namespace App\Models;
use PDO;
class Model{
  protected $dbname="shop";
  protected $dbuser="root";
  protected $dbpass="";
  protected $table='items';
  public $db;
  function __construct(){
    try {
        $this->db=new PDO("mysql:dbname=$this->dbname;dbhost=localhost;charset=utf8",$this->dbuser,$this->dbpass);
      }
    catch(PDOException $e) {  
      echo $e->getMessage();
      die();			
    }
  
  }
  
   function all(){
    $db=$this->db;
    $model=$db->query("select * from $this->table");
    return $model;
  }
}

In the controller I write
....
static function all(){
    $model=new Model;
    $item=$model->all();
    return View::render('show',['items'=>$item]);
  }
....

And further in representation I display. Everything is working! But I want to do like in Laravel i.e. all models will inherit from Model (and have its methods, in my case all())
class items  extends Model {
}

But the problem is with the table name. How can you override the $table property of the Model class from the items child class to substitute the table name in queries?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
DevMan, 2015-11-03
@Afatar

class items  extends Model {
    protected $table='newItem';
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question