diff options
Diffstat (limited to 'documentation')
-rw-r--r-- | documentation/classes-and-objects.html | 4 | ||||
-rw-r--r-- | documentation/constructors-and-destructors.html | 97 |
2 files changed, 99 insertions, 2 deletions
diff --git a/documentation/classes-and-objects.html b/documentation/classes-and-objects.html index aab5468..63c9665 100644 --- a/documentation/classes-and-objects.html +++ b/documentation/classes-and-objects.html @@ -255,7 +255,7 @@ extern "C" { <h2>Abstract and final</h2> <p> In the previous section we showed how to use the Php::Final and Php::Abstract - modifiers to create a final or abstract method. If you want to make your entire + flags to create a final or abstract method. If you want to make your entire class abstract or final, you can do so by using Php::FinalClass or Php::AbstractClass instead of Php::Class. </p> @@ -281,7 +281,7 @@ extern "C" { It may seem strange that you have to pass in the address of a real C++ method when you mark a method as being abstract. Abstract methods do normally not have an implementation, so what do you need this C++ implementation for? - Luckily, we also have a different way for registering abstract methods. + Luckily, there also is a different way for registering abstract methods. </p> <p> <pre class="language-c++"><code> diff --git a/documentation/constructors-and-destructors.html b/documentation/constructors-and-destructors.html index 60ac49d..f9ec612 100644 --- a/documentation/constructors-and-destructors.html +++ b/documentation/constructors-and-destructors.html @@ -1 +1,98 @@ <h1>Constructors and destructors</h1> +<p> + There is a small but important difference between constructor 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 not in an initialized state yet. You can experience this by + calling a pure virtual method from a constructor. This will make your + program crash, even when the pure virtual method was implemented in the + derived class. The reason for this is that inside 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 pure virtual method + can thus not be passed on to the derived object. +</p> +<p> +<pre class="language-c++"><code> +#include <iostream> + +// define a base class with a pure virtual method that is called from the +// constructor +class BASE +{ +public: + // constructor + BASE() + { + // call the pure virtual method + doSomething(); + } + + // define method that should be implemented by derived classes + virtual void doSomething() = 0; +}; + +// define a derived class +class DERIVED : public BASE +{ + // implementation of the virtual function + virtual void doSomething() override + { + std::cout << "doSomething()" << std::endl; + } +} + +// main procedure +int main() +{ + DERIVED d; + return 0; +} +</code></pre> +</p> +<p> + The above program crashes. Unlike similar code in PHP. In PHP, when the + __construct() method gets called, the object is already fully initialized + and it is perfectly legal to make calls to abstract methods that are + implemented in derived classes. +</p> +<p> +<pre class="language-php"><code> +<?php + +// base class in PHP, we call an abstract method from the constructor +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(); +?> +</code></pre> +</p> +<p> + + The __construct() method is thus not part of + the object construction process (like the C++ constructor is), but simply is + the first method to be called <i>after</i> the object was fully initialized. +</p> |