How to add in php declare(strict_types)?

Hello,

is there way how to generate to each php files generated by haxe
declare(strict_types=1);
on top of file afte <?php , please?

Or in general, is there way how to edit files after its compilations?

Thank you

Hello, you can do this by running the haxe code after compilation.

# build.hxml
--dce full

--main Main
--class-path src/

--php bin/php

--cmd haxe --main PostBuild --class-path src/ --interp
// src/PostBuild.hx
using StringTools;

import haxe.io.Path;
import sys.io.File;
import sys.FileSystem;



class PostBuild {
	static function main() {
		final buildPath = "./bin/php";
		
		final queue:Array<String> = [buildPath];

		while (queue.length != 0) {
			final currentPath = queue.shift();

			if (FileSystem.isDirectory(currentPath)) {
				for (file in FileSystem.readDirectory(currentPath)) {
					queue.push(Path.join([currentPath, file]));
				}
			} else if (currentPath.endsWith(".php")) {
				var content = File.getContent(currentPath);

				content = content.replace(
					"<?php",
					"<?php\ndeclare(strict_types=1);"
				);

				File.saveContent(currentPath, content);
			}
		}
	}
}
1 Like

Interesting, thank you :slight_smile:

1 Like