diff --git a/src/gen_stub.php b/src/gen_stub.php index d6746cc7..06dffb0e 100755 --- a/src/gen_stub.php +++ b/src/gen_stub.php @@ -3257,7 +3257,8 @@ class PropertyInfo extends VariableLike $zvalName = "property_{$propertyName}_default_value"; if ($this->defaultValue === null && $this->type !== null) { - $code .= "\tzval $zvalName;\n\tZVAL_UNDEF(&$zvalName);\n"; + $code .= "\tzval $zvalName;\n"; + $code .= $this->getTypeDefaultValueCode($zvalName); } else { $code .= $defaultValue->initializeZval($zvalName, varName: $this->name->__toString()); } @@ -3344,6 +3345,32 @@ class PropertyInfo extends VariableLike $fieldsynopsisElement->appendChild($doc->createElement("modifier", "readonly")); } } + + private function getTypeDefaultValueCode(string $zvalName): string + { + if ($this->type->isNullable()) { + return "\tZVAL_NULL(&$zvalName);\n"; + } + $simpleType = $this->type->tryToSimpleType(); + if ($simpleType !== null) { + if ($simpleType->isInt()) { + return "\tZVAL_LONG(&$zvalName, 0);\n"; + } + if ($simpleType->isFloat()) { + return "\tZVAL_DOUBLE(&$zvalName, 0.0);\n"; + } + if ($simpleType->isBool()) { + return "\tZVAL_FALSE(&$zvalName);\n"; + } + if ($simpleType->isString()) { + return "\tZVAL_EMPTY_STRING(&$zvalName);\n"; + } + if ($simpleType->isArray()) { + return "\tZVAL_EMPTY_ARRAY(&$zvalName);\n"; + } + } + return "\tZVAL_NULL(&$zvalName);\n"; + } } class EnumCaseInfo { diff --git a/tests/aot/optimizations/object-int-prop-02.phpt b/tests/aot/optimizations/object-int-prop-02.phpt new file mode 100644 index 00000000..6634adc7 --- /dev/null +++ b/tests/aot/optimizations/object-int-prop-02.phpt @@ -0,0 +1,22 @@ +--TEST-- +SSA: int +--FILE-- +b += 2; + } + } +} +function main(): void { + $o = new Foo(); + $o->assign_add_prop(100000); + var_dump($o->b); +} +?> +--EXPECT-- +int(200000) \ No newline at end of file