支持静态类属性读写语法

pull/1/head
韩天峰 8 months ago
parent 5824e859e6
commit 73f84217db
  1. 14
      src/Php/Translator.php
  2. 11
      tests/aot/assign-compare.phpt
  3. 61
      tests/aot/static_class_property_read_write.phpt
  4. 47
      tests/aot/static_property_test.inc

@ -463,6 +463,7 @@ class Translator extends \PhpAot\Core\Translator
case self::EXPR_VARIABLE:
return $this->escapeVarName($expr->name);
case 'Name':
case 'VarLikeIdentifier':
case 'Identifier':
return $expr->name;
case 'Scalar_Int':
@ -667,7 +668,6 @@ class Translator extends \PhpAot\Core\Translator
return $this->parseBinaryOpLogicalOr($expr);
case 'Expr_BinaryOp_LogicalXor':
return $this->parseBinaryOpLogicalXor($expr);
// 减法
case 'Expr_BinaryOp_Minus':
return $this->parseBinaryOpMinus($expr);
case 'Expr_Array':
@ -700,6 +700,8 @@ class Translator extends \PhpAot\Core\Translator
return $this->parseMethodCall($expr);
case 'Expr_StaticCall':
return $this->parseStaticCall($expr);
case 'Expr_StaticPropertyFetch':
return $this->parseStaticPropertyFetch($expr);
case 'Expr_Include':
return $this->parseInclude($expr);
case 'Expr_Eval':
@ -780,6 +782,11 @@ class Translator extends \PhpAot\Core\Translator
$array = $this->parseIdentifier($left->var);
$propName = $this->identifierToStr($left->name);
return "$array.setProperty($propName, " . $this->trimBrackets($this->parseExpr($right)) . ")";
} elseif ($left->getType() === 'Expr_StaticPropertyFetch') {
$class = $this->identifierToStr($left->class);
$propName = $this->identifierToStr($left->name);
$value = $this->trimBrackets($this->parseExpr($right));
return "php::setStaticProperty($class, $propName, $value)";
} elseif ($right->getType() === 'Expr_Assign') {
$chain[] = $left;
while ($right->getType() === 'Expr_Assign') {
@ -2434,6 +2441,11 @@ class Translator extends \PhpAot\Core\Translator
}
}
private function parseStaticPropertyFetch(Node $expr): string
{
return 'php::getStaticProperty(' . $this->identifierToStr($expr->class) . ', ' . $this->identifierToStr($expr->name) . ')';
}
private function parseMagicConstLine(Node $expr): int
{
return $expr->getStartLine();

@ -0,0 +1,11 @@
--TEST--
assign compare
--FILE--
<?php
$t1 = 1;
$t2 = 4;
$t3 = 0;
var_dump($t2 < ($t3 = $t1 * 5));
?>
--EXPECT--
bool(true)

@ -0,0 +1,61 @@
--TEST--
Static Class Property Read/Write Test
--FILE--
<?php
include __DIR__ . '/static_property_test.inc';
// Test reading static properties
echo "Initial public property: " . TestClass::$public_static_property . "\n";
echo "Initial protected property: " . TestClass::getProtectedProperty() . "\n";
echo "Initial private property: " . TestClass::getPrivateProperty() . "\n";
echo "Initial default property: " . TestClass::$default_static_property . "\n";
// Test writing static properties
TestClass::$public_static_property = 'modified_public';
TestClass::setProtectedProperty('modified_protected');
TestClass::setPrivateProperty('modified_private');
TestClass::$default_static_property = 'modified_default';
// Test reading static properties after modification
echo "Modified public property: " . TestClass::$public_static_property . "\n";
echo "Modified protected property: " . TestClass::getProtectedProperty() . "\n";
echo "Modified private property: " . TestClass::getPrivateProperty() . "\n";
echo "Modified default property: " . TestClass::$default_static_property . "\n";
echo "Initial counter: " . AnotherClass::$counter . "\n";
echo "After increment 1: " . AnotherClass::increment() . "\n";
echo "After increment 2: " . AnotherClass::increment() . "\n";
echo "Final counter: " . AnotherClass::$counter . "\n";
echo "Int property: " . DataTypeTest::$int_prop . "\n";
echo "Float property: " . DataTypeTest::$float_prop . "\n";
echo "Bool property: " . (DataTypeTest::$bool_prop ? 'true' : 'false') . "\n";
DataTypeTest::$int_prop = 100;
DataTypeTest::$float_prop = 2.71;
DataTypeTest::$bool_prop = false;
echo "Modified int property: " . DataTypeTest::$int_prop . "\n";
echo "Modified float property: " . DataTypeTest::$float_prop . "\n";
echo "Modified bool property: " . (DataTypeTest::$bool_prop ? 'true' : 'false') . "\n";
?>
--EXPECTF--
Initial public property: initial_value
Initial protected property: protected_value
Initial private property: private_value
Initial default property: default_value
Modified public property: modified_public
Modified protected property: modified_protected
Modified private property: modified_private
Modified default property: modified_default
Initial counter: 0
After increment 1: 1
After increment 2: 2
Final counter: 2
Int property: 42
Float property: %s
Bool property: true
Modified int property: 100
Modified float property: 2.71
Modified bool property: false

@ -0,0 +1,47 @@
<?php
// Test static property with different data types (if supported)
class DataTypeTest
{
static int $int_prop = 42;
static float $float_prop = 3.14;
static bool $bool_prop = true;
}
// Test static properties in different context
class AnotherClass
{
static string $counter = '0';
public static function increment() {
self::$counter = strval(intval(self::$counter) + 1);
return self::$counter;
}
}
class TestClass
{
public static string $public_static_property = 'initial_value';
protected static string $protected_static_property = 'protected_value';
private static string $private_static_property = 'private_value';
static string $default_static_property = 'default_value';
public static function getProtectedProperty()
{
return self::$protected_static_property;
}
public static function getPrivateProperty()
{
return self::$private_static_property;
}
public static function setProtectedProperty(string $value) {
self::$protected_static_property = $value;
}
public static function setPrivateProperty(string $value) {
self::$private_static_property = $value;
}
}
Loading…
Cancel
Save