diff --git a/docs/UNSUPPORTED_SYNTAX.md b/docs/UNSUPPORTED_SYNTAX.md index ca2bd370..d4366e54 100644 --- a/docs/UNSUPPORTED_SYNTAX.md +++ b/docs/UNSUPPORTED_SYNTAX.md @@ -270,6 +270,400 @@ $c = $a + $b; // 需要类型转换,可能有性能损失 --- +## 🔒 类型系统对比 + +### 动态类型系统(ZVAL - 默认) + +**特点**: 变量可以在运行时自由改变类型 + +**示例**: +```php +id = $id; + $this->price = $price; + $this->name = $name; + } + + // ✅ 类型安全的 getter/setter + public function getPrice(): std::float { + return $this->price; + } + + // ❌ 错误:类型不匹配 + public function setPrice(std::int $price) { + $this->price = $price; // 编译错误 + } +} +``` + +#### 场景三:循环和计数器 + +```php + std::int(PHP_INT_MAX - 1000)) { + // 转为 ZVAL 处理大数 + return (int)$a / (int)$b; + } + + return std::float($a) / std::float($b); +} +``` + +#### 3. 渐进式迁移 + +```php +hasVar($var)) { $this->addLocalVar($var, $type); - } else { - if ($this->getVarType($var) !== $type) { - $this->fatalError($left, "Cannot re-assign {$var} to {$type}"); - } } $expr = $this->parseExpr($right->args[0]->value); return $var . ' = ' . $this->convertExprFromType($type, $expr); diff --git a/tests/aot/native-type.phpt b/tests/aot/native-type.phpt index 467a361e..79dd7297 100644 --- a/tests/aot/native-type.phpt +++ b/tests/aot/native-type.phpt @@ -12,9 +12,27 @@ function main() $c = std::bool(true); var_dump($c); + + $a = 99; + $d = std::int($a); + var_dump($d); + + $e = '2026_'; + $f = std::float($e); + var_dump($f); + + $h = 10; + var_dump($h / 4); + + $i = std::int(10); + var_dump($i / 4); } ?> --EXPECT-- int(100) float(100) bool(true) +int(99) +float(2026) +float(2.5) +int(2)