HaxeFoundation / haxe

Haxe - The Cross-Platform Toolkit
https://haxe.org
6.19k stars 656 forks source link

[PHP7] root package modules conflict with include_path #8144

Closed AlexHaxe closed 5 years ago

AlexHaxe commented 5 years ago

Recently I wrote a Wordpress plugin in Haxe targeting PHP7. It worked fine on my local system, but not on target system. I could get it to work by switching include paths (Haxe's lib first, then system includes). Today I learned that ordering breaks media uploads, so I had to limit my plugin to only invoke when run through index.php.

I have investigated further (I don't have direct access to target's log files) and I found a way to reproduce locally:

Affects both Haxe 3 and 4, PHP5 works fine due to different loading mechanism.

I guess there is potential for more such conflicts, at least for modules in root package - not only std, but also with modules from libs or your own source code.

AlexHaxe commented 5 years ago
<?php

set_include_path(get_include_path().PATH_SEPARATOR.__DIR__.'/lib');
spl_autoload_register(
        function($class){
                $file = stream_resolve_include_path(str_replace('\\', '/', $class) .'.php');
                if ($file) {
                        include_once $file;
                }
        }
);
\php\Boot::__hx__init();
#(unknown)
\Main::main();
RealyUniqueName commented 5 years ago

Actually, I don't think there is anything to do on the Haxe side here. Except maybe better document such situation to increase awareness. Did you try -D php-prefix=my.pack? It will generate all the code into \\my\\pack namespace.

AlexHaxe commented 5 years ago

-D php-prefix=some.package seems to solve the issue, it seems like I was unaware of that option.

I tried running with -D php-prefix=haxe, which results in a "Class 'haxe\haxe\Std' not found" error, but other namespace names work.

So I would say that fixes my issue, and I agree that maybe some documentation warning of such a naming conflict would help. The PHP section in Haxe manual is rather short and I definitely looked at it before opening my issue.

AlexHaxe commented 5 years ago

ok, now that I've tested with an actual project, instead of sample code, I found two issues when using -D php-prefix=my.pack -php out:

  1. res folder is generated in out/res, but generated code searches for ressources in out/lib/my/res
  2. in Wordpress I need to register some handlers/callbacks for which I call Type.getClassName (MyType), but that call returns classname and package without prefixes. I guess I can call Compiler.getDefine("php-prefix") to adjust any classname I pass on to Wordpress. Not sure if that would pose a problem somewhere else.
RealyUniqueName commented 5 years ago

With the latest dev Haxe there should be no issues with -D php-prefix=haxe

RealyUniqueName commented 5 years ago

in Wordpress I need to register some handlers/callbacks for which I call Type.getClassName (MyType), but that call returns classname and package without prefixes. I guess I can call Compiler.getDefine("php-prefix") to adjust any classname I pass on to Wordpress. Not sure if that would pose a problem somewhere else.

Try php.Syntax.nativeClassName(MyType).

AlexHaxe commented 5 years ago

Thanks, that works for Haxe 4. For Haxe 3 I still have to use Compiler.getDefine.

Haxe 3 also suffers from res folder location issue, when using --php-prefix=my.pack

AlexHaxe commented 5 years ago

latest Resource.hx change fixes res location problem

RealyUniqueName commented 5 years ago

Given that it's unlikely there will be next patch release of 3.4 and the issue is fixed in 4.0, I'm closing it.