Skip to content

Commit

Permalink
replaces tabs in source code with regular spaces, added example for w…
Browse files Browse the repository at this point in the history
…orking with global variables
  • Loading branch information
EmielBruijntjes committed Dec 7, 2013
1 parent 964d627 commit 80c8a16
Show file tree
Hide file tree
Showing 30 changed files with 748 additions and 481 deletions.
48 changes: 24 additions & 24 deletions Examples/CallPhpFunctions/callphpfunction.cpp
@@ -1,12 +1,12 @@
/**
* callphpfunction.cpp
* @author Jasper van Eck<jasper.vaneck@copernica.com>
* callphpfunction.cpp
* @author Jasper van Eck<jasper.vaneck@copernica.com>
*
* An example file to show the working of a php function call in C++.
* An example file to show the working of a php function call in C++.
*/

/**
* Libraries used.
* Libraries used.
*/
#include <iostream>
#include <phpcpp.h>
Expand All @@ -17,37 +17,37 @@
using namespace std;

/**
* call_php_function()
* Calls a function in PHP space.
* @param &params
* @return Php::Value
* call_php_function()
* Calls a function in PHP space.
* @param &params
* @return Php::Value
*/
Php::Value call_php_function(Php::Parameters &params)
{
// check whether the parameter is callable
if (!params[0].isCallable()) throw Php::Exception("Not a callable type.");
// perform the callback
return params[0](1,2,3);
// check whether the parameter is callable
if (!params[0].isCallable()) throw Php::Exception("Not a callable type.");
// perform the callback
return params[0](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
// export the "get_module" function that will be called by the Zend engine
PHPCPP_EXPORT void *get_module()
{
// create extension
static Php::Extension extension("call_php_function","1.0");

// add function to extension
extension.add("call_php_function", call_php_function, {
Php::ByVal("addFunc", Php::callableType),
Php::ByVal("x", Php::numericType)
});
// return the extension module
return extension.module();
}
Php::ByVal("addFunc", Php::callableType),
Php::ByVal("x", Php::numericType)
});
// return the extension module
return extension.module();
}
}
30 changes: 15 additions & 15 deletions Examples/CallPhpFunctions/callphpfunction.php
@@ -1,33 +1,33 @@
<?php
/**
* callphpfunction.php
* @author Jasper van Eck<jasper.vaneck@copernica.com>
* callphpfunction.php
* @author Jasper van Eck<jasper.vaneck@copernica.com>
*
* An example file to show the working of a php function call in C++.
* An example file to show the working of a php function call in C++.
*/

class MyClass
{
function method($a,$b,$c)
{
return $a+$b+$c;
}
function method($a,$b,$c)
{
return $a+$b+$c;
}
}

function myFunction($a,$b,$c)
{
return $a+$b+$c;
return $a+$b+$c;
}

/**
* Call a C++ function with a callable PHP function as its parameter.
* The PHP function is then executed from the C++ code.
* The PHP function is this case, adds three numbers.
* Call a C++ function with a callable PHP function as its parameter.
* The PHP function is then executed from the C++ code.
* The PHP function is this case, adds three numbers.
*/
echo(call_php_function(function($a, $b, $c){
return $a + $b + $c;
})."\n");
return $a + $b + $c;
})."\n");
echo(call_php_function("myFunction")."\n");
echo(call_php_function(array(new MyClass(), 'method'))."\n");
44 changes: 22 additions & 22 deletions Examples/CppClassesInPhp/cppclassinphp.cpp
@@ -1,8 +1,8 @@
/**
* cppclassinphp.cpp
* @author Jasper van Eck<jasper.vaneck@copernica.com>
* cppclassinphp.cpp
* @author Jasper van Eck<jasper.vaneck@copernica.com>
*
* An example file to show the working of using a C++ class in PHP.
* An example file to show the working of using a C++ class in PHP.
*/

#include "includeMyCustomClass.h"
Expand Down Expand Up @@ -40,32 +40,32 @@ class MyCustomClass : public Php::Base
}

void myMethod(Php::Parameters &params)
{
std::cout << "myMethod is called." << std::endl;
std::cout << "_x: " << _x << std::endl;
_x = params[0];
std::cout << "New _x" << _x << std::endl;
}
{
std::cout << "myMethod is called." << std::endl;
std::cout << "_x: " << _x << std::endl;
_x = params[0];
std::cout << "New _x" << _x << std::endl;
}
};


// 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
// export the "get_module" function that will be called by the Zend engine
PHPCPP_EXPORT void *get_module()
{
// create extension
static Php::Extension extension("my_function_with_parameters","1.0");

// add the custom class ot the extension
extension.add("MyClass", Php::Class<MyCustomClass>({
Php::Public("myMethod", Php::Method<MyCustomClass>(&MyCustomClass::myMethod),{
Php::ByVal("newX", Php::numericType)
})
}));
// return the extension module
return extension.module();
}
extension.add("MyClass", Php::Class<MyCustomClass>({
Php::Public("myMethod", Php::Method<MyCustomClass>(&MyCustomClass::myMethod),{
Php::ByVal("newX", Php::numericType)
})
}));
// return the extension module
return extension.module();
}
}
6 changes: 3 additions & 3 deletions Examples/CppClassesInPhp/cppclassinphp.php
@@ -1,9 +1,9 @@
<?php
/**
* cppclassinphp.php
* @author Jasper van Eck<jasper.vaneck@copernica.com>
* cppclassinphp.php
* @author Jasper van Eck<jasper.vaneck@copernica.com>
*
* An example file to show the working of using a C++ class in PHP.
* An example file to show the working of using a C++ class in PHP.
*/

