我知道你可以在构造它时扩展一个类,如下所示:
class b extends a {
}
但是是否可以从脚本中动态扩展类?如:
$b = new b($input) extends a;
我希望完成的是对模块进行不同的扩展,无论它是在管理页面中使用还是在公共页面中使用。我知道我可以使用相同的名称创建两个不同的父类,并且每个管理员或公共只包含一个。但我的问题是,是否可以在 PHP 中动态执行?
不,不是没有像 RunKit 这样的扩展。
您可能会考虑另一种方法。如果您希望 B 承担 A 的功能,也许像下面这样的东西可以提供一种“混合”方法。一般情况下,B 不是 A 的孩子,而是 B 委托给 A。
<?php
class MixMeIn
{
public $favouriteNumber = 7;
public function sayHi() {
echo "Hello\n";
}
}
class BoringClass
{
private $mixins = array();
public function mixin($object)
{
$this->mixins[] = $object;
}
public function doNothing() {
echo "Zzz\n";
}
public function __call($method, $args)
{
foreach ($this->mixins as $mixin)
{
if (method_exists($mixin, $method))
{
return call_user_func_array(array($mixin, $method), $args);
}
}
throw new Exception(__CLASS__ + " has no method " + $method);
}
public function __get($attr)
{
foreach ($this->mixins as $mixin)
{
if (property_exists($mixin, $attr))
{
return $mixin->$attr;
}
}
throw new Exception(__CLASS__ + " has no property " + $attr);
}
public function __set($attr, $value)
{
foreach ($this->mixins as $mixin)
{
if (property_exists($mixin, $attr))
{
return $mixin->$attr = $value;
}
}
throw new Exception(__CLASS__ + " has no property " + $attr);
}
}
// testing
$boring = new BoringClass();
$boring->doNothing();
try {
$boring->sayHi(); // not available :-(
}
catch (Exception $e) {
echo "sayHi didn't work: ", $e->getMessage(), "\n";
}
// now we mixin the fun stuff!
$boring->mixin(new MixMeIn());
$boring->sayHi(); // works! :-)
echo $boring->favouriteNumber;
只是一个滑稽的想法。我希望我正确理解了这个问题。
你不能,但这已经要求了几年:https ://bugs.php.net/bug.php?id=41856&edit=1
您可以在 eval 中定义类,但这比正常声明类更麻烦。
但是您不能extends
在对象创建时使用。extends
仅在类定义中使用,并为我们的新类定义哪个其他类是“父类”。
或者,如果您对 javascript 样式的继承感到满意并且不介意丢失类型检查:
<? //PHP 5.4+
final class ExpandoLookalike {
//Allow callable properties to be executed
public function __call($name, $arguments) {
\call_user_func_array($this->$name, $arguments);
}
}
$newBaseModule = static function(){
$base = new ExpandoLookalike();
//Common base functions get assigned here.
$basePrivateVar = 42;
$base->commonFunction = static function($params1, $params2) use ($basePrivateVar){
echo "common function\n";
};
$base->comment = static function() use ($basePrivateVar){
echo "Doing base comment with $basePrivateVar\n";
};
return $base;
};
//Javascript-style extends
$newAdminModule = static function($param) use ($newBaseModule){
$base = $newBaseModule();
$privateVar = 5;
$base->adminProperty = 60;
$base->suspendSite = static function() use ($param, $privateVar){
echo 'Doing admin only function ';
echo "with $param, $privateVar\n";
};
return $base;
};
$newPublicModule = static function() use ($newBaseModule){
$base = $newBaseModule();
$privateVar = 3;
//Javascript-style overloading
$oldComment = $base->comment;
$base->comment = static function($data) use ($oldComment, $privateVar){
$oldComment();
echo 'Doing public function ';
echo "with $data\n";
};
return $base;
};
$baseModule = $newBaseModule();
$adminModule = $newAdminModule('P');
$publicModule = $newPublicModule();
$adminModule->suspendSite(); //echos 'Doing admin only function with P, 5'
echo "{$adminModule->adminProperty}\n"; //echos '60'
$publicModule->comment('com'); //echos 'Doing base comment with 42'
//'Doing public function with com'
?>
尽管关闭了特性和接口的大门,但它打开了其他有趣的大门来弥补:
<? //PHP 5.4+
$inheritAllTheThings = static function(){
$base = new ExpandoLookalike();
foreach(\func_get_args() as $object){
foreach($object as $key => $value){
//Properties from later objects overwrite properties from earlier ones.
$base->$key = $value;
}
}
return $base;
};
$allOfEm = $inheritAllTheThings(
$newPublicModule(),
$newAdminModule('Q'),
['anotherProp' => 69,]
);
$allOfEm->comment('f'); //echos 'Doing base comment with 42'
//Because AdminModule came after PublicModule, the function that echos 'f'
//from PublicModule was overridden by the function from AdminModule.
//Hence, order denotes resolutions for multiple inheritance collisions.
$allOfEm->suspendSite(); //echos 'Doing admin only function with Q, 5'
echo $allOfEm->anotherProp . "\n"; //echos '69'
?>
您可以使用类型转换。如果 a 扩展 b 那么你可以做
$a=(a)(new b($input));
这不完全相同,但相似。
是的,正如 cory 所提到的,这个功能之前已经被请求过。但在此之前,您可以创建一个解决方法。这是我的老把戏
创建两个单独的类,如下所示:
class a {
}
class b {
public $object;
}
然后,也创建一个扩展版本
class bextendeda extends a {
}
在类 b 的构造方法中,放置几个如果请求重定向到扩展对象的函数。
class b {
public $object;
public function __contruct($extend = false) {
if($extend) $this -> object = new bextendeda();
else $this -> object = $this;
}
function __get($prop) {
return $this-> object -> $prop;
}
function __set($prop, $val) {
$this-> object -> $prop = $val;
}
function __call($name, $arguments)
{
return call_user_func_array(array($this -> object, $name), $arguments);
}
}
有了它,如果您想要扩展版本,只需执行此操作
$b = new b(true);
如果不
$b = new b();
享受 :)