[PHP] Wrap renamed constructor as static method

Previously it was wrapped as a non-static method, which results in
a diagnostic from PHP if called as a static method.
This commit is contained in:
Olly Betts 2019-02-09 11:48:33 +13:00
commit 123eabf200
2 changed files with 26 additions and 9 deletions

View file

@ -1096,11 +1096,11 @@ public:
if (constructor) {
class_has_ctor = true;
// Skip the Foo:: prefix.
char *ptr = strrchr(GetChar(Swig_methodclass(n), "sym:name"), ':');
char *ptr = strrchr(GetChar(current_class, "sym:name"), ':');
if (ptr) {
ptr++;
} else {
ptr = GetChar(Swig_methodclass(n), "sym:name");
ptr = GetChar(current_class, "sym:name");
}
if (strcmp(ptr, GetChar(n, "constructorHandler:sym:name")) == 0) {
methodname = "__construct";
@ -1509,7 +1509,7 @@ public:
Replaceall(value, "$", "\\$");
}
Printf(args, "$%s=%s", arg_names[i], value);
} else if (constructor && i >= 1 && i < min_num_of_arguments) {
} else if (constructor && strcmp(methodname, "__construct") == 0 && i >= 1 && i < min_num_of_arguments) {
// We need to be able to call __construct($resource).
Printf(args, "$%s=null", arg_names[i]);
} else {
@ -1667,20 +1667,33 @@ public:
}
if (constructor) {
const char * arg0;
// Discriminate between the PHP constructor and a C++ constructor
// renamed to become a factory function in PHP.
bool php_constructor = (strcmp(methodname, "__construct") == 0);
const char * arg0 = NULL;
if (max_num_of_arguments > 0) {
arg0 = Char(arg_names[0]);
} else {
} else if (php_constructor) {
// The PHP constructor needs to be able to wrap a resource, but a
// renamed constructor doesn't.
arg0 = "res";
Delete(args);
args = NewString("$res=null");
}
String *mangled_type = SwigType_manglestr(Getattr(n, "type"));
if (!php_constructor) {
// A renamed constructor should be a static method.
Append(acc, "static ");
}
Printf(output, "\t%sfunction %s(%s) {\n", acc, methodname, args);
Printf(output, "\t\tif (is_resource($%s) && get_resource_type($%s) === '%s') {\n", arg0, arg0, mangled_type);
Printf(output, "\t\t\t$this->%s=$%s;\n", SWIG_PTR, arg0);
Printf(output, "\t\t\treturn;\n");
Printf(output, "\t\t}\n");
if (php_constructor) {
// The PHP constructor needs to be able to wrap a resource, but a
// renamed constructor doesn't.
Printf(output, "\t\tif (is_resource($%s) && get_resource_type($%s) === '%s') {\n", arg0, arg0, mangled_type);
Printf(output, "\t\t\t$this->%s=$%s;\n", SWIG_PTR, arg0);
Printf(output, "\t\t\treturn;\n");
Printf(output, "\t\t}\n");
}
} else {
Printf(output, "\t%sfunction %s(%s) {\n", acc, methodname, args);
}