RealyUniqueName / haxe

Haxe - The Cross-Platform Toolkit

Home Page:http://haxe.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Rename methods with the same name as their class, keywords.

benmerckx opened this issue · comments

commented

Methods which have the same name as the class they're defined in should be renamed to prevent php from seeing them as the constructor.

I just compiled something like this:

class Main {
    public static function main()
        trace('hello world');
}

Which results in:

PHP Fatal error:  Constructor Main::main() cannot be static in .../Main.php on line 18

This is only relevant for non-namespaced classes in the top package:

As of PHP 5.3.3, methods with the same name as the last element of a namespaced class name will no longer be treated as constructor. This change doesn't affect non-namespaced classes.

I wonder if it's ok to generate __call() and __callStatic() for cases like this.

class Main {
    public function __hx__main () {        
    }

    public function __call($method, $args) {
        if ($method == 'main')
            return call_user_func_array([$this, '__hx__main'], $args);
        } else {
            //trigger error: Unknown method
        }
    }
}

Without such magic user will get runtime error if he attempts to invoke renamed method on a dynamic var:

var q : Dynamic = new Main();
q.main() //runtime error, because compiler does not know real type of `q` 
         //and we cannot replace `.main()" with `.__hx__main()` in compiletime

__call() solves such issues. But could it potentially introduce some other issues?

On the other hand dynamic access requires additonal runtime manipulations anyway. We can implement renamed fields resolution as a part of that manipulations.

commented

The first solution would allow the method name to be called from non-haxe code as well. But in the interest of keeping the compiled code clean, I'd go for your second suggestion.

The third solution could be generating all code in some default namespace (which can be changed with --php-prefix)

commented

That would take care of this problem. But you'll need either of the previous solutions anyway in cases where you must rename a method (for example if someone uses a php reserved keyword as method name).

Absorbed by #47