summaryrefslogtreecommitdiff
path: root/Examples/CppClassesInPhp/cppclassinphp.cpp
blob: 400d9429eb7137ce5cdb66e557a039b78bffb2dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/**
 *  cppclassinphp.cpp
 *  @author Jasper van Eck<jasper.vaneck@copernica.com>
 * 
 *  An example file to show the working of using a C++ class in PHP.
 */

#include "includeMyCustomClass.h"
/**
 *  Namespace to use
 */
using namespace std;

class MyCustomClass : public Php::Base // , public Php::Countable
{
private:
    int _x = 3;
    
public:
    MyCustomClass()
    {
        std::cout << "MyCustomClass::MyCustomClass()" << std::endl;
    }

    MyCustomClass(int value) : _x(value)
    {
        std::cout << "MyCustomClass::MyCustomClass(" << value << ")" << std::endl;
    }

    MyCustomClass(const MyCustomClass &that)
    {
        std::cout << "MyCustomClass::MyCustomClass copy constructor" << std::endl;
    }
    
    virtual ~MyCustomClass()
    {
        std::cout << "MyCustomClass::~MyCustomClass" << std::endl;
    }

    virtual void __construct()
    {
        std::cout << "MyCustomClass::__construct" << std::endl;
    }

    virtual void __destruct()
    {
        std::cout << "MyCustomClass::__destruct" << std::endl;
    }
    
    virtual Php::Value count() //override
    {
        return 33;
    }
    
    Php::Value myMethod(Php::Parameters &params)
    {
        // check number of parameters
        if (params.size() != 1) throw Php::Exception("Invalid number of parameters supplied");
        
        std::cout << "myMethod is called for object " << _x << std::endl;

        std::cout << "property1: " << property("property1") << std::endl;


        return Php::Object("MyClass", new MyCustomClass(100));


        return false;


        // create a new PHP DateTime object representing the current time
        Php::Object now("DateTime", "now");

        // show it
        std::cout << "current time: " << now.call("format", "Y-m-d H:i:s") << std::endl;
        
        std::cout << "construct " << params[0] << std::endl;

        // construct a new class
        Php::Object obj(params[0]);

        std::cout << "return " << params[0] << std::endl;
        
        
        
        
        // return it
        return obj;
        
        
   //     std::cout << "get property1 " << value()["property1"] << std::endl;
   //     
   //     // set it to something else
   //     value().set("property1", "new value");
   //
   //     std::cout << "get property1 " << value()["property1"] << std::endl;
    }
};

// Symbols are exported according to the "C" language
extern "C" 
{
    // export the "get_module" function that will be called by the Zend engine
    PHPCPP_EXPORT void *get_module()
    {
        // create extension
        static Php::Extension extension("Cpp_classes_in_php","1.0");
        
        // build an interface
        Php::Interface interface("MyInterface");
        
        // 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.method("myMethod", &MyCustomClass::myMethod, Php::Final, {});
        customClass.method("myMethod2", &MyCustomClass::myMethod);
        customClass.property("property1", "prop1");
        customClass.property("property2", "prop2", Php::Protected);
        
        // add the class to the extension
        extension.add(customClass);
        
        // return the extension module
        return extension;
    }
}