<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                [TOC] ## 概述 php 的構造函數與c++ 的構造函數不同 php的構造函數在調用時候,類已經初始化,而c++卻并未初始化 ``` abstract class BASE { public function __construct() { $this->doSomething(); } public abstract function doSomething(); } class DERIVED extends BASE { public function doSomething() { echo ("doSomething()\n"); } } $d = new DERIVED(); ``` > 在 php 中上述運行可以,但在c++ 的構造時不能調用虛擬方法 ## 示例 ### hello world <details> <summary>main.cpp</summary> ``` #include <phpcpp.h> class Counter : public Php::Base { private: int _value = 0; public: Counter() = default; virtual ~Counter() = default; void __construct(Php::Parameters &params) { if (!params.empty()) _value = params[0]; } Php::Value increment() { return ++_value; } Php::Value decrement() { return --_value; } Php::Value value() const { return _value; } }; extern "C" { PHPCPP_EXPORT void *get_module() { static Php::Extension myExtension("my_extension", "1.0"); Php::Class<Counter> counter("Counter"); counter.method<&Counter::__construct>("__construct"); counter.method<&Counter::increment>("increment"); counter.method<&Counter::decrement>("decrement"); counter.method<&Counter::value>("value"); myExtension.add(std::move(counter)); return myExtension; } } ``` </details> <br /> 示例 ``` <?php $counter = new Counter(10); $counter->increment(); echo($counter->value()."\n");// 11 ``` ### 設置__construct 為私有,會報錯 在php 進行 new 時,會報錯,因為__construct是私有方法 ``` counter.method<&Counter::__construct>("__construct", Php::Private); ``` ### __clone 克隆對象 如果php類不想被clone 1. 您可以從類中刪除復制構造函數 2. 可以注冊一個私有__clone()方法 ``` counter.method("__clone", Php::Private); ``` ### c++調用c++對象,返回給php 推薦使用`Php::Object()`好于`Php::Value()` <details> <summary>main.cpp</summary> ``` #include <phpcpp.h> class Counter : public Php::Base { private: int _value = 0; public: Counter(int value) : _value(value) {} virtual ~Counter() = default; void __construct() {} Php::Value value() const { return _value; } }; Php::Value createTimer() { return Php::Object("Counter", new Counter(100)); } extern "C" { PHPCPP_EXPORT void *get_module() { static Php::Extension myExtension("my_extension", "1.0"); Php::Class<Counter> counter("Counter"); counter.method<&Counter::__construct>("__construct", Php::Private); counter.method<&Counter::value>("value"); myExtension.add(std::move(counter)); myExtension.add("createTimer", createTimer); return myExtension; } } ``` </details> <br />
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看