Skip to content

Fix GH-13177: PHP 8.3.2: final private constructor not allowed when used in trait #13179

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
50 changes: 50 additions & 0 deletions Zend/tests/traits/bugs/gh13177.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
--TEST--
GH-13177 (PHP 8.3.2: final private constructor not allowed when used in trait)
--FILE--
<?php

trait Bar {
final private function __construct() {}
}

final class Foo1 {
use Bar;
}

final class Foo2 {
use Bar {
__construct as final;
}
}

class Foo3 {
use Bar {
__construct as final;
}
}

for ($i = 1; $i <= 3; $i++) {
$rc = new ReflectionClass("Foo$i");
echo $rc->getMethod("__construct"), "\n";
}

class Foo4 extends Foo3 {
private function __construct() {}
}

?>
--EXPECTF--
Method [ <user, ctor> final private method __construct ] {
@@ %sgh13177.php 4 - 4
}

Method [ <user, ctor> final private method __construct ] {
@@ %sgh13177.php 4 - 4
}

Method [ <user, ctor> final private method __construct ] {
@@ %sgh13177.php 4 - 4
}


Fatal error: Cannot override final method Foo3::__construct() in %s on line %d
2 changes: 1 addition & 1 deletion Zend/zend_inheritance.c
Original file line number Diff line number Diff line change
Expand Up @@ -1949,7 +1949,7 @@ static void zend_add_trait_method(zend_class_entry *ce, zend_string *name, zend_
zend_function *new_fn;
bool check_inheritance = false;

if ((fn->common.fn_flags & (ZEND_ACC_PRIVATE | ZEND_ACC_FINAL)) == (ZEND_ACC_PRIVATE | ZEND_ACC_FINAL)) {
if ((fn->common.fn_flags & (ZEND_ACC_PRIVATE | ZEND_ACC_FINAL)) == (ZEND_ACC_PRIVATE | ZEND_ACC_FINAL) && !zend_string_equals_literal_ci(name, ZEND_CONSTRUCTOR_FUNC_NAME)) {
zend_error(E_COMPILE_WARNING, "Private methods cannot be final as they are never overridden by other classes");
}

Expand Down