从抽象类中声明的方法中的子方法中重写常量



我有这个(缩短):

abstract class MyModel
{
    const DBID = "";
    const TOKEN = "";
    public function getDB()
    {
        if(!$this->_DB)
        {
            $c = get_called_class(); // doesn't work pre php 5.3
            echo $c::DBID; // doesn't work pre php 5.3
            echo $c::TOKEN // doesn't work pre php 5.3
        }
        return $this->_qb;
    } 

问题是get_called_class()和$c::DBID/TOKEN不工作在php <5.3

是否有一种方法可以从与5.2.9兼容的抽象类中完成相同的任务?

编辑:在对象实例化的过程中,常量并不是真的要被改变的,你可能需要考虑使用成员变量。

抽象类不能直接实例化。您可以创建一个子类来扩展抽象类,然后调用getDb()。

abstract class MyModel
{
    private $dbId;
    private $token;
    public function setDbId($dbId)
    {
        $this->dbId = $dbId;
    }
    public function setToken($token)
    {
        $this->token = $token;
    }
    public function getDbId()
    {
        return $this->dbId;
    }
    public function getToken()
    {
        return $this->token;
    }
    public function __construct()
    {
        // All child classes will have the same values
        $this->setDbId('myParentDbId');
        $this->setToken('myParentToken');
    }
    protected function getDb()
    {
        echo $this->getDbId();
        echo $this->getToken();
    }
}
class MyChildModel extends MyModel
{
    // Don't need any methods, just access to abstract parent
    // But if I wanted to override my parent I could do this
    public function __construct()
    {            
        parent::__construct();
        $this->setDbId('myChildDbId');
        $this->setToken('myChildToken');
    }
}
$myChildModel = new MyChildModel();
var_dump($myChildModel->getDb());

我在PHP 5.2代码库中有一个解决方案,使用反射从超类中获取子类的常量,但我不建议这样做,除非绝对必要,因为反射在PHP中是一个相对昂贵的性能工具。

PHP 5.3在static::CONST中引入了static关键字来访问类的静态成员,而不是self::CONST。我从来没有真正尝试过,但我相信它应该能够做你需要的。在PHP手册中查阅晚期静态绑定。

对于记录,这里是一个使用反射来获取子类常量的方法的代码。

class SomeClass
{
    /**
     * Get reflection class for item
     * 
     * Get a reflector for this item so that internal constants can be used for the permission checking
     * functions.  This is necessary because of how static binding works prior to PHP 5.3.  
     * 
     * @return ReflectionClass
     */
    protected function getRef ()
    {
        if (!$this -> ref)
        {
            $this -> ref    = new ReflectionClass ($this);
        }
        return ($this -> ref);
    }
    /**
     * Check that the user has permission to create an item of the type this object represents
     *
     * @todo Use late static binding instead of reflection once PHP 5.3 becomes available on the server
     * @return bool True if OK
     */
    public function canCreate ()
    {
        $ref    = $this -> getRef ();
        if ($flag = $ref -> getConstant ('USR_FLAG_CREATE'))
        {
            return (self::$user -> permissions [$flag]);
        }
    }
}

相关内容

  • 没有找到相关文章

最新更新