From ceac7c034aab703772231ea3b8375abec67cd3d9 Mon Sep 17 00:00:00 2001 From: Emiel Bruijntjes Date: Tue, 23 Jun 2015 09:51:20 +0200 Subject: Added extra example script (i thought this was a bug, but in this small setup thinks work as expected) --- Examples/EmptyExtension/Makefile | 2 +- Examples/ReturnObject/Makefile | 135 +++++++++++++++++++++++++++++++++ Examples/ReturnObject/child.h | 36 +++++++++ Examples/ReturnObject/main.cpp | 37 +++++++++ Examples/ReturnObject/master.h | 65 ++++++++++++++++ Examples/ReturnObject/returnobject.ini | 1 + Examples/ReturnObject/test.php | 36 +++++++++ 7 files changed, 311 insertions(+), 1 deletion(-) create mode 100644 Examples/ReturnObject/Makefile create mode 100644 Examples/ReturnObject/child.h create mode 100644 Examples/ReturnObject/main.cpp create mode 100644 Examples/ReturnObject/master.h create mode 100644 Examples/ReturnObject/returnobject.ini create mode 100644 Examples/ReturnObject/test.php diff --git a/Examples/EmptyExtension/Makefile b/Examples/EmptyExtension/Makefile index 76d161d..deb78f2 100644 --- a/Examples/EmptyExtension/Makefile +++ b/Examples/EmptyExtension/Makefile @@ -29,7 +29,7 @@ NAME = yourextension # one for each extension. Use this variable to specify this directory. # -INI_DIR = /etc/php5/conf.d +INI_DIR = /etc/php5/mods-available/ # diff --git a/Examples/ReturnObject/Makefile b/Examples/ReturnObject/Makefile new file mode 100644 index 0000000..526e4bc --- /dev/null +++ b/Examples/ReturnObject/Makefile @@ -0,0 +1,135 @@ +# +# Makefile template +# +# This is an example Makefile that can be used by anyone who is building +# his or her own PHP extensions using the PHP-CPP library. +# +# In the top part of this file we have included variables that can be +# altered to fit your configuration, near the bottom the instructions and +# dependencies for the compiler are defined. The deeper you get into this +# file, the less likely it is that you will have to change anything in it. +# + +# +# Name of your extension +# +# This is the name of your extension. Based on this extension name, the +# name of the library file (name.so) and the name of the config file (name.ini) +# are automatically generated +# + +NAME = returnobject + + +# +# Php.ini directories +# +# In the past, PHP used a single php.ini configuration file. Today, most +# PHP installations use a conf.d directory that holds a set of config files, +# one for each extension. Use this variable to specify this directory. +# + +INI_DIR = /etc/php5/mods-available/ + + +# +# The extension dirs +# +# This is normally a directory like /usr/lib/php5/20121221 (based on the +# PHP version that you use. We make use of the command line 'php-config' +# instruction to find out what the extension directory is, you can override +# this with a different fixed directory +# + +EXTENSION_DIR = $(shell php-config --extension-dir) + + +# +# The name of the extension and the name of the .ini file +# +# These two variables are based on the name of the extension. We simply add +# a certain extension to them (.so or .ini) +# + +EXTENSION = ${NAME}.so +INI = ${NAME}.ini + + +# +# Compiler +# +# By default, the GNU C++ compiler is used. If you want to use a different +# compiler, you can change that here. You can change this for both the +# compiler (the program that turns the c++ files into object files) and for +# the linker (the program that links all object files into the single .so +# library file. By default, g++ (the GNU C++ compiler) is used for both. +# + +COMPILER = g++ +LINKER = g++ + + +# +# Compiler and linker flags +# +# This variable holds the flags that are passed to the compiler. By default, +# we include the -O2 flag. This flag tells the compiler to optimize the code, +# but it makes debugging more difficult. So if you're debugging your application, +# you probably want to remove this -O2 flag. At the same time, you can then +# add the -g flag to instruct the compiler to include debug information in +# the library (but this will make the final libphpcpp.so file much bigger, so +# you want to leave that flag out on production servers). +# +# If your extension depends on other libraries (and it does at least depend on +# one: the PHP-CPP library), you should update the LINKER_DEPENDENCIES variable +# with a list of all flags that should be passed to the linker. +# + +COMPILER_FLAGS = -Wall -c -O2 -std=c++11 -fpic -o +LINKER_FLAGS = -shared +LINKER_DEPENDENCIES = -lphpcpp + + +# +# Command to remove files, copy files and create directories. +# +# I've never encountered a *nix environment in which these commands do not work. +# So you can probably leave this as it is +# + +RM = rm -f +CP = cp -f +MKDIR = mkdir -p + + +# +# All source files are simply all *.cpp files found in the current directory +# +# A builtin Makefile macro is used to scan the current directory and find +# all source files. The object files are all compiled versions of the source +# file, with the .cpp extension being replaced by .o. +# + +SOURCES = $(wildcard *.cpp) +OBJECTS = $(SOURCES:%.cpp=%.o) + + +# +# From here the build instructions start +# + +all: ${OBJECTS} ${EXTENSION} + +${EXTENSION}: ${OBJECTS} + ${LINKER} ${LINKER_FLAGS} -o $@ ${OBJECTS} ${LINKER_DEPENDENCIES} + +${OBJECTS}: + ${COMPILER} ${COMPILER_FLAGS} $@ ${@:%.o=%.cpp} + +install: + ${CP} ${EXTENSION} ${EXTENSION_DIR} + ${CP} ${INI} ${INI_DIR} + +clean: + ${RM} ${EXTENSION} ${OBJECTS} + diff --git a/Examples/ReturnObject/child.h b/Examples/ReturnObject/child.h new file mode 100644 index 0000000..b9e8d9c --- /dev/null +++ b/Examples/ReturnObject/child.h @@ -0,0 +1,36 @@ +/** + * Child.h + * + * Class that is exported to PHP space + * + * @author Emiel Bruijntjes + * @copyright 2015 Copernica BV + */ + +/** + * Include guard + */ +#pragma once + +/** + * Class definition + */ +class Child : public Php::Base +{ +public: + /** + * Constructor and destructor + */ + Child() {} + virtual ~Child() {} + + /** + * Cast to a string + * @return const char * + */ + const char *__toString() const + { + return "this is the child"; + } +}; + diff --git a/Examples/ReturnObject/main.cpp b/Examples/ReturnObject/main.cpp new file mode 100644 index 0000000..6ad3552 --- /dev/null +++ b/Examples/ReturnObject/main.cpp @@ -0,0 +1,37 @@ +#include +#include "master.h" +#include "child.h" + +/** + * tell the compiler that the get_module is a pure C function + */ +extern "C" { + + /** + * Function that is called by PHP right after the PHP process + * has started, and that returns an address of an internal PHP + * strucure with all the details and features of your extension + * + * @return void* a pointer to an address that is understood by PHP + */ + PHPCPP_EXPORT void *get_module() + { + // static(!) Php::Extension object that should stay in memory + // for the entire duration of the process (that's why it's static) + static Php::Extension extension("returnobjecy", "1.0"); + + // we have to class - master and child + Php::Class master("master"); + Php::Class child("child"); + + // the master class has one method - to return a child + master.method("child", &Master::child); + + // add all classes to the extension + extension.add(master); + extension.add(child); + + // return the extension + return extension; + } +} diff --git a/Examples/ReturnObject/master.h b/Examples/ReturnObject/master.h new file mode 100644 index 0000000..b4f9ceb --- /dev/null +++ b/Examples/ReturnObject/master.h @@ -0,0 +1,65 @@ +/** + * Master.h + * + * Class that is exported to PHP space + * + * @author Emiel Bruijntjes + * @copyright 2015 Copernica BV + */ + +/** + * Include guard + */ +#pragma once + +/** + * Dependencies + */ +#include "child.h" + +/** + * Class definition + */ +class Master : public Php::Base +{ +private: + /** + * One child + * @var Php::Value + */ + Php::Value _value; + +public: + /** + * Constructor + */ + Master() + { + // create a child instance + _value = Php::Object("Child", new Child()); + } + + /** + * Destructor + */ + virtual ~Master() {} + + /** + * Retrieve the child + * @return Php::Value + */ + Php::Value child() const + { + return _value; + } + + /** + * Cast to a string + * @return const char * + */ + const char *__toString() const + { + return "this is the master"; + } +}; + diff --git a/Examples/ReturnObject/returnobject.ini b/Examples/ReturnObject/returnobject.ini new file mode 100644 index 0000000..db0d481 --- /dev/null +++ b/Examples/ReturnObject/returnobject.ini @@ -0,0 +1 @@ +extension=returnobject.so diff --git a/Examples/ReturnObject/test.php b/Examples/ReturnObject/test.php new file mode 100644 index 0000000..8fa8b6c --- /dev/null +++ b/Examples/ReturnObject/test.php @@ -0,0 +1,36 @@ + + * @copyright 2015 Copernica BV + */ + +/** + * Construct master object + * @var Master + */ +$master = new Master(); + +/** + * Construct derived child object + * @var Child + */ +$child1 = new Child(); + +/** + * Fetch the child object that is stored as member var in the master + * @var Child + */ +$child2 = $master->child(); + +/** + * Show output, expected is: + * this is the master + * this is the child + * this is the child + */ +echo(strval($master)."\n"); +echo(strval($child1)."\n"); +echo(strval($child2)."\n"); + -- cgit v1.2.3