Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7932307
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T20:58:38+00:00 2026-06-03T20:58:38+00:00

I am writing a PHP wrapper for a C++ library using SWIG, but I

  • 0

I am writing a PHP wrapper for a C++ library using SWIG, but I am having trouble using a structure with an instance of a template type as a data member.

Suppose I have the following header file:

template <typename>
struct myvector
{
};

struct S
{
    myvector<int> v;
};

myvector<int> foo();
S bar();

and interface file:

%module test
%{
#include "test.hpp"
%}

%include "test.hpp"
%template(IntVec) myvector<int>;

When I try to use a function that directly returns an myvector, it works fine:

$v1 = test::foo();

However, when I try to use a function that returns an S object, and try to access its data member which is of type myvector:

$s = test::bar();
$v2 = $s->v;

I get the following error at runtime:

PHP Fatal error:  Class 'myvectorT_int_t' not found in test.php on line 145

I am probably missing something from my interface file, but I don’t know what. Can anyone help?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T20:58:40+00:00Added an answer on June 3, 2026 at 8:58 pm

    As far as I can make out this is a SWIG bug. Someone else has already reported it in fact. Fortunately there’s a simple, reliable workaround via PHP’s 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>;
    

    The pragma here inserts the code to setup the alias at the start of the generated PHP file.

    (There’s another possible work around too – rather than using public member variables access via getter/setter functions works as expected)

    The bug report also mentions another possible workaround although I’m not keen on that since it requires using a rather ugly name for the template type.


    Justification for bug assumption

    The code for __get 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 is set to myvectorT_int_t which would be correct except for the %template directive.

    When we add a 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 the generic code that would be correct without the %template and as special check to see if it’s actually an IntVec.

    There’s also a comment in the Source/Modules/php.cxx:

    // FIXME: Currently we always use call_user_func for __get, so we can
    // check and wrap the result.  This is needless if all the properties
    // are primitive types.  Also this doesn't handle all the cases which
    // a method returning an object does.
    

    Finally the code generated by the same interface file for Java is correct.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a PHP script that downloads a series of generated files (using wget
What is faster: writing PHP code using functions or writing it as pure script?
I am having an problem writing unit tests for an simple session wrapper. The
I am writing an PHP application and using the gettext module to translate to
I'm about to get my hands dirty writing an FTP wrapper for PHP, I
I've been writing PHP applications using PHP for a while in WAMP. Now I'm
I'm writing php web-app using unholy alliance of php+oracle+iis :) every time script being
When writing PHP code for any given project, do you find you can write
Often when writing PHP I'll have it output some HTML like this - echo
Im writing a php script to compare the similarity of 2 strings. This works

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.