Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions phpunit/code/property_nonstatic_mismatch.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
class A
{
public int $x = 1;
}

class B extends A
{
public static int $x = 2;
}

function main() {}
14 changes: 14 additions & 0 deletions phpunit/code/property_static_match.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
class A
{
public static int $x = 1;
public int $y = 1;
}

class B extends A
{
public static int $x = 2;
public int $y = 2;
}

function main() {}
12 changes: 12 additions & 0 deletions phpunit/code/property_static_mismatch.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
class A
{
public static int $x = 1;
}

class B extends A
{
public int $x = 2;
}

function main() {}
32 changes: 32 additions & 0 deletions phpunit/src/StaticPropertyOverrideTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

use TypePhp\Exception\TestError;

/**
* A static property and an instance property are different kinds of storage;
* Zend forbids redeclaring one as the other in either direction ("Cannot
* redeclare static A::$x as non static B::$x" and vice versa).
*/
class StaticPropertyOverrideTest extends BaseTest
{
public function testMatchingStaticnessCompiles(): void
{
$this->compile('property_static_match.php');
}

public function testStaticCannotBecomeInstance(): void
{
$this->exec(
'Cannot redeclare static `A::$x` as non static `B::$x`',
'property_static_mismatch.php',
);
}

public function testInstanceCannotBecomeStatic(): void
{
$this->exec(
'Cannot redeclare non static `A::$x` as static `B::$x`',
'property_nonstatic_mismatch.php',
);
}
}
8 changes: 8 additions & 0 deletions src/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5375,6 +5375,14 @@ private function checkPropertyOverride(Node\Stmt\Class_|Node\Stmt\Trait_|Node\St
"`{$parentClass}::\${$name}`; property shadowing across inheritance is not allowed");
}
$matchedOverrides[$name] = true;
// A static property and an instance property are different
// kinds of storage; Zend forbids redeclaring one as the
// other in either direction.
if (($childProp->flags & Modifiers::STATIC) !== ($parentProp->flags & Modifiers::STATIC)) {
$this->fatalError($classStmt, ($parentProp->flags & Modifiers::STATIC)
? "Cannot redeclare static `{$parentClass}::\${$name}` as non static `{$className}::\${$name}`"
: "Cannot redeclare non static `{$parentClass}::\${$name}` as static `{$className}::\${$name}`");
}
// PHP inherits get and set independently. A child may
// override only one hook, or redeclare the property
// without hooks while retaining both parent hooks.
Expand Down
Loading