pheromix
pheromix

Reputation: 19297

Cannot extends Model

I want to create a class which extends Model :

<?php

use Phalcon\Mvc\Model;
use Phalcon\Mvc\Model\Query;

class ModelBase extends Model {

    var $table = null;

    public function __construct($table) {
        parent::__construct();
        $this->table = $table;
    }
    ...
}
?>

This class ModelBase will be extended by all models classes.

At runtime I get an error saying : PHP Fatal error: Cannot override final method Phalcon\Mvc\Model::__construct() in D:\wamp\www\resto\app\models\ModelBase.php on line 117

So how to extend correctly the Model class ?

Upvotes: 0

Views: 993

Answers (2)

James Fenwick
James Fenwick

Reputation: 2211

If $table is the database table name for the model the best way to set it is:

class SomeModel extends ModelBase
{
    public function getSource()
    {
        return 'table_name';
    }
}

Upvotes: 0

galki
galki

Reputation: 8715

Use the onConstruct method in your base model.

class ModelBase extends \Phalcon\Mvc\Model
{
    protected $_table;

    public function onConstruct()
    {
        $this->_table = 'whatever';
    }
}

You can then test that extending the base model works.

class SomeModel extends \ModelBase
{
    public function test()
    {
        echo $this->_table;
    }
}

So calling the extended model's test method will echo whatever

$model = new SomeModel();
$model->test();

Upvotes: 1

Related Questions