【发布时间】:2016-02-03 17:58:29
【问题描述】:
下面我的webhook 类以及测试会产生该错误。
当我echo 输出$this->callable_object 时,它输出invoice,当我echo 输出$this->callable_method 时,它输出created。出于某种原因,这条线return $object->$this->callable_method( $this->event->data ); 产生了该错误,我不确定为什么?应该和$invoice->created( $args );一样
类+测试
class webhook {
private $event;
private $callable_object;
private $callable_method;
private $types = array(
'invoice.created',
'invoice.payment_failed',
'invoice.payment_succeeded',
'invoice.updated'
);
public function __construct( $event ) {
$this->event = $event;
$this->valid();
$this->parse();
}
public function valid() {
if( !in_array( $this->event->type, $this->types ) ) {
throw new Exception( 'unknown event' );
}
}
public function parse() {
$parts = explode( '.', $this->event->type );
$this->callable_object = $parts[0];
$this->callable_method = $parts[1];
}
public function get_event_type() {
return $this->event->type;
}
public function get_event_data() {
return $this->event->data;
}
public function get_callable_object() {
return $this->callable_object;
}
public function get_callable_method() {
return $this->callable_method;
}
public function execute() {
$object = new $this->callable_object();
return $object->$this->callable_method( $this->event->data );
}
}
$event['type'] = 'invoice.created';
$webhook = new webhook( (object)$event );
$webhook->execute();
【问题讨论】:
-
这个
$object->$this->callable_method( $this->event->data );的解析肯定不是你所期望的。试试$object->{$this->callable_method}( $this->event->data );
标签: php object fatal-error