codeigniter - PHP - defined constant returns undefined


I'm working on a CodeIgniter project and I've been having some trouble with constants.

My instances model - which extends a generic MY_Model - defines a constant of its corresponding SQL table, 'instances'.

const DB_TABLE = 'instances';

Elsewhere, $instances_model->get_by() is called, which lives in MY_Model, that references that constant, in the context of Instances_model which should override the constant of the same name in MY_Model. get_by() begins with:

if(!defined('$this::DB_TABLE')) exit($this->table_not_set_error);

The application returns "Instances_model error: table name is not set" and exits.

Declaration/constructor of MY_Model:

class MY_Model extends CI_Model {

  const DB_TABLE = 'abstract';

  public $table_not_set_error;

  function __construct() {

    $this->table_not_set_error = get_class($this) . " error: table name is not set.";


public function get_by($attribute, $value = null, $include_obsolete=0)
    if(!defined('$this::DB_TABLE')) exit($this->table_not_set_error);


class Instances_model extends MY_Model

  const DB_TABLE = 'instances';

  public function __construct()

The two var_dumps in the get_by() function return false and "instances", respectively. This doesn't make sense. If PHP can return the value of the constant, how can it not be defined? It's even returning the correct context: "instances" instead of "abstract". Why does PHP think $this::DB_TABLE is undefined?

Thanks for your help.




The issue is that you're using$this to access a class constant, which is not an instance constant. You want:

if (!defined('self::DB_TABLE')) exit($this->table_not_set_error);



You can also usestatic:: rather thanself:: if you want a child class to be able to redefine the constant to its own value. You use$this-> to access things that belong to an instance object of a class but,self:: orstatic:: to access things that belong to a class itself, like a constant or a static method or property.

People are also looking for solutions to the problem: ajax - How can i make full post-back request from angularJS to phalcon php


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.