swig and php

View: New views
7 Messages — Rating Filter:   Alert me  

swig and php

by Paul Lowman :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi all

I have just joined the list as I am in the process of writing a machine
control application with a c++ frontend and a browser based GUI using
AJAX techniques. This requires me to interface c++ to php and swig
appears to be the correct approach (thus avoiding writing custom php
extensions ..).

My problem at the moment is that I am trying to access a pre-existing
structure in the c++ process by using a simple factory method that
returns a pointer to the structure. I see in the php4 examples that are
in the swig source tree an example called shadow. This appears to do
what I want. Unfortunately when I try to compile the code I get a bunch
of errors with example_wrap.cxx - a shortened list of which follows -

make[1]: Entering directory `/usr/src/swig-1.3.35/Examples/php4/shadow'
.../../../preinst-swig -php4 -cppext cxx -c++  example.i
g++ -c -fpic   example.cxx example_wrap.cxx  -I/usr/include/php
-I/usr/include/php/main -I/usr/include/php/TSRM -I/usr/include/php/Zend
-I/usr/include/php/ext -I/usr/include/php/ext/date/lib
example_wrap.cxx:981: error: 'zend_property_reference' was not declared
in this scope
example_wrap.cxx:981: error: 'property_reference' was not declared in
this scope
example_wrap.cxx:981: error: expected primary-expression before '*' token
example_wrap.cxx:981: error: 'value' was not declared in this scope
example_wrap.cxx:981: error: initializer expression list treated as
compound expression
example_wrap.cxx:982: error: 'zend_property_reference' was not declared
in this scope
example_wrap.cxx:982: error: 'property_reference' was not declared in
this scope
example_wrap.cxx:982: error: expected primary-expression before '*' token
example_wrap.cxx:982: error: 'value' was not declared in this scope

..
..
..
..

example_wrap.cxx:1705: error: 'value' was not declared in this scope
example_wrap.cxx: In function 'int zm_startup_example(int, int)':
example_wrap.cxx:2067: error: cannot convert 'pval' to '_zend_function*'
in assignment
example_wrap.cxx:2067: error: invalid conversion from 'int' to
'_zend_function*'
example_wrap.cxx:2071: error: cannot convert 'pval' to '_zend_function*'
in assignment
example_wrap.cxx:2071: error: invalid conversion from 'int' to
'_zend_function*'
example_wrap.cxx:2075: error: cannot convert 'pval' to '_zend_function*'
in assignment
example_wrap.cxx:2075: error: invalid conversion from 'int' to
'_zend_function*'
make[1]: *** [php4_cpp] Error 1
make[1]: Leaving directory `/usr/src/swig-1.3.35/Examples/php4/shadow'
make: *** [all] Error 2

All the other examples I have tried to compile work fine.

Unfortunately I am not gcc expert and I cannot track down the problem.
It seems that as the wrapper code is produced by swig then the problem
must lie there somewhere.

All this being said what I really need to do is access a c++ class /
structure via a pointer. I can see that php can get a resource in this
way but I could not see how to get php to use that resource to access
the fields and methods of the class/structure. It appears from the
shadow example that this can be achieved using the %newobject keyword.
Is this correct?

An further problem that I have noted is missing closing brackets in a
proxy class in php_example.php. Again it appears to be an issue with the
automatically generated code. It occurs in the proxy class for a
structure defined in the c++ header.

Any help or advice on these issues will be most appreciated.

Cheers

Paul Lowman

-------------------------------------------------------------------------
This SF.net email is sponsored by the 2008 JavaOne(SM) Conference
Don't miss this year's exciting event. There's still time to save $100.
Use priority code J8TL2D2.
http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone
_______________________________________________
Swig-user mailing list
Swig-user@...
https://lists.sourceforge.net/lists/listinfo/swig-user

Re: swig and php

