Php 在抽象类中声明的方法中从子级重写Get常量
我有这个(缩短): 问题是,$c::DBID/TOKEN在php<5.3中不起作用 是否有一种方法可以从与5.2.9兼容的抽象类中完成相同的任务?编辑: 常数并不真正意味着要在对象实例化过程中被改变,您可能需要考虑成员变量。 抽象类不能直接实例化。您可以创建一个子类来扩展抽象类,然后调用getDb()Php 在抽象类中声明的方法中从子级重写Get常量,php,oop,class,reflection,constants,Php,Oop,Class,Reflection,Constants,我有这个(缩短): 问题是,$c::DBID/TOKEN在phpsetDbId(父::$dbID)和$this->setToken(父::$token)?如果要使用相同值的父版本,只需取出子版本的重写值,那么对getDb()的任何调用都将使用父版本。我只是向您展示了此解决方案提供的灵活性。 abstract class MyModel { private $dbId; private $token; public function setDbId($dbId)
我在PHP5.2代码库中有一个解决方案,它使用反射从超类中获取子类的常量,但我建议不要这样做,除非这是绝对必要的,因为反射在PHP中是性能相对昂贵的工具 PHP5.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]);
}
}
}
在抽象构造函数中,我不会这样做:$this->setDbId(父::$dbID)和$this->setToken(父::$token)?如果要使用相同值的父版本,只需取出子版本的重写值,那么对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());
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]);
}
}
}