diff options
author | Emiel Bruijntjes <emiel.bruijntjes@copernica.com> | 2014-03-01 22:39:31 +0100 |
---|---|---|
committer | Emiel Bruijntjes <emiel.bruijntjes@copernica.com> | 2014-03-01 22:39:31 +0100 |
commit | 6abc8b4c062c7333a98830004da894ff81613d5b (patch) | |
tree | 7b11179ef6775130fce0c4ef537fb3656f716852 /Examples | |
parent | abc3b4fbf996a647bcefb02e4ecf643b659577c9 (diff) |
added possibility to define interfaces, the class::add() method has been renamed to class::method() and class::property() to prevent ambiguity in defining properties and methods
Diffstat (limited to 'Examples')
-rw-r--r-- | Examples/CppClassesInPhp/cppclassinphp.cpp | 41 | ||||
-rw-r--r-- | Examples/CppClassesInPhp/cppclassinphp.php | 11 |
2 files changed, 24 insertions, 28 deletions
diff --git a/Examples/CppClassesInPhp/cppclassinphp.cpp b/Examples/CppClassesInPhp/cppclassinphp.cpp index ebc4969..23f86bf 100644 --- a/Examples/CppClassesInPhp/cppclassinphp.cpp +++ b/Examples/CppClassesInPhp/cppclassinphp.cpp @@ -41,26 +41,14 @@ public: void myMethod(Php::Parameters ¶ms) { + // check number of parameters + if (params.size() != 1) throw Php::Exception("Invalid number of parameters supplied"); + std::cout << "myMethod is called." << std::endl; - std::cout << "_x: " << _x << std::endl; _x = params[0]; - std::cout << "New _x" << _x << std::endl; - - Php::Value v = params[0]; - - std::cout << "contains: " << v.contains("bla") << std::endl; - std::cout << "value: " << v["bla"] << std::endl; - - v["something"] = "something else"; } }; -void myFunction(Php::Parameters ¶ms) -{ - std::cout << "regular function" << std::endl; -} - - // Symbols are exported according to the "C" language extern "C" { @@ -70,26 +58,29 @@ extern "C" // create extension static Php::Extension extension("Cpp_classes_in_php","1.0"); - // create a namespace too - Php::Namespace ns("MyNamespace"); + // build an interface + Php::Interface interface("MyInterface"); - // add custom function - extension.add("myFunction", myFunction, { }); + // add methods to the interface + interface.method("method1"); + interface.method("method2"); + + // add the interface to the extension + extension.add(interface); // we are going to define a class Php::Class<MyCustomClass> customClass("MyClass"); // add methods to it - customClass.add("myMethod", &MyCustomClass::myMethod, Php::Final, {}); - customClass.add("property1", "prop1"); - customClass.add("property2", "prop2", Php::Protected); + customClass.method("myMethod", &MyCustomClass::myMethod, Php::Final, {}); + customClass.method("myMethod2", &MyCustomClass::myMethod); + customClass.property("property1", "prop1"); + customClass.property("property2", "prop2", Php::Protected); + customClass.method("myAbstract"); // add the class to the extension extension.add(customClass); - // add the namespace to the extension - extension.add(ns); - // return the extension module return extension.module(); } diff --git a/Examples/CppClassesInPhp/cppclassinphp.php b/Examples/CppClassesInPhp/cppclassinphp.php index cf1e42c..c2076d3 100644 --- a/Examples/CppClassesInPhp/cppclassinphp.php +++ b/Examples/CppClassesInPhp/cppclassinphp.php @@ -6,12 +6,17 @@ * An example file to show the working of using a C++ class in PHP. */ -// run the regular function -myFunction(); - //create a MyCustomClass object, which is an object of a C++ class $object = new MyClass(); +class ImplementedInterface implements MyInterface +{ +} + +$object2 = new ImplementedInterface(); + +return; + // run a function of the class $object->myMethod(1); |