by Olly Betts :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On 2008-04-30, Paul Lowman <paul_lowman@...> wrote:
> g++ -c -fpic   example.cxx example_wrap.cxx  -I/usr/include/php
> -I/usr/include/php/main -I/usr/include/php/TSRM -I/usr/include/php/Zend
> -I/usr/include/php/ext -I/usr/include/php/ext/date/lib
> example_wrap.cxx:981: error: 'zend_property_reference' was not declared
> in this scope

SWIG is generating code for PHP4 (since you specified "-php4" in the
command line), but this error suggests you are using PHP5.  If you want
code which works with PHP5, use "-php5" instead!

Cheers,
    Olly


-------------------------------------------------------------------------
This SF.net email is sponsored by the 2008 JavaOne(SM) Conference
Don't miss this year's exciting event. There's still time to save $100.
Use priority code J8TL2D2.
http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone
_______________________________________________
Swig-user mailing list
Swig-user@...
https://lists.sourceforge.net/lists/listinfo/swig-user

Parent Message unknown Re: swig and php

by Paul Lowman :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Olly Betts wrote:
 > On 2008-04-30, Paul Lowman <paul_lowman@...> wrote:
 >> g++ -c -fpic   example.cxx example_wrap.cxx  -I/usr/include/php
-I/usr/include/php/main -I/usr/include/php/TSRM -I/usr/include/php/Zend
-I/usr/include/php/ext -I/usr/include/php/ext/date/lib
 >> example_wrap.cxx:981: error: 'zend_property_reference' was not
declared in this scope
 >
 > SWIG is generating code for PHP4 (since you specified "-php4" in the
 > command line), but this error suggests you are using PHP5.  If you want
 > code which works with PHP5, use "-php5" instead!
 >
 > Cheers,
 >     Olly
 >

That helped - it now compiles but unless I remove the %newobject line in
example.i no code appears in example.php for the factory method. Even
then - either way - I get -

Fatal error: Call to a member function area() on a non-object in
/usr/src/swig-1.3.35/Examples/php4/shadow/runme.php4 on line 13

This is the problem I cannot resolve - how do I get php to see the
returned resource as an instance of a class?

This apparently worked OK in php4 so what are the relevent changes?

TIA

Paul Lowman

-------------------------------------------------------------------------
This SF.net email is sponsored by the 2008 JavaOne(SM) Conference
Don't miss this year's exciting event. There's still time to save $100.
Use priority code J8TL2D2.
http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone
_______________________________________________
Swig-user mailing list
Swig-user@...
https://lists.sourceforge.net/lists/listinfo/swig-user

Re: swig and php

by Olly Betts :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On 2008-05-05, Paul Lowman <paul_lowman@...> wrote:
> That helped - it now compiles but unless I remove the %newobject line in
> example.i no code appears in example.php for the factory method. Even
> then - either way - I get -
>
> Fatal error: Call to a member function area() on a non-object in
> /usr/src/swig-1.3.35/Examples/php4/shadow/runme.php4 on line 13
>
> This is the problem I cannot resolve - how do I get php to see the
> returned resource as an instance of a class?

It needs to be wrapped in the appropriate PHP class (using
"new MyPHPClass($handle)" IIRC), which should happen automatically
in the PHP wrapper for CircleFactory().

> This apparently worked OK in php4 so what are the relevent changes?

I guess you mean changes between PHP4 and PHP5 support?

The two work in completely different ways.  PHP4 constructs PHP objects
using the C API.  The functions used aren't present in PHP5 (hence the
zend_property_reference() errors you saw), and the PHP APIs aren't
well documented so we couldn't see how to fix this.  So instead we
took the approach used in SWIG/Python and wrap the C++ class as a
set of flat functions which take a resource as the first argument, then
create a PHP class wrapper which has the resource as a member variable
and methods which call the flat functions.

If you're able to see what needs fixing (in Source/Modules/php4.cxx
I imagine) a patch would be great.  Otherwise, file a bug and I'll take
a look when I get a chance.

