首页 > 解决方案 > 在 php 中构造之前会运行什么代码?

问题描述

我正在学习grpc,但是我发现了一些奇怪的问题。

<?php
# Generated by the protocol buffer compiler.  DO NOT EDIT!
# source: order.proto

namespace Order;

use Google\Protobuf\Internal\GPBUtil;

class Test extends \Google\Protobuf\Internal\Message
{
    private $student_id = '';

    public function __construct($data = NULL)
    {
        echo 'test message';
        exit;
        \GPBMetadata\Order::initOnce();
        parent::__construct($data);
    }

    /**
     * Generated from protobuf field <code>string student_id = 1;</code>
     *
     * @return string
     */
    public function getStudentId()
    {
        return $this->student_id;
    }

    /**
     * Generated from protobuf field <code>string student_id = 1;</code>
     *
     * @param string $var
     * @return $this
     */
    public function setStudentId($var)
    {
        GPBUtil::checkString($var, True);
        $this->student_id = $var;

        return $this;
    }

}

require __DIR__ . '/vendor/autoload.php';
$test = new \Order\Test();

当我执行上面的代码时,我无法得到'测试消息';

但是,当我从“测试”类中删除“扩展”时,我可以得到它。

它出什么问题了 ?一些代码之前运行过__construct

nginx/error.log 我看到以下错误:</p>

74988#0:*380 kevent() 在从上游读取响应标头时报告关闭连接(54:对等方重置连接),客户端:127.0.0.1,服务器:local.grpc.develop,请求:“GET / HTTP/ 1.1”,上游:“fastcgi://127.0.0.1:9000”,主机:“local.grpc.develop”


[1] 37548 segmentation fault在cli中运行时得到了。

标签: phpextendsgrpc

解决方案


 parent::__construct($data);

这应该在构造中的代码之上,以便应用程序父构造具有优先级。那么你的调试应该会更容易。当您在父构造之前退出时,您会杀死应用程序。


推荐阅读