diff options
author | Emiel Bruijntjes <emiel.bruijntjes@copernica.com> | 2014-03-14 09:47:54 +0100 |
---|---|---|
committer | Emiel Bruijntjes <emiel.bruijntjes@copernica.com> | 2014-03-14 09:47:54 +0100 |
commit | 159781ee8257329ca9c40306f7495a8c2f31f710 (patch) | |
tree | 4d465797f334e27a836c0c41b4aa82d4fe5586ab | |
parent | c9274ab3c422390998e628820afc6a27c12a1a57 (diff) |
update documentation, added super.h and super.cpp files that I forgot in previous commit
-rw-r--r-- | documentation/variables.html | 22 | ||||
-rw-r--r-- | include/super.h | 76 | ||||
-rw-r--r-- | src/super.cpp | 59 |
3 files changed, 155 insertions, 2 deletions
diff --git a/documentation/variables.html b/documentation/variables.html index 6b63598..b3f4330 100644 --- a/documentation/variables.html +++ b/documentation/variables.html @@ -286,7 +286,7 @@ std::cout << array("Y-m-d H:i:s") << std::endl; <h2 id="global-variables">Global variables</h2> <p> To read or update global PHP variables, you can use the Php::GLOBALS - variable. This variable works more or less the same as the $_GLOBALS + variable. This variable works more or less the same as the $GLOBALS variable in a PHP script. </p> <p> @@ -305,7 +305,25 @@ std::cout << Php::GLOBALS["b"] << std::endl; </code></pre> </p> <p> - Unlike PHP scripts, that handles only single pageviews, an extension is + Next to the $GLOBALS variable, PHP allows you to access variables using + the $_GET, $_POST, $_COOKIE, $_FILES, $_SERVER, $_REQUEST and $_ENV variables. + In your C++ extension you can do something similar with the global variables + Php::GET, Php::POST, Php::COOKIE, Php::FILES, Php::SERVER, Php::REQUEST and + Php::ENV. These are global, read-only, objects with an overloaded operator[] + method. You can thus access them as if it were associative arrays. +</p> +<p> +<pre class="language-c++"><code> +// retrieve the value of a request variable +int age = Php::REQUEST["name"]; + +// or retrieve the value of a server variable +std::string referer = Php::SERVER["HTTP_REFERER"]; +</code></pre> +</p> +<h2 id="global-natives">Be careful with global C++ variables</h2> +<p> + Unlike PHP scripts, that only handle single pageviews, an extension is used to handle multiple pageviews after each other. This means that when you use global C++ (!) variables in your extension, that these variables are not set back to their initial value in between the pageviews. The diff --git a/include/super.h b/include/super.h new file mode 100644 index 0000000..73e3761 --- /dev/null +++ b/include/super.h @@ -0,0 +1,76 @@ +/** + * Super.h + * + * The Super class is used to implement one of the super variables $_POST, + * $_GET, $_SERVER, et cetera + * + * @copyright 2014 Copernica BV + * @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com> + */ + +/** + * Set up namespace + */ +namespace Php { + +/** + * Class definition + */ +class Super +{ +public: + /** + * Constructor + * + * Extension writers do not have to access the super-globals themselves. + * They are always accessible via Php::POST, Php::GET, et cetera. + * + * @param index number + */ + Super(int index) : _index(index) {} + + /** + * Destructor + */ + virtual ~Super() {} + + /** + * Array access operator + * This can be used for accessing associative arrays + * @param key + * @return Value + */ + Value operator[](const std::string &key) const; + + /** + * Array access operator + * This can be used for accessing associative arrays + * @param key + * @return Value + */ + Value operator[](const char *key) const; + +private: + /** + * Index number + * @var int + */ + int _index; +}; + +/** + * A number of super-globals are always accessible + */ +extern Super POST; +extern Super GET; +extern Super COOKIE; +extern Super SERVER; +extern Super ENV; +extern Super FILES; +extern Super REQUEST; + +/** + * End namespace + */ +} + diff --git a/src/super.cpp b/src/super.cpp new file mode 100644 index 0000000..6b02916 --- /dev/null +++ b/src/super.cpp @@ -0,0 +1,59 @@ +/** + * Super.cpp + * + * @copyright 2014 Copernica BV + * @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com> + */ +#include "includes.h" + +/** + * Set up namespace + */ +namespace Php { + +/** + * A number of super-globals are always accessible + */ +Super POST (TRACK_VARS_POST); +Super GET (TRACK_VARS_GET); +Super COOKIE (TRACK_VARS_COOKIE); +Super SERVER (TRACK_VARS_SERVER); +Super ENV (TRACK_VARS_ENV); +Super FILES (TRACK_VARS_FILES); +Super REQUEST (TRACK_VARS_REQUEST); + +/** + * Array access operator + * This can be used for accessing associative arrays + * @param key + * @return Value + */ +Value Super::operator[](const std::string &key) const +{ + // create a value object that wraps around the actual zval + Value value(PG(http_globals)[_index]); + + // pass on the call + return value[key]; +} + +/** + * Array access operator + * This can be used for accessing associative arrays + * @param key + * @return Value + */ +Value Super::operator[](const char *key) const +{ + // create a value object that wraps around the actual zval + Value value(PG(http_globals)[_index]); + + // pass on the call + return value[key]; +} + +/** + * End namespace + */ +} + |