diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index 6ab3bcc1..af399649 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -86,9 +86,13 @@ class CompilerBase extends \PhpAot\Core\Translator protected array $useFunctions = []; protected string $class = ''; /** - * @var array + * @var array */ protected array $classes = []; + /** + * @var array + */ + protected array $classesDefineInFile = []; protected FunctionDef $functionDef; protected ClassDef $classDef; protected array $globalVars = [ @@ -201,6 +205,7 @@ class CompilerBase extends \PhpAot\Core\Translator { $this->indentLevel = 0; $this->strictTypes = false; + $this->classesDefineInFile = []; } protected function resetNamespace(): void diff --git a/src/Php/Translator.php b/src/Php/Translator.php index 7308ea7d..726f50a0 100644 --- a/src/Php/Translator.php +++ b/src/Php/Translator.php @@ -145,6 +145,23 @@ class Translator extends Preprocessor abort($v); } } + + foreach ($this->classesDefineInFile as $classDef) { + $name = $classDef->getNamespacedName(); + + if ($classDef->extends) { + $parentName = 'zend_class_entry *parent_ce'; + $param = 'parent_ce'; + } else { + $parentName = ''; + $param = ''; + } + $cppCode .= 'zend_class_entry *' . self::PREFIX . 'register_class_' . $name . '(' . $parentName . ') {' . PHP_EOL; + $cppCode .= $this->getIndent() . 'return register_class_' . $name . '(' . $param . ');' . PHP_EOL; + $cppCode .= '}' . PHP_EOL . PHP_EOL; + } + $cppCode .= PHP_EOL; + // include + extern global vars + function impl return $this->genIncludeHeaderFiles() . $cppCode; } @@ -390,17 +407,16 @@ class Translator extends Preprocessor $this->stubFileIncluded = true; } - if (!isset($this->classes[$this->class])) { - $this->classDef = new ClassDef(); - $this->classDef->name = $this->class; - if ($class->extends) { - $this->classDef->extends = $this->parseIdentifier($class->extends); - } - $this->classDef->implements = $this->parseIdentifierList($class->implements); - $this->classDef->namespace = $this->namespace; - - $this->classes[$this->class] = $this->classDef; + $this->classDef = new ClassDef(); + $this->classDef->name = $this->class; + if ($class->extends) { + $this->classDef->extends = $this->parseIdentifier($class->extends); } + $this->classDef->implements = $this->parseIdentifierList($class->implements); + $this->classDef->namespace = $this->namespace; + + $this->classes[$this->class] = $this->classDef; + $this->classesDefineInFile[$this->class] = $this->classDef; $methodCodes = [];