Hello Haxers!
I started using Haxe for a legacy PHP project, as a “more sane” alternative for new features,
and at the moment im (re) writing old features to Haxe to get better type safety, and
access to the awesome language features.
The generated PHP code is used from PHP, meaning i dont have a Main
function, but
rather build libraries that is then used from the PHP side. I also use externs for things i dont want to rebuild in Haxe.
Im having some trouble with the generated PHP files, basically i need to have access
to PHP’s use
keyword. We are targeting PHP8, with the use of PHP attributes.
An example Haxe file:
package controller;
@:native('Foo\\Bar\\Baz\\Component')
extern class Component {}
@:keep
class Foo {
@:php.attribute("Component('/foo', name: 'foo_bar')")
function foo() {
return "FOO";
}
}
And the generated PHP file:
<?php
/**
* Generated by Haxe 4.3.6
*/
namespace Hx\controller;
use \Hx\php\Boot;
class Foo {
/**
* @return string
*/
#[Component('/foo', name: 'foo_bar')]
public function foo () {
#hx/controller/Foo.hx:10: characters 3-15
return "FOO";
}
}
Boot::registerClass(Foo::class, 'controller.Foo');
The Component
attribute is namespaced, and is actually living under Foo\Bar\Baz\Component
, so i need to be able to add a use to the PHP class from Haxe. But the @:php.attribute
only accepts a string constant. I cant push our users to write the entire namespace here as a string, as we have a huge nested codebase, and use namespaces heavily. Instead i want to alias the namespace like you normally do in PHP.
TLDR:
How can i add a PHP use
from the Haxe side and generate the following code in the PHP file top.
use \Foo\Bar\Baz\Component;
and/or
use \Foo\Bar\Baz\Component as MyaliasedComponent;