//create a MyCustomClass object, which is an object of a C++ class
Expand Down
10 changes: 5 additions & 5 deletions Examples/Exceptions/ExceptionCatch/exception.php
@@ -1,12 +1,12 @@
<?php
/**
* exception.cpp
* exception.cpp
*
* @author Jasper van Eck <jasper.vaneck@copernica.com>
* @author Jasper van Eck <jasper.vaneck@copernica.com>
*
* This example shows the working of a C++ function that throws an
* exception, and that exception is then caught by PHP code.
*
* This example shows the working of a C++ function that throws an
* exception, and that exception is then caught by PHP code.
*
*/

// call the second C++ function that accepts a callback
Expand Down
38 changes: 19 additions & 19 deletions Examples/Exceptions/ExceptionCatch/exceptionCatch.cpp
@@ -1,14 +1,14 @@
/**
* exception.cpp
* @author Jasper van Eck<jasper.vaneck@copernica.com>
* exception.cpp
* @author Jasper van Eck<jasper.vaneck@copernica.com>
*
* An example file to show the working of a C++ function that
* takes a callback function as parameter, and handles the
* exception thrown by the callback function.
* An example file to show the working of a C++ function that
* takes a callback function as parameter, and handles the
* exception thrown by the callback function.
*/

/**
* Libraries used.
* Libraries used.
*/
#include <phpcpp.h>

Expand All @@ -18,9 +18,9 @@
using namespace std;

/**
* my_catch_exception_function()
* Catches the exception thrown by the PHP callback function.
* @param Php::Parameters
* my_catch_exception_function()
* Catches the exception thrown by the PHP callback function.
* @param Php::Parameters
*/
void my_catch_exception_function(Php::Parameters &params)
{
Expand All @@ -47,18 +47,18 @@ void my_catch_exception_function(Php::Parameters &params)
// 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
// export the "get_module" function that will be called by the Zend engine
PHPCPP_EXPORT void *get_module()
{
// create extension
static Php::Extension extension("my_exception_catch","1.0");

// add function to extension
extension.add("my_catch_exception_function", my_catch_exception_function, {
Php::ByVal("callback", Php::callableType);
});
// return the extension module
return extension.module();
}
Php::ByVal("callback", Php::callableType);
});
// return the extension module
return extension.module();
}
}
24 changes: 12 additions & 12 deletions Examples/Exceptions/ExceptionThrow/exception.php
@@ -1,25 +1,25 @@
<?php
/**
* exception.cpp
* exception.cpp
*
* @author Jasper van Eck <jasper.vaneck@copernica.com>
* @author Jasper van Eck <jasper.vaneck@copernica.com>
*
* This example shows the working of a C++ function that throws an
* exception, and that exception is then caught by PHP code.
*
* This example shows the working of a C++ function that throws an
* exception, and that exception is then caught by PHP code.
*
*/

// try-catch block to be able to handle the exception
try
{
// the my_throw_exception function is implemented in C++. and
// it is going to throw an exception
my_throw_exception_function();
// the my_throw_exception function is implemented in C++. and
// it is going to throw an exception
my_throw_exception_function();
}
catch (Exception $exception)
{
// the exception object is thrown by C++ code, and caught by PHP
// code
echo $exception->getMessage(). "\n";
// the exception object is thrown by C++ code, and caught by PHP
// code
echo $exception->getMessage(). "\n";
}
34 changes: 17 additions & 17 deletions Examples/Exceptions/ExceptionThrow/exceptionThrow.cpp
@@ -1,14 +1,14 @@
/**
* exception.cpp
* @author Jasper van Eck<jasper.vaneck@copernica.com>
* exception.cpp
* @author Jasper van Eck<jasper.vaneck@copernica.com>
*
* An example file to show the working of a C++ function that
* throws an exception, which can be caught by PHP.
*
* An example file to show the working of a C++ function that
* throws an exception, which can be caught by PHP.
*
*/

/**
* Libraries used.
* Libraries used.
*/
#include <phpcpp.h>

Expand All @@ -18,28 +18,28 @@
using namespace std;

/**
* my_throw_exception_function()
* Throws an exception which should be caught by PHP.
* my_throw_exception_function()
* Throws an exception which should be caught by PHP.
*/
void my_throw_exception_function()
{
throw Php::Exception("I threw an exception in my_throw_exception_function()");
throw Php::Exception("I threw an exception in my_throw_exception_function()");
}


// 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
// export the "get_module" function that will be called by the Zend engine
PHPCPP_EXPORT void *get_module()
{
// create extension
static Php::Extension extension("my_exception_throw","1.0");

// add function to extension
extension.add("my_throw_exception_function", my_throw_exception_function);
// return the extension module
return extension.module();
}
// return the extension module
return extension.module();
}
}

0 comments on commit 80c8a16

Please sign in to comment.