Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
benmerckx opened this issue Aug 14, 2016 · 6 comments
Closed

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

benmerckx opened this issue Aug 14, 2016 · 6 comments
Labels

Comments

@benmerckx
Copy link

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.

@RealyUniqueName
Copy link
Owner

RealyUniqueName commented Aug 14, 2016

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?

@RealyUniqueName
Copy link
Owner

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

@benmerckx
Copy link
Author

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.

@RealyUniqueName
Copy link
Owner

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

@benmerckx
Copy link
Author

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).

@RealyUniqueName RealyUniqueName changed the title Rename methods with the same name as their class Rename methods with the same name as their class, keywords. Aug 20, 2016
@RealyUniqueName
Copy link
Owner

Absorbed by #47

RealyUniqueName pushed a commit that referenced this issue Jun 19, 2019
* Fix for --times on OSX & linux under reporting time.

* Fix for linux.

* Fix for OSX without support for clock_gettime
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants