【问题标题】:What code will run before construct in php?在 php 中构造之前会运行什么代码?
【发布时间】:2019-01-24 16:12:45
【问题描述】:

我正在学习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看到如下错误:

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”


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

【问题讨论】:

  • 你确定你配置你的开发环境来显示所有发生在 php 中的错误吗? (我的意思是error_reporting(-1);
  • 您的构造可能会抛出您没有看到的错误。
  • 是的。 error_reporting = E_ALL 在 php.ini 中。而我刚刚添加了error_reporting(-1);,同样的错误
  • 我把代码放在try catch,还是得到502 bad gateway
  • 将您的代码发布为纯文本,而不是图像。有关代码格式化帮助,请参阅 formatting

标签: php extends grpc


【解决方案1】:
 parent::__construct($data);

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

【讨论】:

    猜你喜欢
    • 2016-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-14
    相关资源
    最近更新 更多