From a4bc4fcfc02fc7717d3a18a5ce42bdced83ae339 Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Wed, 18 Mar 2026 18:25:23 +0800 Subject: [PATCH] =?UTF-8?q?test(aot):=20=E6=B7=BB=E5=8A=A0=E6=99=9A?= =?UTF-8?q?=E6=9C=9F=E9=9D=99=E6=80=81=E7=BB=91=E5=AE=9A=E5=8A=9F=E8=83=BD?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 测试 static:: 和 self:: 在继承中的行为差异 - 验证静态属性在不同子类中的独立性 - 检查静态方法重写和构造函数中的调用 - 覆盖 late static binding 的各种使用场景 --- tests/aot/late-static-binding.phpt | 131 +++++++++++++++++++++++++++++ 1 file changed, 131 insertions(+) create mode 100644 tests/aot/late-static-binding.phpt diff --git a/tests/aot/late-static-binding.phpt b/tests/aot/late-static-binding.phpt new file mode 100644 index 00000000..63f194a0 --- /dev/null +++ b/tests/aot/late-static-binding.phpt @@ -0,0 +1,131 @@ +--TEST-- +Early Static Binding - late static binding with static:: and self:: +--FILE-- + true, 'debug' => true]; + var_dump(DevConfig::get('dev')); + var_dump(DevConfig::get('debug')); + + // ProdConfig 使用自己的 settings + \ProdConfig::$settings = ['prod' => true, 'debug' => false]; + var_dump(ProdConfig::get('prod')); + var_dump(ProdConfig::get('debug')); +} + +class DevConfig extends Config { + // 子类使用自己的属性 +} + +class ProdConfig extends Config { + // 子类使用自己的属性 +} + +// Test with static method override +class Factory { + protected static string $className = 'BaseClass'; + + public static function create(): object { + return new static::$className(); + } + + public static function getClassName(): string { + return static::$className; + } +} + +class UserFactory extends Factory { + protected static string $className = 'User'; +} + +class ProductFactory extends Factory { + protected static string $className = 'Product'; +} + +class User {} +class Product {} + +// Test in constructor +class Base { + public function __construct() { + echo "Base constructor\n"; + static::init(); + } + + public static function init(): void { + echo "Base init\n"; + } +} + +class Derived extends Base { + public static function init(): void { + echo "Derived init\n"; + } +} + +function main() { + // Test basic late static binding + var_dump(ParentClass::test_self()); // Should print "ParentClass" + var_dump(ParentClass::test_static()); // Should print "ParentClass" + var_dump(Child::test_self()); // Should print "ParentClass" (self refers to Parent) + var_dump(Child::test_static()); // Should print "Child" (static refers to called class) + + // Test with static properties + test_static_properties(); + + // Test with static method override + var_dump(UserFactory::getClassName()); + var_dump(ProductFactory::getClassName()); + + // Test in constructor + new Derived(); +} +?> +--EXPECT-- +string(11) "ParentClass" +string(11) "ParentClass" +string(11) "ParentClass" +string(5) "Child" +bool(true) +bool(true) +bool(true) +bool(false) +string(4) "User" +string(7) "Product" +Base constructor +Derived init