Search code examples
phppropel

Const Defined in Base Class Not Resolvable in Child Class


For anyone that has read my last couple of questions, I have successfully got CI and Propel integrated and autoloading is working great. Now I am getting into the nuances of Propel itself.

From my CI controller, I want to query all of the records from a table via Propel. So the code in my controller is like this:

function index() {
    $data = array();
    $data['policytypes'] = PolicytypeQuery::create()->find();
    $this->load->view('policytype_view',$data);
}

Somewhere down in the callstack for PolicytypeQuery::create()->find(), we step into ModelCriteria::__construct(), which yields the following error:

constant(): Couldn't find constant Policytype::PEER

Here's the entire __construct() method:

public function __construct($dbName = null, $modelName, $modelAlias = null){
    $this->setDbName($dbName);
    $this->originalDbName = $dbName;
    $this->modelName = $modelName;
    // THIS LINE GENERATES THE ERROR
    $this->modelPeerName = constant($this->modelName . '::PEER');  
    $this->modelAlias = $modelAlias;
    $this->tableMap = Propel::getDatabaseMap($this->getDbName())->getTableByPhpName($this->modelName);
}

So the generated Policytype class is just a stub, and inherits from BasePolicytype, which has a bit more meat to it (but is also generated). In BasePolicytype we have:

abstract class BasePolicytype extends BaseObject  implements Persistent
{

    /**
     * Peer class name
     */
    const PEER = 'PolicytypePeer';

So...the const is defined in the base class. Why is it not resolvable from the child class?


UPDATE:

So, this does work:

abstract class ClassA {
    const CLASSA_CONST = 'foo';
}

class ClassB extends ClassA {
}

class ClassC {
    function __construct($classname){
        echo constant($classname . "::CLASSA_CONST");
    }
}

$c=new ClassC("ClassB");

Both run under PHP 5.3.


Solution

  • It's "resolvable" (visible and accessible), but you got the name wrong.

    It's BasePolicytype::PEER, not Policytype::PEER.