PHP可捕获致命错误:webhook类的对象无法转换为字符串

PHP可捕获致命错误:webhook类的对象无法转换为字符串,php,object,fatal-error,Php,Object,Fatal Error,下面的webhook类与测试一起产生该错误。 当我echoout$this->callable\u对象时,它输出invoice,当我echoout$this->callable\u方法时,它输出创建的。出于某种原因,此行return$object->$this->callable\u方法($this->event->data)产生了那个错误,我不知道为什么?它应该与创建的($args)相同 课程+测试 class webhook { private $event; privat

下面的
webhook
类与测试一起产生该错误。 当我
echo
out
$this->callable\u对象时,它输出
invoice
,当我
echo
out
$this->callable\u方法时,它输出创建的
。出于某种原因,此行
return$object->$this->callable\u方法($this->event->data)产生了那个错误,我不知道为什么?它应该与创建的($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();

告诉php如何解析如下构造:

$object->$this->callable_method( $this->event->data );
改为:

$object->{$this->callable_method}( $this->event->data );

告诉php如何解析如下构造:

$object->$this->callable_method( $this->event->data );
改为:

$object->{$this->callable_method}( $this->event->data );

u_mulder的解决方案是正确的,但也需要进一步解释

以下是您目前拥有的:
$object->$this->callable\u方法($this->event->data)

PHP将尝试将属性向下递归到
callable_method
,这意味着它正在
$object
上查找属性
$this
,由于
$this
无法转换为字符串,因此会出现错误

这基本上与数学中的情况类似,如果下面的
1+1x1+1
结果将是
3
,而实际上我们的意思是
(1+1)x(1+1)
,这将是
4
。我们在数学中加括号是为了增加重要性(不要因为术语而批评我,你懂的)

就像数学一样,我们可以用PHP做同样的事情,但是我们用大括号代替


$object->{$this->callable_method}($this->event->data)

u\u mulder的解决方案是正确的,但也需要进一步解释

以下是您目前拥有的:
$object->$this->callable\u方法($this->event->data)

PHP将尝试将属性向下递归到
callable_method
,这意味着它正在
$object
上查找属性
$this
,由于
$this
无法转换为字符串,因此会出现错误

这基本上与数学中的情况类似,如果下面的
1+1x1+1
结果将是
3
,而实际上我们的意思是
(1+1)x(1+1)
,这将是
4
。我们在数学中加括号是为了增加重要性(不要因为术语而批评我,你懂的)

就像数学一样,我们可以用PHP做同样的事情,但是我们用大括号代替


$object->{$this->callable_method}($this->event->data)

This
$object->$This->callable\u方法($This->event->data)
肯定是以某种方式解析的,而不是您所期望的。尝试
$object->{$this->callable_method}($this->event->data)
This
$object->$This->callable\u方法($This->event->data)
肯定是以某种方式解析的,而不是您所期望的。尝试
$object->{$this->callable_method}($this->event->data)伟大的解释李!现在完全有道理了。李解释得很好!现在完全有道理了。