dan-da/py2php

super __init__ calls

Closed this issue · 4 comments

lines like this

super(Guillotine, self).__init__(width, height, rot, *args, **kwargs)

are converting to

py2php_kwargs_method_call('super(Guillotine, $this)', '__init__', $args, $kwargs);

which somehow is missing those first 3 params.

But also should rename the __INIT__ to construct like it already does in other places

should be more like...

parent::__construct( $width, $height, $rot, $args, $kwargs );

parent::__construct( $width, $height, $rot, $args, $kwargs );

This certainly looks nicer but it requires that the parent's constructor have specific logic to understand the kwargs. In python, kwargs are used to pass named parameters without special interpretation by called func. Eg, if we have a function:

def myfunc(a, b=None, c=None, d=None):
    pass

then we can call it like so

myfunc( 'hey', c = "yellow")

or we could do

varargs = {"a": 1, "d": "green"}
myfunc( **varargs )

There are also ordered *args. More detail.

To deal with this, I created an intermediary function that interprets the named parameters and then calls target func. Unfortunately this code path is not doing some of the class related translation logic.

When it is fixed, the translated code should look something like:

py2php_kwargs_method_call('parent', '__construct', $args, $kwargs);

Well, it's kind of an ugly hack, but I got this specific case working in 2facf6d and 88acd6e.

I made a test case for it in tests/namedparameters.py which passes.

Please verify when you have a chance.

aah okay... that seems pretty tough to map that logic to php :)

like i just mentioned in another ticket, I'd suggest getting these to convert the calls during the translation rather than calling the custom php method. Cuz these methods will almost all certainly be removed by hand for any project that isn't a one-off. And it's more understandable when looking at the results

Well it's a tradeoff.

Thus far, I've tried to make php code that actually runs rather than just a kinda/sorta translation. This is very useful for writing test cases in python and verifying that the output is exactly the same when executed as php.

It would be possible to add a mode/flag that optimizes for PHP readability with less chance it will actually execute correctly. This would require a separate set of test cases though, and I think more effort than it is worth right now. One thing I might implement if I find the time is to output the "native php way" as a comment, so that the person doing the porting can just uncomment and use it if desired.