user3167249
user3167249

Reputation: 1102

Extending A PHP Class - Help Understanding

I am having a little trouble understanding how a class extends another. I have my model class.

class model{
    public $db;

    public function __construct(){
        $this->db = $GLOBALS['db']; 
    }

    public function _sel($table, $where="", $order_by="", $limit="", $group_by="",$database = NULL){            
        if($database === NULL) {  $database = $this->db; }
        // Yada Yada Yada
        $results = $database->select($sql);
    }

And I have a pagination class to extend it:

    class pagination extends model {

        public $limit;
        public $page;
        public $criteria;
        private $_totalRecords;
        private $_totalPages;   

        public function __construct(){  

            // Initialize
            $arguments = func_get_args();
            if(!empty($arguments)){
                foreach($arguments[0] as $key => $property){
                    if(property_exists($this, $key)){
                        $this->{$key} = $property;
                    }
                }
            }

        }

        public function getPaginationPage(){
          // Yada Yada Yada
          // This next line causes the issue
          $records = $this->_sel($query['table'],$query['where'],$query['order_by'],$start." , ".$end,$query['group_by'],$query['database']);

To keep this post short I tried to only include the necessary sections of code. The issue I am having is when I execute a query in the extended class it is failing because $this->db has no value. Since I set this in the constructor it seems to me that I need to call it again with something like $xx = new model() however since it's just an extension of a class that should already exist I am confused as to why it would not already have a value. Admittedly I am self taught and just starting to get into classes. Is my understanding of them wrong or is there just something I need to do make sure it "extends"?

Also this is how I am calling the extended class:

$this->model->pagination = new pagination(array(
            'limit'     =>  10,
            'page'      =>  1,
            'criteria'  => array('projects','status_id > 0','name ASC',''),         
        ));

Upvotes: 1

Views: 49

Answers (1)

mopo922
mopo922

Reputation: 6381

You have overwritten your model constructor in the pagination class. Simply call the parent's constructor from the child's constructor to retain both:

class pagination extends model {
    ...

    public function __construct() { 
        parent::__construct();
        ...

Upvotes: 1

Related Questions