As far as I can understand this, this is a SWIG error. Someone else has already reported this . Fortunately, there is a simple and reliable workaround through the PHP class_alias
:
%module test %{ #include "test.h" %} %pragma(php) code=" # This code is inserted as a workaround for template bugs with SWIG class_alias('IntVec', 'myvectorT_int_t'); " %include "test.h" %template(IntVec) myvector<int>;
Here, the pragma inserts the code for setting the alias at the beginning of the generated PHP file.
(There's also another possible job - instead of using access to public member variables via getter / setter functions, it works as expected)
The error report also mentions another possible workaround, although I am not interested in this because it requires the use of a rather ugly name for the template type.
Justification for the mistake
The __get
code includes:
$c=substr(get_resource_type($r), (strpos(get_resource_type($r), '__') ? strpos(get_resource_type($r), '__') + 2 : 3)); return new $c($r);
When you get here $c
set to myvectorT_int_t
, which will be correct, with the exception of the %template
directive.
When we add the myvector<int> get()
function to S
, the generated code results in:
$c=substr(get_resource_type($r), (strpos(get_resource_type($r), '__') ? strpos(get_resource_type($r), '__') + 2 : 3)); if (!class_exists($c)) { return new IntVec($r); } return new $c($r);
which crucially includes generic code that would be correct without %template
and as a special check to see if this is IntVec
.
There is also a comment in Source/Modules/php.cxx
:
Finally, the code generated by the same interface file for Java is correct.