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 6939027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:33:59+00:00 2026-05-27T12:33:59+00:00

I am working on wrapping a C++ API in PHP using SWIG. I am

  • 0

I am working on wrapping a C++ API in PHP using SWIG. I am most of the way there but I am having problems with a function that returns a vector. The header looks something like this:

#include <vector>

namespace STF
{
class MyClass
{
    public:
        const std::vector<MyOtherClass> &getList();
};
}

The interface file looks like this:

%include <std_vector.i>
%import "STF_MyOtherClass.i"

%{
    #include "STF_MyOtherClass.h"
    #include "STF_MyClass.h"
%}

%include "STF_MyClass.h"

I seem to be able to call the function fine but it is returning a PHP resource instead of an object. Specifically it is a resource of type: “_p_std__vectorT_STF__MyClass_t”.

How can I either get this to return an object that I can iterate through (preferably with a foreach loop) or how can I iterate through this resource?

Update:

I have been working on a solution based off of what I read here: http://permalink.gmane.org/gmane.comp.programming.swig/16817

Basically I am trying to convert the vector into a python array:

%typemap(out) std::vector<STF::MyOtherClass>
{
    array_init( return_value );

    std::vector<STF::MyOtherClass>::iterator itr;

    itr = $1.begin();

    for( itr; itr != $1.end(); itr++ )
    {
        zval* tmp;

        MAKE_STD_ZVAL( tmp );
        SWIG_SetPointerZval( tmp, &*itr, $descriptor(STF::MyOtherClass*), 2 );

        add_next_index_zval( return_value, tmp );
    }
}

This is very close to working. I put a breakpoint inside the wrapper code within SWIG_ZTS_SetPointerZval. When it goes to initialize the object it does a zend_lookup_class for “stf__myotherclass” which fails (it doesn’t find a clasS). I am not sure why it can’t find the class.

  • 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-05-27T12:34:00+00:00Added an answer on May 27, 2026 at 12:34 pm

    In the end this is what I did to convert the vector into a PHP array (put this in the interface file for MyOtherClass):

    %typemap(out) const std::vector<STF::MyOtherClass>&
    {
        array_init( return_value );
    
        std::vector<STF::MyOtherClass>::const_iterator itr;
    
        itr = $1->begin();
    
        for( itr; itr != $1->end(); itr++ )
        {
            zval* tmp;
    
            STF::MyOtherClass * res = new STF::MyOtherClass( *itr );
    
            MAKE_STD_ZVAL( tmp );
    
            swig_type_info type = *$descriptor(STF::MyOtherClass*);
            type.name = (char*)"_p_CustomNamespace\\MyOtherClass";
    
            SWIG_SetPointerZval( tmp, res, &type, 2 );
    
            add_next_index_zval( return_value, tmp );
        }
    }
    

    The %template that awoodland did not work for me. I think it is probably because I did not put the PHP class in a different custom namespace. Instead, I did this manually and passed in the exact php class I wanted it to use.

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

Sidebar

Related Questions

I am working on a site that is using the unfortunate practice of wrapping
Working with dates in ruby and rails on windows, I'm having problems with pre-epoch
I'm working with colorizing some output using readline in Ruby, but I am not
I am having trouble wrapping my mind around primarily RequireJS. I see that it
I've been working up my answer to Is there a standard Scala function for
I'm working on wrapping a C DLL library to Java using JNA. The library
I'm working on a wrapping library for a robot controller, that mostly relies on
I'm working on wrapping up the ugly innards of the FindFirstFile / FindNextFile loop
Greetings! I'm working on wrapping my head around LINQ. If I had some XML
Working with a SqlCommand in C# I've created a query that contains a IN

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.