summaryrefslogtreecommitdiff
path: root/documentation/constructors-and-destructors.html
blob: 399367a310602f4a9ebc81e1b6a3fc06eacf416b (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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
<h1>Constructors</h1>
<p>
    There is a small but very important difference between constructors and 
    destructors in C++, and the __construct() and __destruct() methods in PHP.
</p>
<p>
    A C++ constructor is called on an object that is <i>being</i> initialized,
    but that is <i>not</i> in an initialized state <i>yet</i>. You can experience 
    this by calling a virtual method from the constructor. Even when this virtual 
    method was overridden in a derived class, this will always execute the method 
    of the class itselves, and not the overridden implementation. The reason 
    for this is that during the call to the C++ constructor the object is not yet 
    fully initialized, and the object is not yet aware of it's position in the 
    class hierarchy. The call to the virtual method can thus not be passed 
    on to the derived object.
</p>
<p>
    In PHP however, the __construct() method has a different behavior. When
    it gets called, the object is already initialized and it is perfectly 
    legal to make calls to even abstract methods that are implemented in derived 
    classes. The following PHP script is completely valid - but it is impossible
    to do a similar thing in C++.
</p>
<p>
<pre class="language-php"><code>
&lt;?php

// base class in PHP, in which the an abstract method is called
abstract class BASE 
{
    // constructor
    public function __construct() 
    {
        // call abstract method
        $this->doSomething();
    }
    
    // abstract method to be implemented by derived classes
    public abstract function doSomething();
}

// the derived class
class DERIVED extends BASE 
{
    // implement the abstract method
    public function doSomething() 
    {
        echo("doSomething()\n");
    }
}

// create an instance of the derived class
$d = new DERIVED();
?&gt;    
</code></pre>
</p>
<p>
    This script outputs 'doSomething()'. The reason for this is that  __construct() 
    is not a constructor at all, but a very normal method that just happens to 
    be the first method that is called, and that is called automatically <i>after</i>
    the object is constructed.
</p>
<p>
    This difference is important for you as a C++ programmer, because you should
    never confuse your C++ constructor with the PHP __construct() method. In the C++
    constructor the object is being constructed and not all data is yet available. 
    Virtual methods can not be called, and the object also does not yet exist in
    PHP user space.
</p>
<p>
    After the constructor is finished, the PHP engine takes over control and creates 
    the PHP object, and the PHP-CPP library then links that PHP object to your C++ 
    object. Only after both the PHP object and the C++ object are fully constructed, 
    the __construct() method is called - just like a normal method. It is therefore 
    not uncommon to have both a C++ constructor and a __construct() method in your 
    class. The C++ constructor to initialize the member variables, and __construct() 
    to activate the object.
</p>
<p>
<pre class="language-c++"><code>
#include &lt;phpcpp.h&gt;

/**
 *  Simple counter class
 */
class Counter : public Php::Base
{
private:
    /**
     *  Internal value
     *  @var int
     */
    int _value = 0;

public:
    /**
     *  c++ constructor
     */
    Counter() {}
    
    /**
     *  c++ destructor
     */
    virtual ~Counter() {}
    
    /**
     *  php "constructor"
     *  @param  params
     */
    void __construct(Php::Parameters &amp;params)
    {
        // copy first parameter (if available)
        if (params.size() > 0) _value = params[0];
    }

    /**
     *  functions to increment and decrement
     */
    Php::Value increment() { return ++_value; }
    Php::Value decrement() { return --_value; }
    Php::Value value() const { return _value; }
};

/**
 *  Switch to C context so that the get_module() function can be
 *  called by C programs (which the Zend engine is)
 */
extern "C" {
    /**
     *  Startup function for the extension
     *  @return void*
     */
    PHPCPP_EXPORT void *get_module() {
        static Php::Extension myExtension("my_extension", "1.0");
        
        // description of the class so that PHP knows which methods are accessible
        Php::Class&lt;Counter&gt; counter("Counter");
        counter.method("__construct", &amp;Counter::__construct);
        counter.method("increment", &amp;Counter::increment);
        counter.method("decrement", &amp;Counter::decrement);
        counter.method("value", &amp;Counter::value);
        
        // add the class to the extension
        myExtension.add(std::move(counter));
        
        // return the extension
        return myExtension;
    }
}
</code></pre>
</p>
<p>
    The code above shows that __construct() is registered as if it was 
    a regular method - and that's what it is. The example that we've 
    used before (the one with the Counter class) is now extended so that it 
    is possible to give it an initial value to the counter by passing a 
    value to the "constructor".
</p>
<p>
<pre class="language-php"><code>
&lt;?php
$counter = new Counter(10);
$counter-&gt;increment();
echo($counter->value()."\n");
?&gt;
</code></pre>
</p>
<p>
    Because the __construct() method is seen as a regular method, you can also 
    specify its parameters, and whether the method is public, private or protected.
    The __construct() is also directly callable from PHP user space, so that
    derived methods can explicitly call parent::__construct().
</p>
<h2 id="private-constructors">Private constructors</h2>
<p>
    Just like any other method, the __construct() method can also be
    marked as being private or protected. If you do this, you will make it
    impossible to create instances of your class from PHP scripts. It is 
    important to realize that the C++ constructor and C++ destructor still get 
    called in such situations, because it is the __construct() call that is 
    going to fail - and not the actual object construction.
</p>
<p>
    Yes indeed: if you make the __construct() method private, and inside a PHP
    script a "new Counter()" call is executed, the PHP-CPP library will first
    instantiate a new instance of your class, then report an error because the
    __construct() method is private, and then immediately destruct the object
    (and call the C++ destructor).
</p>
<p>
<pre class="language-c++"><code>
#include &lt;phpcpp.h&gt;

/**
 *  Switch to C context so that the get_module() function can be
 *  called by C programs (which the Zend engine is)
 */
extern "C" {
    /**
     *  Start point of the extension
     *  @return void*
     */
    PHPCPP_EXPORT void *get_module() {
        static Php::Extension myExtension("my_extension", "1.0");
        
        // description of the class so that PHP knows which methods are accessible
        Php::Class&lt;Counter&gt; counter("Counter");
        
        // add a private __construct method to the class, so that objects can 
        // not be constructed from PHP scripts. Be aware that the C++ constructer 
        // does get called - it will be the call to the first __construct() 
        // function that will fail, and not the actual object construction.
        counter.method("__construct", &Counter::__construct, Php::Private);
        
        ...
    }
}
</code></pre>
</p>
<h2 id=cloning">Cloning objects</h2>
<p>
    If your class has a copy constructor, it automatically becomes clonable. If
    you do not want that your class can be cloned by PHP scripts, you can do
    two things: you can either remove the copy constructor from your class, or
    you can register a private __clone() method, just like we registered a 
    private __construct() method before.
</p>
<p>
<pre class="language-c++"><code>
#include &lt;phpcpp.h&gt;

/**
 *  Simple counter class
 */
class Counter : public Php::Base
{
private:
    /**
     *  Internal value
     *  @var int
     */
    int _value = 0;

public:
    /**
     *  c++ constructor
     */
    Counter() {}
    
    /**
     *  Remove the copy constructor
     *  
     *  By removing the copy constructor, the PHP clone operator will
     *  automatically be deactivated. PHP will trigger an error if 
     *  an object is attempted to be cloned.
     *  
     *  @param  counter
     */
    Counter(const Counter &amp;counter) = delete;
    
    /**
     *  c++ destructor
     */
    virtual ~Counter() {}
    
    /**
     *  php "constructor"
     *  @param  params
     */
    void __construct(Php::Parameters &amp;params)
    {
        // copy first parameter (if available)
        if (params.size() > 0) _value = params[0];
    }

    /**
     *  functions to increment and decrement
     */
    Php::Value increment() { return ++_value; }
    Php::Value decrement() { return --_value; }
    Php::Value value() const { return _value; }
};

/**
 *  Switch to C context so that the get_module() function can be
 *  called by C programs (which the Zend engine is)
 */
extern "C" {
    /**
     *  Startup function for the extension
     *  @return void*
     */
    PHPCPP_EXPORT void *get_module() {
        static Php::Extension myExtension("my_extension", "1.0");
        
        // description of the class so that PHP knows which methods are accessible
        Php::Class&lt;Counter&gt; counter("Counter");
        counter.method("__construct", &Counter::__construct);
        counter.method("increment", &Counter::increment);
        counter.method("decrement", &Counter::decrement);
        counter.method("value", &Counter::value);
        
        // alternative way to make an object unclonable
        counter.method("__clone", Php::Private);
        
        // add the class to the extension
        myExtension.add(std::move(counter));
        
        // return the extension
        return myExtension;
    }
}
</code></pre>
</p>
<p>
    In the above example we have shown both ways to make object unclonable. 
    Using only one of them is already sufficient.    
</p>
<h2 id="constructing-objects">Constructing objects</h2>
<p>
    The Php::Value class can be used as a regular PHP $variable, and you can therefore
    also use it for storing object instances. But how do you create brand
    new objects? For this we have the Php::Object class - which is simply an
    overridden Php::Value class with alternative constructors, and some additional 
    checks to prevent that you will ever use a Php::Object object to store values
    other than objects.
</p>
<p>
<pre class="language-c++"><code>
// new variable holding the string "Counter"
Php::Value counter0("Counter");

// new variable holding a newly created object of type "Counter",
// the __construct() gets called without parameters
Php::Object counter1("Counter");

// new variable holding a newly created object, and 
// the __construct() is being called with value 10
Php::Object counter2("Counter", 10);

// new built-in DateTime object, constructed with "now"
Php::Object time("DateTime", "now");

// valid, a Php::Object is an extended Php::Value, and 
// can thus be assigned to a base Php::Value object
Php::Value copy1 = counter1;

// invalid statement, a Php::Object can only be used for storing objects
Php::Object copy2 = counter0;
</code></pre>
</p>
<p>
    The constructor of a Php::Object takes the name of a class, and an optional
    list of parameters that will be passed to the __construct() function. You 
    can use names from built-in PHP classes and other extensions (like DateTime), 
    classes from your extension (like Counter), and even classes from PHP user 
    space.
</p>
<p>
    The Php::Object class can also be used if you want to construct an instance
    of your own C++ class without calling the __construct() function. This can 
    for example be useful when the __construct() method is private, or when you
    want to bypass a call to your own __construct() method.
</p>
<p>
<pre class="language-c++"><code>
#include &lt;phpcpp.h&gt;

// actual class implementation
class Counter : public Php::Base
{
private:
    int _value = 0;

public:
    // c++ constructor
    Counter(int value) : _value(value) {}
    
    // c++ destructor
    virtual ~Counter() {}
    
    // php "constructor"
    void __construct() {}

    // functions to increment and decrement
    Php::Value value() const { return _value; }
};

// function to create a new timer
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");
        
        // description of the class so that PHP knows which methods are accessible,
        // the __construct method is private because PHP scripts are not allowed
        // to create Counter instances
        Php::Class&lt;Counter&gt; counter("Counter");
        counter.method("__construct", &Counter::__construct, Php::Private);
        counter.method("value", &Counter::value);
        
        // add the class to the extension
        myExtension.add(std::move(counter));
        
        // add the factory function to create a timer to the extension
        myExtension.add("createTimer", createTimer);
        
        // return the extension
        return myExtension;
    }
}
</code></pre>
</p>
<p>
    In the code above we made the __construct() function of the Counter class
    private. This makes it impossible to create instances of this class - both
    from PHP user scripts, and via calls to Php::Object("Counter") - because
    constructing objects in these ways will eventually result in a forbidden 
    __construct() call.
</p>
<p>
    The Php::Object does have an alternative syntax that takes a pointer 
    to a C++ class (allocated on the heap, with operator new!) and that turns 
    this pointer into a PHP variable without calling the 
    __construct() method. Notice that you must also specify the classname, 
    because C++ classes do not hold any information about themselves (like their 
    name), while in PHP such information is required to handle reflection and 
    functions like get_class().
</p>