Skip to content

Commit b378815

Browse files
committed
Added tests for static class feature.
Added compile-time check to enforce all methods must be declared static in a static class. Added compile-time mutual-exclusivity check for static classes marked with abstract or readonly modifiers. Added compile-time check to preclude static class inheriting from non-static class and vice-versa. Added compile-time check to preclude static class using a trait with non-static properties or methods. Fixed ZEND_ACC_IMPLICIT_ABSTRACT_CLASS collision with ZEND_ACC_STATIC.
1 parent 5274b65 commit b378815

21 files changed

+251
-5
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
--TEST--
2+
Tests that a non-static class cannot inherit from a static class
3+
--FILE--
4+
<?php
5+
6+
static class C {}
7+
8+
class C2 extends C {}
9+
?>
10+
--EXPECTF--
11+
Fatal error: Non-static class C2 cannot extend static class C in %s on line %d
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
--TEST--
2+
Tests that a static class cannot inherit from a non-static class
3+
--FILE--
4+
<?php
5+
6+
class C {}
7+
8+
static class C2 extends C {}
9+
?>
10+
--EXPECTF--
11+
Fatal error: Static class C2 cannot extend non-static class C in %s on line %d
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
--TEST--
2+
Tests that a static class can inherit from another static class
3+
--FILE--
4+
<?php
5+
6+
static class C {}
7+
8+
static class C2 extends C {}
9+
?>
10+
--EXPECT--
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
--TEST--
2+
Tests that a static class can inherit static members from a trait
3+
--FILE--
4+
<?php
5+
6+
trait T {
7+
static $P = 'OK';
8+
9+
static function F() {
10+
echo self::$P;
11+
}
12+
}
13+
14+
static class C {
15+
use T;
16+
}
17+
18+
C::F();
19+
?>
20+
--EXPECT--
21+
OK
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
--TEST--
2+
Tests that a static class cannot inherit an instance method from a trait
3+
--FILE--
4+
<?php
5+
6+
trait T {
7+
function F() {}
8+
}
9+
10+
static class C {
11+
use T;
12+
}
13+
?>
14+
--EXPECTF--
15+
Fatal error: Static class C cannot use trait with a non-static method T::F in %s on line %d
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
--TEST--
2+
Tests that a static class cannot inherit an instance property from a trait
3+
--FILE--
4+
<?php
5+
6+
trait T {
7+
var $V;
8+
}
9+
10+
static class C {
11+
use T;
12+
}
13+
?>
14+
--EXPECTF--
15+
Fatal error: Static class C cannot use trait with a non-static property T::$V in %s on line %d
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
--TEST--
2+
Tests that a static class cannot be instantiated
3+
--FILE--
4+
<?php
5+
6+
static class C {}
7+
8+
new C;
9+
?>
10+
--EXPECTF--
11+
Fatal error: Uncaught Error: Cannot instantiate static class C in %s:%d
12+
Stack trace:%a
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
--TEST--
2+
Tests that a static class cannot be instantiated via reflection
3+
--FILE--
4+
<?php
5+
6+
static class C {}
7+
8+
new ReflectionClass('C')->newInstance();
9+
?>
10+
--EXPECTF--
11+
Fatal error: Uncaught Error: Cannot instantiate static class C in %s:%d
12+
Stack trace:%a
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
--TEST--
2+
Tests that a static class cannot be marked abstract
3+
--FILE--
4+
<?php
5+
6+
abstract static class C {}
7+
?>
8+
--EXPECTF--
9+
Fatal error: Cannot use the static modifier on an abstract class in %s on line %d
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
--TEST--
2+
Tests that an anonymous class cannot be marked static
3+
--FILE--
4+
<?php
5+
6+
new static class C {}
7+
?>
8+
--EXPECTF--
9+
Fatal error: Cannot use the static modifier on an anonymous class in %s on line %d

0 commit comments

Comments
 (0)