summaryrefslogtreecommitdiff
path: root/Examples/Globals
diff options
context:
space:
mode:
authorEmiel Bruijntjes <emiel.bruijntjes@copernica.com>2013-12-07 13:12:25 -0800
committerEmiel Bruijntjes <emiel.bruijntjes@copernica.com>2013-12-07 13:12:25 -0800
commit80c8a16fb145f7ed4867d31fad3c22318a1ce708 (patch)
treef7605cdb86d653efaac761363ed596dd6b2e0459 /Examples/Globals
parent964d6274b0eba38df43d77b87c44bd728d2f0fb5 (diff)
replaces tabs in source code with regular spaces, added example for working with global variables
Diffstat (limited to 'Examples/Globals')
-rw-r--r--Examples/Globals/globals.cpp60
-rw-r--r--Examples/Globals/globals.php29
2 files changed, 89 insertions, 0 deletions
diff --git a/Examples/Globals/globals.cpp b/Examples/Globals/globals.cpp
new file mode 100644
index 0000000..6fa9eea
--- /dev/null
+++ b/Examples/Globals/globals.cpp
@@ -0,0 +1,60 @@
+/**
+ * functionvoid.cpp
+ * @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com>
+ *
+ * An example file to show how global variables can be accessed
+ */
+
+/**
+ * Libraries used.
+ */
+#include <iostream>
+#include <phpcpp.h>
+
+/**
+ * Namespace to use
+ */
+using namespace std;
+
+/**
+ * process_globals()
+ *
+ * This function reads and modifies global variables
+ */
+Php::Value process_globals()
+{
+ // all global variables can be accessed via the Php::globals variable,
+ // which is more or less the same as the PHP $_GLOBALS variable
+
+ // set a global variable
+ Php::globals["a"] = 1;
+
+ // increment a global variable
+ Php::globals["b"] += 1;
+
+ // set a global variable to be an array
+ Php::globals["c"] = Php::Array();
+
+ // add a member to an array
+ Php::globals["c"]["member"] = 123;
+
+ // if a global variable holds a function, we can call it
+ return Php::globals["d"](1,2,3);
+}
+
+// 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("globals","1.0");
+
+ // add function to extension
+ extension.add("process_globals", process_globals);
+
+ // return the extension module
+ return extension.module();
+ }
+}
diff --git a/Examples/Globals/globals.php b/Examples/Globals/globals.php
new file mode 100644
index 0000000..36febf6
--- /dev/null
+++ b/Examples/Globals/globals.php
@@ -0,0 +1,29 @@
+<?php
+/*
+ * globals.php
+ * @author Jasper van Eck<jasper.vaneck@copernica.com>
+ *
+ * An example file to show the working of a void function call.
+ */
+
+// we first need to set a number of globals
+$b = 10;
+$d = function($a,$b,$c) {
+ return $a+$b+$c;
+};
+
+// call the C++ function that will do something
+$d = process_globals();
+
+// the global variable $a should not have the value 1
+echo("a = $a\n");
+
+// the variable $b should not be 11
+echo("b = $b\n");
+
+// $c should be an array
+echo("c['member'] = ".$c['member']."\n");
+
+// $d is overwritten and now is 6
+echo("d = $d\n");
+