首頁  >  文章  >  後端開發  >  protocol-buffer - protocolbuffers擴充升級到php7遇到的問題?

protocol-buffer - protocolbuffers擴充升級到php7遇到的問題?

WBOY
WBOY原創
2016-09-08 08:43:55988瀏覽

背景

由於專案中正在使用protocolbuffers,整個專案中使用到的擴展基本上都已經支援php7,只有protocolbuffers這個還不支援php7,原作者已經停止維護了該擴展,無奈只能自己開始研究如何將protocolbuffers擴展的5.x版本升級到php7。目前該擴充可以透過編譯。 github

make test結果如下:

<code>Number of tests :  144               144
Tests skipped   :    0 (  0.0%) --------
Tests warned    :    0 (  0.0%) (  0.0%)
Tests failed    :   67 ( 46.5%) ( 46.5%)
Expected fail   :    0 (  0.0%) (  0.0%)
Tests passed    :   77 ( 53.5%) ( 53.5%)</code>

遇到的問題

以下為測試程式碼:

<code><?php
require dirname(__FILE__) . DIRECTORY_SEPARATOR . "messages" . DIRECTORY_SEPARATOR . "field_int32.proto.php";

$bytes = file_get_contents(dirname(__FILE__) . DIRECTORY_SEPARATOR . "fixtures" . DIRECTORY_SEPARATOR . "001_int32_init.bin");

$u = new Tutorial_Integer32();
$u->setValue(0);

$obj = ProtocolBuffers::decode("Tutorial_Integer32", $bytes);
var_dump($obj);
if ($obj instanceof Tutorial_Integer32) {
    var_dump($obj->getValue());
    if ($obj->getValue() == 0) {
        echo "OK" . PHP_EOL;
    } else {
        var_dump($obj);
    }
} else {
    var_dump($obj);
}

ini_set("protocolbuffers.native_scalars", 1);
$obj = ProtocolBuffers::decode("Tutorial_Integer32", $bytes);

if ($obj instanceof Tutorial_Integer32) {
    var_dump($obj->getValue());
    if ($obj->getValue() === 0) {
        echo "OK" . PHP_EOL;
    } else {
        var_dump($obj);
    }
} else {
    var_dump($obj);
}

</code>

輸出結果如下:

<code>object(Tutorial_Integer32)#2 (2) {
  ["_properties":protected]=>
  array(0) {
  }
  ["value":protected]=>
  string(1) "0"
}
NULL
OK
NULL
object(Tutorial_Integer32)#3 (2) {
  ["_properties":protected]=>
  array(0) {
  }
  ["value":protected]=>
  int(0)
}</code>

問題總結:在protocolbuffers::decode時,能夠傳回被解析的對象,但是對象的屬性無法讀取。求解中。 。 。

回覆內容:

背景

由於專案中正在使用protocolbuffers,整個專案中使用到的擴展基本上都已經支援php7,只有protocolbuffers這個還不支援php7,原作者已經停止維護了該擴展,無奈只能自己開始研究如何將protocolbuffers擴展的5.x版本升級到php7。目前該擴充可以透過編譯。 github

make test結果如下:

<code>Number of tests :  144               144
Tests skipped   :    0 (  0.0%) --------
Tests warned    :    0 (  0.0%) (  0.0%)
Tests failed    :   67 ( 46.5%) ( 46.5%)
Expected fail   :    0 (  0.0%) (  0.0%)
Tests passed    :   77 ( 53.5%) ( 53.5%)</code>

遇到的問題

以下為測試程式碼:

<code><?php
require dirname(__FILE__) . DIRECTORY_SEPARATOR . "messages" . DIRECTORY_SEPARATOR . "field_int32.proto.php";

$bytes = file_get_contents(dirname(__FILE__) . DIRECTORY_SEPARATOR . "fixtures" . DIRECTORY_SEPARATOR . "001_int32_init.bin");

$u = new Tutorial_Integer32();
$u->setValue(0);

$obj = ProtocolBuffers::decode("Tutorial_Integer32", $bytes);
var_dump($obj);
if ($obj instanceof Tutorial_Integer32) {
    var_dump($obj->getValue());
    if ($obj->getValue() == 0) {
        echo "OK" . PHP_EOL;
    } else {
        var_dump($obj);
    }
} else {
    var_dump($obj);
}

ini_set("protocolbuffers.native_scalars", 1);
$obj = ProtocolBuffers::decode("Tutorial_Integer32", $bytes);

if ($obj instanceof Tutorial_Integer32) {
    var_dump($obj->getValue());
    if ($obj->getValue() === 0) {
        echo "OK" . PHP_EOL;
    } else {
        var_dump($obj);
    }
} else {
    var_dump($obj);
}

</code>

輸出結果如下:

<code>object(Tutorial_Integer32)#2 (2) {
  ["_properties":protected]=>
  array(0) {
  }
  ["value":protected]=>
  string(1) "0"
}
NULL
OK
NULL
object(Tutorial_Integer32)#3 (2) {
  ["_properties":protected]=>
  array(0) {
  }
  ["value":protected]=>
  int(0)
}</code>

問題總結:在protocolbuffers::decode時,能夠傳回被解析的對象,但是對象的屬性無法讀取。求解中。 。 。

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn