4

If I use SWIG to wrap this C++ function:

boost::shared_ptr<Client> Client::create() {
    return boost::shared_ptr<Client>(new Client());
}

And then call it in PHP:

$client = Client::create();
echo gettype($client);

The type of $client is resource, not object, and so I cannot call Client methods.

What are my options for wrapping this function? I'm creating a PHP wrapper for someone else's C++ library, so reworking the code to not use boost::shared_ptr isn't really an option.

This is the only solution I've come up with so far:

MyClient Client::createObject() {
    return *Client::create();
}

And calling it in PHP:

$client = Client::createObject();
echo gettype($client);

Now the type of $client is object and I can call Client methods on it. Is this a reasonable workaround? If not, what should I be doing?

Ed Mazur
  • 3,042
  • 3
  • 21
  • 21

1 Answers1

0

You should take a look to http://www.swig.org/Doc2.0/SWIGPlus.html#SWIGPlus_smart_pointers ;)

Pik'
  • 6,819
  • 1
  • 28
  • 24