php 怎么继承父类的变量_PHP: 对象继承 - Manual

Here's fun, an attempt to make some degree of multiple inheritance work in PHP using mixins. It's not particularly pretty, doesn't support method visibility modifiers and, if put to any meaningful purpose, could well make your call stack balloon to Ruby-on-Rails-esque proportions, but it does work.

protected$_mixMap= array();

public function__construct(){$this->_mixMap=$this->collectMixins($this);

}

public function__call($method,$args){// doesn't pass scope

//return call_user_func_array(array($className, $method), $args);

// Error: Given object is not an instance of the class this method was declared in

//$method = new ReflectionMethod($className, $method);

//return $method->invokeArgs($this, $args);$payload=$this->buildMixinPayload($this->_mixMap,$method,$args);

if(!$payload) throw newException('Method '.$method.' not found');

list($mixinMethod, list($method,$args)) =$payload;

return$this->$mixinMethod($method,$args);

}

protected functioncollectMixins($class){

static$found= array();

static$branch= array();

if(empty($branch))$branch[] =get_class($this);$mixins= array();

foreach(array_reverse(get_class_methods($class)) as$method){

if(preg_match('/^mixin(\w+)$/',$method,$matches)){$className=$matches[1];

if(in_array($className,$branch))

throw newException('Circular reference detected '.implode(' > ',$branch) .' > '.$className);

if(!in_array($className,$found)){

if(!class_exists($className)) throw newException('Class '.$className.' not found');// populate props from mixin classforeach(get_class_vars($className) as$key=>$value){

if(!property_exists($this,$key))$this->$key=$value;

}$found[] =$branch[] =$className;$mixins[$className] =$this->collectMixins($className);

}$branch= array(get_class($this));

}

}

return$mixins;

}

protected functionbuildMixinPayload($mixins,$method,$args){

foreach($mixinsas$className=>$parents){$mixinMethod='mixin'.$className;

if(method_exists($className,$method)) return array($mixinMethod, array($method,$args));

if(!empty($parents) &&$return=$this->buildMixinPayload($parents,$method,$args)){

return array($mixinMethod,$return);

}

}

returnfalse;

}

}?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值