This guide will help you get started with the PHP AOT compiler within 5 minutes.
- Operating System: Linux (Ubuntu 20.04+ recommended)
- PHP Version: PHP 8.0+
- Compiler: GCC 9.0+ or Clang 10.0+
- Memory: at least 2GB RAM
- Disk Space: at least 500MB
# Ubuntu/Debian
sudo apt-get update
sudo apt-get install -y build-essential php-cli php-dev clang-format
# CentOS/RHEL
sudo yum install -y gcc gcc-c++ php php-devel clang-tools-extragit clone https://github.com/your-org/php-aot-compiler.git
cd php-aot-compilercomposer installphp bin/tpc.php --helpIf you see the help information, the installation was successful!
Suppose we have a simple PHP project:
my-project/
βββ src/
β βββ Calculator.php
β βββ main.php
Calculator.php:
<?php
class Calculator {
public function add($a, $b) {
return $a + $b;
}
public function multiply($a, $b) {
return $a * $b;
}
}main.php:
<?php
require_once 'Calculator.php';
function main() {
$calc = new Calculator();
echo "5 + 3 = " . $calc->add(5, 3) . "\n";
echo "4 * 7 = " . $calc->multiply(4, 7) . "\n";
}php bin/tpc.php my-project/src/ -o my-app./my-app5 + 3 = 8
4 * 7 = 28
β Advantages:
- Runs standalone without a PHP environment
- Simple deployment
- Better performance
main() function is required
php bin/tpc.php my-project/src/ --mode=ext -o calculator# Copy the .so file to the PHP extension directory
sudo cp calculator.so $(php-config --extension-dir)/
# Add to php.ini
echo "extension=calculator" | sudo tee /etc/php/8.1/cli/conf.d/30-calculator.iniphp -m | grep calculator # verify the extension is loadedβ Advantages:
- Integrates with existing PHP projects
- Can be used in php-fpm
- Suitable for web applications
main() function is needed
<?php
// Class and function definitions (allowed at the global scope)
class MyClass {
public function doSomething() {
return "Something";
}
}
function helperFunction() {
return "Helper";
}
const MY_CONSTANT = 'value';
// Executable code must be inside the main() function
function main() {
$obj = new MyClass();
echo $obj->doSomething();
echo helperFunction();
echo MY_CONSTANT;
}<?php
// β Free-floating executable code (not allowed)
echo "Hello World"; // error!
some_function_call(); // error!
for ($i = 0; $i < 10; $i++) { // error!
echo $i;
}PHPT=1 php run-tests.php tests/compiler/arrow_fn/001.phptPHPT=1 php run-tests.php tests/compiler/PASS Arrow Functions - PHP 8.1+ short closure syntax
FAIL Some test
=====================================================================
Number of tests : 100 100
Tests passed : 95 ( 95.0%)
Tests failed : 5 ( 5.0%)
project/
βββ src/ # source code
β βββ Classes/ # class files
β βββ Functions/ # function library
β βββ main.php # entry file
βββ tests/ # test files
βββ build/ # compilation output
- File names use lowercase, with words separated by underscores:
my_class.php - Class names use PascalCase:
MyClass - Function names use camelCase:
myFunction
- Avoid unnecessary object creation
- Use scalar type declarations
- Reduce the use of global variables
- Prefer arrays over object collections
A: First check the Compatibility Checklist to determine whether it is a TypePHP design rule, partially supported, or not yet implemented.
A: Use --dry to generate only the intermediate code, and specify a directory with --build-dir.
php bin/tpc.php src/ --dry --build-dir /tmp/typephp-buildA: Use the parallel compilation option -j:
php bin/tpc.php src/ -o app -j4 # use 4 processesAfter completing the quick start, the following are recommended reads:
- Compatibility Checklist - understand the current limitations
- Compilation Modes Explained - learn the two compilation modes in depth
- Build Speed Research - optimize the compilation flow
- π View the full documentation: docs/
- π Report issues: [GitHub Issues]
- π¬ Community discussion: [forum/chat room link]
Enjoy! π