Cheers,
    Olly


-------------------------------------------------------------------------
This SF.net email is sponsored by the 2008 JavaOne(SM) Conference
Don't miss this year's exciting event. There's still time to save $100.
Use priority code J8TL2D2.
http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone
_______________________________________________
Swig-user mailing list
Swig-user@...
https://lists.sourceforge.net/lists/listinfo/swig-user

Re: swig and php

by Paul Lowman :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Olly Betts wrote:

> On 2008-05-05, Paul Lowman <paul_lowman@...> wrote:
>> That helped - it now compiles but unless I remove the %newobject line in
>> example.i no code appears in example.php for the factory method. Even
>> then - either way - I get -
>>
>> Fatal error: Call to a member function area() on a non-object in
>> /usr/src/swig-1.3.35/Examples/php4/shadow/runme.php4 on line 13
>>
>> This is the problem I cannot resolve - how do I get php to see the
>> returned resource as an instance of a class?
>
> It needs to be wrapped in the appropriate PHP class (using
> "new MyPHPClass($handle)" IIRC), which should happen automatically
> in the PHP wrapper for CircleFactory().
>
>> This apparently worked OK in php4 so what are the relevent changes?
>
> I guess you mean changes between PHP4 and PHP5 support?
>
> The two work in completely different ways.  PHP4 constructs PHP objects
> using the C API.  The functions used aren't present in PHP5 (hence the
> zend_property_reference() errors you saw), and the PHP APIs aren't
> well documented so we couldn't see how to fix this.  So instead we
> took the approach used in SWIG/Python and wrap the C++ class as a
> set of flat functions which take a resource as the first argument, then
> create a PHP class wrapper which has the resource as a member variable
> and methods which call the flat functions.
>
> If you're able to see what needs fixing (in Source/Modules/php4.cxx
> I imagine) a patch would be great.  Otherwise, file a bug and I'll take
> a look when I get a chance.
>
> Cheers,
>     Olly
>
>
> -------------------------------------------------------------------------
> This SF.net email is sponsored by the 2008 JavaOne(SM) Conference
> Don't miss this year's exciting event. There's still time to save $100.
> Use priority code J8TL2D2.
> http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone
> _______________________________________________
> Swig-user mailing list
> Swig-user@...
> https://lists.sourceforge.net/lists/listinfo/swig-user
>

I don't see that code as a wrapper. This is what I see in example.php

abstract class example {
        static function CircleFactory($r_) {
                $r=CircleFactory($r_);
                return is_resource($r) ? new Circle($r) : $r;
        }
}

the .i files is -

/* File : example.i */
%module example

%{
#include "example.h"
%}

/* Let's just grab the original header file here */

/*%newobject CircleFactory;*/
%include "example.h"

If I uncomment the %newobject line then I get no reference to the circle
factory at all.

I assume I am not putting something in the .i file that needs to be there.

Cheers

Paul Lowman

-------------------------------------------------------------------------
This SF.net email is sponsored by the 2008 JavaOne(SM) Conference
Don't miss this year's exciting event. There's still time to save $100.
Use priority code J8TL2D2.
http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone
_______________________________________________
Swig-user mailing list
Swig-user@...
https://lists.sourceforge.net/lists/listinfo/swig-user

Re: swig and php

by Olly Betts :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On 2008-05-06, Paul Lowman <paul_lowman@...> wrote:

> Olly Betts wrote:
>> On 2008-05-05, Paul Lowman <paul_lowman@...> wrote:
> I don't see that code as a wrapper. This is what I see in example.php
>
> abstract class example {
> static function CircleFactory($r_) {
> $r=CircleFactory($r_);
> return is_resource($r) ? new Circle($r) : $r;
> }
> }

Well, it is wrapping the resource with the appropriate PHP class wrapper
(Circle in this case).  I'm not sure that the is_resource() check is
required in this case - I don't exactly remember why that's there.

