Skip to content

Commit 884316c

Browse files
explained how to specify class constants
1 parent d41e5b4 commit 884316c

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

documentation/properties.html

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,49 @@ <h2 id="normal-members">Normal member variables</h2>
148148
properties, but even that is probably not what you want, as storing
149149
data in native C++ variables is much faster.
150150
</p>
151+
<h2 id="constants">Class constants</h2>
152+
<p>
153+
Class constants can be defined in a similar way as properties. The only
154+
difference is that you have to pass in the flag 'Php::Const' instead of
155+
one of the public, private or protected access modifiers.
156+
</p>
157+
<p>
158+
<pre class="language-cpp"><code>
159+
#include &lt;phpcpp.h&gt;
160+
161+
// @todo you class definition
162+
163+
/**
164+
* Switch to C context so that the get_module() function can be
165+
* called by C programs (which the Zend engine is)
166+
*/
167+
extern "C" {
168+
/**
169+
* Startup function for the extension
170+
* @return void*
171+
*/
172+
PHPCPP_EXPORT void *get_module() {
173+
// create static extension object
174+
static Php::Extension myExtension("my_extension", "1.0");
175+
176+
// description of the class so that PHP knows which methods are accessible
177+
Php::Class&lt;Example&gt; example("Example");
178+
179+
// the Example class has a class constant
180+
example.property("MY_CONSTANT", "some value", Php::Const);
181+
182+
// add the class to the extension
183+
myExtension.add(std::move(example));
184+
185+
// return the extension
186+
return myExtension;
187+
}
188+
}
189+
</code></pre>
190+
</p>
191+
<p>
192+
The class constant can be accessed from PHP scripts using Example::MY_CONSTANT.
193+
</p>
151194
<h2>Smart properties</h2>
152195
<p>
153196
With the <a href="magic-methods">magic methods __get() and __set()</a> you

0 commit comments

Comments
 (0)