开发者

Const Defined in Base Class Not Resolvable in Child Class

开发者 https://www.devze.com 2023-03-27 21:48 出处:网络
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.

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 PolicytypeQu开发者_StackOverflowery::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.


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

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


I just had the exact same problem described above, however, in my case, the problem was not a bug with Propel. It was a class name collision in my code.

I was adding propel to an existing system. In my case the collision was a table called "product". I already have a class Product loaded. So when I try to use propel, it never auto-loads the propel Product class at all. Hence, the undefined constant. It's using the wrong code.

The solution is dependent on your situation. I don't expect this to be a quick transition, and I don't want to refactor the deprecated code, so I'm renaming Product in my schema. In a different situation, I might refactor the old code to avoid the collision.

I figured this out reading this soliloquy, thanks David Rea.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号