summaryrefslogtreecommitdiff
path: root/trunk/Examples/php/callback
diff options
context:
space:
mode:
authorWilliam S Fulton <wsf@fultondesigns.co.uk>2010-06-02 20:53:17 +0000
committerWilliam S Fulton <wsf@fultondesigns.co.uk>2010-06-02 20:53:17 +0000
commit2824b0cbb66e715490e1ef13250bd675d87b32d9 (patch)
treec3bc8d54c6d73f2b7ce08cac34172dbc9f5e5b95 /trunk/Examples/php/callback
parent289cfef4b4766ff266f3b1bdda8ca3a952e5a047 (diff)
downloadswig-2.0.0.tar.gz
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/tags/rel-2.0.0@12089 626c5289-ae23-0410-ae9c-e8d60b6d4f22
Diffstat (limited to 'trunk/Examples/php/callback')
-rw-r--r--trunk/Examples/php/callback/Makefile22
-rw-r--r--trunk/Examples/php/callback/example.cxx4
-rw-r--r--trunk/Examples/php/callback/example.h22
-rw-r--r--trunk/Examples/php/callback/example.i13
-rw-r--r--trunk/Examples/php/callback/index.html19
-rw-r--r--trunk/Examples/php/callback/runme.php47
6 files changed, 127 insertions, 0 deletions
diff --git a/trunk/Examples/php/callback/Makefile b/trunk/Examples/php/callback/Makefile
new file mode 100644
index 000000000..42597202b
--- /dev/null
+++ b/trunk/Examples/php/callback/Makefile
@@ -0,0 +1,22 @@
+TOP = ../..
+SWIG = $(TOP)/../preinst-swig
+CXXSRCS = example.cxx
+TARGET = example
+INTERFACE = example.i
+LIBS = -lm
+SWIGOPT =
+
+all::
+ $(MAKE) -f $(TOP)/Makefile $(SWIGLIB) CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \
+ SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' php_cpp
+
+static::
+ $(MAKE) -f $(TOP)/Makefile $(SWIGLIB) CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \
+ SWIGOPT='$(SWIGOPT)' TARGET='myphp' INTERFACE='$(INTERFACE)' php_cpp_static
+
+clean::
+ $(MAKE) -f $(TOP)/Makefile php_clean
+ rm -f $(TARGET).php
+
+check: all
+ $(MAKE) -f $(TOP)/Makefile php_run
diff --git a/trunk/Examples/php/callback/example.cxx b/trunk/Examples/php/callback/example.cxx
new file mode 100644
index 000000000..450d75608
--- /dev/null
+++ b/trunk/Examples/php/callback/example.cxx
@@ -0,0 +1,4 @@
+/* File : example.cxx */
+
+#include "example.h"
+
diff --git a/trunk/Examples/php/callback/example.h b/trunk/Examples/php/callback/example.h
new file mode 100644
index 000000000..2a0194999
--- /dev/null
+++ b/trunk/Examples/php/callback/example.h
@@ -0,0 +1,22 @@
+/* File : example.h */
+
+#include <iostream>
+
+class Callback {
+public:
+ virtual ~Callback() { std::cout << "Callback::~Callback()" << std:: endl; }
+ virtual void run() { std::cout << "Callback::run()" << std::endl; }
+};
+
+
+class Caller {
+private:
+ Callback *_callback;
+public:
+ Caller(): _callback(0) {}
+ ~Caller() { delCallback(); }
+ void delCallback() { delete _callback; _callback = 0; }
+ void setCallback(Callback *cb) { delCallback(); _callback = cb; }
+ void call() { if (_callback) _callback->run(); }
+};
+
diff --git a/trunk/Examples/php/callback/example.i b/trunk/Examples/php/callback/example.i
new file mode 100644
index 000000000..90beda01a
--- /dev/null
+++ b/trunk/Examples/php/callback/example.i
@@ -0,0 +1,13 @@
+/* File : example.i */
+%module(directors="1") example
+%{
+#include "example.h"
+%}
+
+%include "std_string.i"
+
+/* turn on director wrapping Callback */
+%feature("director") Callback;
+
+%include "example.h"
+
diff --git a/trunk/Examples/php/callback/index.html b/trunk/Examples/php/callback/index.html
new file mode 100644
index 000000000..2aa720e24
--- /dev/null
+++ b/trunk/Examples/php/callback/index.html
@@ -0,0 +1,19 @@
+<html>
+<head>
+<title>SWIG:Examples:php:callback</title>
+</head>
+
+<body bgcolor="#ffffff">
+
+
+<tt>SWIG/Examples/php/callback/</tt>
+<hr>
+
+<H2>Implementing C++ callbacks in PHP</H2>
+
+<p>
+This example illustrates how to use directors to implement C++ callbacks in PHP.
+
+<hr>
+</body>
+</html>
diff --git a/trunk/Examples/php/callback/runme.php b/trunk/Examples/php/callback/runme.php
new file mode 100644
index 000000000..2be71994f
--- /dev/null
+++ b/trunk/Examples/php/callback/runme.php
@@ -0,0 +1,47 @@
+<?php
+
+# This file illustrates the cross language polymorphism using directors.
+
+require("example.php");
+
+# Class, which overwrites Callback::run().
+
+class PhpCallback extends Callback {
+ function run() {
+ print "PhpCallback.run()\n";
+ }
+};
+
+# Create an Caller instance
+
+$caller = new Caller();
+
+# Add a simple C++ callback (caller owns the callback, so
+# we disown it first by clearing the .thisown flag).
+
+print "Adding and calling a normal C++ callback\n";
+print "----------------------------------------\n";
+
+$callback = new Callback();
+$callback->thisown = 0;
+$caller->setCallback($callback);
+$caller->call();
+$caller->delCallback();
+
+print "\n";
+print "Adding and calling a PHP callback\n";
+print "------------------------------------\n";
+
+# Add a PHP callback.
+
+$callback = new PhpCallback();
+$callback->thisown = 0;
+$caller->setCallback($callback);
+$caller->call();
+$caller->delCallback();
+
+# All done.
+
+print "php exit\n";
+
+?>