PHP use keyword

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;

1 Like

The @:php.attribute meta requires a fully qualified name (like the @:native meta).

package controller;

@:keep
class Foo {
	@:php.attribute("\\Foo\\Bar\\Baz\\Component('/foo', name: 'foo_bar')")
	function foo() {
		return "FOO";
	}
}

As far as I know, you can’t create a use statement directly with Haxe.

@filt3rek could probably give a better answer, as he has a lot of experience with the PHP compilation target.

Hej !

I’m sorry, idk if I’ll be helpful I never dealed with something like that.
But you should be able to simulate a use statement doing something like that :

class Main{
	static function main(){
		var _ = Component;
	}
}

@:extern
@:native( "Foo.Bar.Baz.Component" )
class Component{}

Then you’ll get

<?php
/**
 * Generated by Haxe 5.0.0-alpha.1+c0d033d25
 */

use \php\Boot;
use \Foo\Bar\Baz\Component;

class Main {
	/**
	 * @return void
	 */
	public static function main () {
		#Main.hx:3: characters 3-21
		$_ = Boot::getClass(Component::class);
	}

	/**
	 * @return void
	 */
	public function __construct () {
	}
}

Boot::registerClass(Main::class, 'Main');

Thanks for the reply! This is actually a nice solution, and is simple enough. I might try to build a macro to autogenerate these “fake” functions to get the desired use statements!

Thanks again!