最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Is there a way SWIG C++ to PHP could force 'static' in front of a function? - Stack Overflow

programmeradmin5浏览0评论

Please note that I am not a Python/PHP expert but I need to solve C++ to PHP case on overloaded constructors.

I have C++ code like:

enum Enum_Foo {
    Foo_A.
    Foo_B,
    Foo_C
};

enum Enum_Bar {
    Bar_A.
    Bar_B,
    Bar_C
};

class Creator
{
public:
    Creator();
    Creator(Enum_Foo);
    Creator(Enum_Bar);
};

SWIG complains on overloaded methods and it is ok. Recomendation is to use %rename for the second method (constructor) and I do that as:

Python.i:

%rename (CreatorFromBar) Creator::Creator(Enum_Bar);

The Python result is a function (reworked a bit from original):

def CreatorFromBar(in_enum_bar):
    val = _SomeNamespacePython.new_CreatorFromBar(in_enum_bar)
    return val

which could be used in final Python code as:

    bar = Enum_Bar.Bar_A
    creator = CreatorFromBar(bar)

But PHP is a different case:

PHP.i:

%rename (CreateFromBar) Creator::Creator(Enum_Bar);

The PHP result is a class method (reworked a bit from original):

class Creator {
    ...
    function CreateFromBar($in_enum_bar_type) {
        ...
        return new Creator(new_CreateFromBar($in_enum_bar_type));
    }
    ...
}

which could be used in final PHP code only as:

    $enum_bar = Enum_Bar::Bar_A;
    $dummy = new Creator();
    $correct = $dummy->CreatorFromBar($enum_bar);

If the SWIG result was with static qualifier - it would be called as:

    $enum_bar = Enum_Bar::Bar_A;
    $correct = Creator::CreatorFromBar($enum_bar);

Questions:

  • Is there any way to force static in front of PHP function SWIG result?
  • Is there any other way to avoid dummy instance?

Please note that I am not a Python/PHP expert but I need to solve C++ to PHP case on overloaded constructors.

I have C++ code like:

enum Enum_Foo {
    Foo_A.
    Foo_B,
    Foo_C
};

enum Enum_Bar {
    Bar_A.
    Bar_B,
    Bar_C
};

class Creator
{
public:
    Creator();
    Creator(Enum_Foo);
    Creator(Enum_Bar);
};

SWIG complains on overloaded methods and it is ok. Recomendation is to use %rename for the second method (constructor) and I do that as:

Python.i:

%rename (CreatorFromBar) Creator::Creator(Enum_Bar);

The Python result is a function (reworked a bit from original):

def CreatorFromBar(in_enum_bar):
    val = _SomeNamespacePython.new_CreatorFromBar(in_enum_bar)
    return val

which could be used in final Python code as:

    bar = Enum_Bar.Bar_A
    creator = CreatorFromBar(bar)

But PHP is a different case:

PHP.i:

%rename (CreateFromBar) Creator::Creator(Enum_Bar);

The PHP result is a class method (reworked a bit from original):

class Creator {
    ...
    function CreateFromBar($in_enum_bar_type) {
        ...
        return new Creator(new_CreateFromBar($in_enum_bar_type));
    }
    ...
}

which could be used in final PHP code only as:

    $enum_bar = Enum_Bar::Bar_A;
    $dummy = new Creator();
    $correct = $dummy->CreatorFromBar($enum_bar);

If the SWIG result was with static qualifier - it would be called as:

    $enum_bar = Enum_Bar::Bar_A;
    $correct = Creator::CreatorFromBar($enum_bar);

Questions:

  • Is there any way to force static in front of PHP function SWIG result?
  • Is there any other way to avoid dummy instance?
Share Improve this question edited Mar 10 at 15:59 IMSoP 98.3k18 gold badges132 silver badges180 bronze badges asked Mar 10 at 15:28 NaumNaum 1251 silver badge7 bronze badges 1
  • FWIW, you can skip the variable $dummy by just writing it inline: $correct = (new Creator())->CreatorFromBar($enum_bar); But I think the "how can I make it static?" part, to avoid the extra constructor call completely, is more important, so won't leave this as an answer. – IMSoP Commented Mar 10 at 16:57
Add a comment  | 

1 Answer 1

Reset to default 0

I found a way by creating a global function at the end of PHP.i:

First set overloaded constructor to be ignored (not necessary but prevents warning):

%ignore SomeNamespace::Creator(Enum_Bar);

and then define a global function:

%inline %{
namespace SomeNamespace {
    Creator* CreatorFromBar(Enum_Bar in_enum_bar) {
        return new Creator(in_enum_bar);
    }
}
%}

And the call is as:

$enum_bar = Enum_Bar::Bar_A; // May come via argument of a function
$correct = CreatorFromBar($enum_bar);

PS: It isn't answer to the question but solves my problem. It works for PHP and Ruby. I will apply for Python too - in order to have consistent code.

发布评论

评论列表(0)

  1. 暂无评论