<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>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                [TOC] ## 概述 魔術方法中(__set,__get等)除了`__construct`,其他方法都可以不進行注冊 `__onstruct`必須顯示進行說明是因為: 1. 該__construct()方法沒有固定的簽名 2. 通過將其顯式添加到擴展中,還可以指定它接受的參數 3. 該__construct()方法是公共,私有還是受保護的 ## 示例 ### 偽屬性(__get,__set,__isset,__unset) <details> <summary>main.cpp</summary> ``` #include <phpcpp.h> class User : public Php::Base { private: std::string _name; std::string _email; public: User() = default; virtual ~User() = default; // 參數使用 Php::Value 而不是 Php::Parameters // 是因為 __get 方法只能為一個參數 Php::Value __get(const Php::Value &name) { if (name == "name") return _name; if (name == "email") return _email; return Php::Base::__get(name); } void __set(const Php::Value &name, const Php::Value &value) { if (name == "name") { // 使用 stringValue() 函數把 value 轉為 字符串 _name = value.stringValue(); } else if (name == "email") { std::string email = value; if (email.find('@') == std::string::npos) { throw Php::Exception("Invalid email address"); } _email = email; } else { // 默認處理 Php::Base::__set(name, value); } } bool __isset(const Php::Value &name) { if (name == "name" || name == "email") return true; return Php::Base::__isset(name); } void __unset(const Php::Value &name) { if (name == "name" || name == "email") { throw Php::Exception("Name and email address can not be removed"); } Php::Base::__unset(name); } }; extern "C" { PHPCPP_EXPORT void *get_module() { static Php::Extension myExtension("my_extension", "1.0"); Php::Class<User> user("User"); myExtension.add(std::move(user)); return myExtension; } } ``` </details> <br /> <details> <summary>main.php</summary> ``` <?php $user = new User(); // 調用 __set $user->name = "John Doe"; $user->email = "john.doe@example.com"; // 調用 __get echo ($user->email . "\n"); // john.doe@example.com // 調用 __isset var_dump(isset($user->name)); // bool(true) // 調用 __unset // 由于在c++中設置了移除email會拋異常 try { unset($user->email); } catch (Exception $e) print_r($e->getMessage()); //Name and email address can not be removed% } ``` </details> <br /> ### 魔術方法__call(),__callStatic()和__invoke() <details> <summary>main.cpp</summary> ``` #include <phpcpp.h> #include <iostream> class MyClass : public Php::Base { public: MyClass() = default; virtual ~MyClass() = default; Php::Value regular(Php::Parameters &params) { return "this is a regular method"; } Php::Value __call(const char *name, Php::Parameters &params) { std::string retval = std::string("__call ") + name; for (auto &param : params) { retval += " " + param.stringValue(); } return retval; } // 報錯,無法實現 static Php::Value __callStatic(const char *name, Php::Parameters &params) { Php::out<<"123"<<std::endl; // std::string retval = std::string("__callStatic ") + name; // for (auto &param : params) // { // retval += " " + param.stringValue(); // } return 1; } // 報錯,無法實現 Php::Value __invoke(Php::Parameters &params) { // std::string retval = "invoke"; // for (auto &param : params) // { // retval += " " + param.stringValue(); // } // return retval; Php::out<<"123"<<std::endl; } }; extern "C" { PHPCPP_EXPORT void *get_module() { static Php::Extension myExtension("my_extension", "1.0"); Php::Class<MyClass> myClass("MyClass"); myClass.method<&MyClass::regular>("regular"); myExtension.add(std::move(myClass)); return myExtension; } } ``` </details> <br /> <details> <summary>main.php</summary> ``` <?php $object = new MyClass(); echo($object->regular()."\n"); // this is a regular method // 調用 __call echo($object->something()."\n"); // __call something echo($object->myMethod(1,2,3,4)."\n"); // __call myMethod 1 2 3 4 echo($object->whatever("a","b")."\n"); // __call whatever a b // 調用 __callStatic 但是失敗了 MyClass::something()."\n"; echo(MyClass::myMethod(5,6,7)."\n"); echo(MyClass::whatever("x","y")."\n"); // 調用 __invoke 單是失敗了 echo($object("parameter","passed","to","invoke")."\n"); ``` </details> <br /> ### __toString <details> <summary>main.cpp</summary> ``` #include <phpcpp.h> #include <iostream> class MyClass : public Php::Base { public: MyClass() = default; virtual ~MyClass() = default; Php::Value __toString(){ return "123"; } }; extern "C" { PHPCPP_EXPORT void *get_module() { static Php::Extension my_extension("my_extension","1.0.0"); Php::Class<MyClass> myclass("MyClass"); my_extension.add(std::move(myclass)); return my_extension; } } ``` </details> <br /> <details> <summary>main.php</summary> ``` <?php $object = new MyClass(); echo $object; //123 ``` </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>

                              哎呀哎呀视频在线观看