It does look like it might just call itself though.  Does this code
work?

> If I uncomment the %newobject line then I get no reference to the circle
> factory at all.

I guess that's the nub of the issue then.  I don't know quite how
newobject works internally but it certainly shouldn't suppress the
wrapper method entirely!

> I assume I am not putting something in the .i file that needs to be there.

No, I think it's probably a code generation bug in the PHP backend.
Probably just an incorrect assumption on my part.

I'm planning to switch the test suite over to testing PHP5 rather than
PHP4 in the nearish future, and then I'll work through to fix any
failing cases such as this one.  Patches are certainly welcome if
you can figure out where the problem is (almost certainly somewhere
in the (increasingly misnamed) Source/Modules/php4.cxx).

Cheers,
    Olly


-------------------------------------------------------------------------
This SF.net email is sponsored by the 2008 JavaOne(SM) Conference
Don't miss this year's exciting event. There's still time to save $100.
Use priority code J8TL2D2.
http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone
_______________________________________________
Swig-user mailing list
Swig-user@...
https://lists.sourceforge.net/lists/listinfo/swig-user

Re: swig and php

by Paul Lowman :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Olly Betts wrote:

> On 2008-05-06, Paul Lowman <paul_lowman@...> wrote:
>> Olly Betts wrote:
>>> On 2008-05-05, Paul Lowman <paul_lowman@...> wrote:
>> I don't see that code as a wrapper. This is what I see in example.php
>>
>> abstract class example {
>> static function CircleFactory($r_) {
>> $r=CircleFactory($r_);
>> return is_resource($r) ? new Circle($r) : $r;
>> }
>> }
>
> Well, it is wrapping the resource with the appropriate PHP class wrapper
> (Circle in this case).  I'm not sure that the is_resource() check is
> required in this case - I don't exactly remember why that's there.
>
> It does look like it might just call itself though.  Does this code
> work?
>
>> If I uncomment the %newobject line then I get no reference to the circle
>> factory at all.
>
> I guess that's the nub of the issue then.  I don't know quite how
> newobject works internally but it certainly shouldn't suppress the
> wrapper method entirely!
>
>> I assume I am not putting something in the .i file that needs to be there.
>
> No, I think it's probably a code generation bug in the PHP backend.
> Probably just an incorrect assumption on my part.
>
> I'm planning to switch the test suite over to testing PHP5 rather than
> PHP4 in the nearish future, and then I'll work through to fix any
> failing cases such as this one.  Patches are certainly welcome if
> you can figure out where the problem is (almost certainly somewhere
> in the (increasingly misnamed) Source/Modules/php4.cxx).
>
> Cheers,
>     Olly
>
>
> -------------------------------------------------------------------------
> This SF.net email is sponsored by the 2008 JavaOne(SM) Conference
> Don't miss this year's exciting event. There's still time to save $100.
> Use priority code J8TL2D2.
> http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone
> _______________________________________________
> Swig-user mailing list
> Swig-user@...
> https://lists.sourceforge.net/lists/listinfo/swig-user
>
Olly

The code complains when it is run that a non object is being accessed -
so it would seem that php5 does not know about the class returned by the
factory method. Here is a snippet of code being used -

<?php

# This file illustrates the low-level C++ interface
# created by SWIG.  In this case, all of our C++ classes
# get converted into function calls.

include("example.php");

# ----- Object creation -----

print "Creating some objects:\n";
$c = CircleFactory(10);
print "    Created circle $c with area ". $c->area() ."\n";
..
..

It complains about $c->area ...

Cheers

Paul Lowman

-------------------------------------------------------------------------
This SF.net email is sponsored by the 2008 JavaOne(SM) Conference
Don't miss this year's exciting event. There's still time to save $100.
Use priority code J8TL2D2.
http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone
_______________________________________________
Swig-user mailing list
Swig-user@...
https://lists.sourceforge.net/lists/listinfo/swig-user