Skip to content

Commit

Permalink
[PHP] Add support for specifying any PHP interfaces a wrapped class
Browse files Browse the repository at this point in the history
implements, e.g.: %typemap("phpinterfaces") MyIterator "Iterator";
  • Loading branch information
ojwb committed Sep 12, 2014
1 parent e12322d commit 1a99212
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGES.current
Expand Up @@ -5,6 +5,10 @@ See the RELEASENOTES file for a summary of changes in each release.
Version 3.0.3 (in progress)
===========================

2014-09-12: olly
[PHP] Add support for specifying any PHP interfaces a wrapped class
implements, e.g.: %typemap("phpinterfaces") MyIterator "Iterator";

2014-09-11: olly
[PHP] Fix throwing a PHP exception through C++ from a subclassed
director method - PHP NULL gets returned by the subclassed method
Expand Down
1 change: 1 addition & 0 deletions Doc/Manual/Contents.html
Expand Up @@ -1416,6 +1416,7 @@ <h3><a href="Php.html#Php">34 SWIG and PHP</a></h3>
<li><a href="Php.html#Php_nn2_6_2">Constructors and Destructors</a>
<li><a href="Php.html#Php_nn2_6_3">Static Member Variables</a>
<li><a href="Php.html#Php_nn2_6_4">Static Member Functions</a>
<li><a href="Php.html#Php_nn2_6_5">Specifying Implemented Interfaces</a>
</ul>
<li><a href="Php.html#Php_nn2_7">PHP Pragmas, Startup and Shutdown code</a>
</ul>
Expand Down
18 changes: 18 additions & 0 deletions Doc/Manual/Php.html
Expand Up @@ -29,6 +29,7 @@ <H1><a name="Php"></a>34 SWIG and PHP</H1>
<li><a href="#Php_nn2_6_2">Constructors and Destructors</a>
<li><a href="#Php_nn2_6_3">Static Member Variables</a>
<li><a href="#Php_nn2_6_4">Static Member Functions</a>
<li><a href="#Php_nn2_6_5">Specifying Implemented Interfaces</a>
</ul>
<li><a href="#Php_nn2_7">PHP Pragmas, Startup and Shutdown code</a>
</ul>
Expand Down Expand Up @@ -770,6 +771,23 @@ <H4><a name="Php_nn2_6_4"></a>34.2.6.4 Static Member Functions</H4>
</pre></div>


<H4><a name="Php_nn2_6_5"></a>34.2.6.5 Specifying Implemented Interfaces</H4>

<p>
PHP supports the concept of abstract interfaces which a class can implement.
Since SWIG 3.0.3, you can tell SWIG that a wrapped class (for example
<code>MyIterator</code>) implements the <code>Iterator</code> interface like
so:
</p>

<div class="code"><pre>
%typemap("phpinterfaces") MyIterator "Iterator";
</pre></div>

<p>
If there are multiple interfaces, just list them separated by commas.
</p>

<H3><a name="Php_nn2_7"></a>34.2.7 PHP Pragmas, Startup and Shutdown code</H3>


Expand Down
1 change: 1 addition & 0 deletions Examples/test-suite/php/Makefile.in
Expand Up @@ -10,6 +10,7 @@ top_srcdir = @top_srcdir@
top_builddir = @top_builddir@

CPP_TEST_CASES += \
php_iterator \
php_namewarn_rename \

include $(srcdir)/../common.mk
Expand Down
24 changes: 24 additions & 0 deletions Examples/test-suite/php/php_iterator_runme.php
@@ -0,0 +1,24 @@
<?php

require "tests.php";
require "php_iterator.php";

check::functions(array(myiterator_rewind,myiterator_key,myiterator_current,myiterator_next,myiterator_valid));
check::classes(array(MyIterator));
// No new global variables.
check::globals(array());

$s = '';
foreach (new MyIterator(1, 6) as $i) {
$s .= $i;
}
check::equal($s, '12345', 'Simple iteration failed');

$s = '';
foreach (new MyIterator(2, 5) as $k => $v) {
$s .= "($k=>$v)";
}
check::equal($s, '(0=>2)(1=>3)(2=>4)', 'Simple iteration failed');

check::done();
?>
20 changes: 20 additions & 0 deletions Examples/test-suite/php_iterator.i
@@ -0,0 +1,20 @@
/* php_iterator.i - PHP-specific testcase for wrapping to a PHP Iterator */
%module php_iterator

%typemap("phpinterfaces") MyIterator "Iterator";

%inline %{

class MyIterator {
int i, from, to;
public:
MyIterator(int from_, int to_)
: i(from_), from(from_), to(to_) { }
void rewind() { i = from; }
bool valid() const { return i != to; }
int key() const { return i - from; }
int current() const { return i; }
void next() { ++i; }
};

%}
11 changes: 11 additions & 0 deletions Source/Modules/php.cxx
Expand Up @@ -2045,6 +2045,17 @@ class PHP : public Language {
} else if (GetFlag(n, "feature:exceptionclass")) {
Append(s_phpclasses, "extends Exception ");
}
{
Node *node = NewHash();
Setattr(node, "type", Getattr(n, "name"));
Setfile(node, Getfile(n));
Setline(node, Getline(n));
String * interfaces = Swig_typemap_lookup("phpinterfaces", node, "", 0);
if (interfaces) {
Printf(s_phpclasses, "implements %s ", interfaces);
}
Delete(node);
}
Printf(s_phpclasses, "{\n\tpublic $%s=null;\n", SWIG_PTR);
if (!baseclass) {
// Only store this in the base class (NB !baseclass means we *are*
Expand Down

0 comments on commit 1a99212

Please sign in to comment.