#986 Cannot access struct object within struct

closed-fixed
Olly Betts
php (58)
5
2009-08-17
2009-01-30
Barry Cohen
No

Using SWIG 1.3.37, it doesn't seem to be possible to get access to a non-primitive object declared within a struct/class. Consider the following:

struct bar
{
int get4()
{
return 4;
}
};

struct foo
{
bar a;
};

From PHP, I would expect to be able to do:

$myfoo = new foo();
printf($myfoo->{"a"}->get4() . "\n");

but the error is:

PHP Fatal error: Call to a member function get4() on a non-object

$myfoo->{"a"} returns a resource instead of an object.

Perhaps this is a problem with my PHP syntax, but this process works as expected in Python and PHP. Is it possible to access the get4() function through foo?

A complete example is attached.

Thanks

Barry

Discussion

  • Barry Cohen
    Barry Cohen
    2009-01-30

    example.zip

     
    Attachments
  • Olly Betts
    Olly Betts
    2009-06-30

    Kevin's no longer active in SWIG so reassigning to myself.

    I don't know about $myfoo->{"a"}, but we document this should be usable as $myfoo->a and I bet that doesn't currently work as we don't check for a resource and wrap it in the __get() method we generate.gets wrapped.

    What's missing is the check for a resource and returning it wrapped in a PHP5 object, as happens for a method returning a class.

     
  • Olly Betts
    Olly Betts
    2009-06-30

    • assigned_to: kruland --> olly
     
  • Olly Betts
    Olly Betts
    2009-08-17

    • status: open --> closed-fixed
     
  • Olly Betts
    Olly Betts
    2009-08-17

    Fixed in trunk r11619.

    I need to remove the attempts to print objects from your example (that won't work without defining a __toString() method), but then it passes with this change.