Skip to content
Merged
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
11 changes: 11 additions & 0 deletions Zend/tests/anon/gh15994.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
--TEST--
Abstract function must be implemented
--FILE--
<?php

$c = new class {
abstract public function f();
};
Copy link
Member

@iluuu1994 iluuu1994 Sep 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, this message doesn't make too much sense to me, as we cannot implement a method that is also declared as abstract. Maybe we should disallow abstract functions in anonymous classes in the compiler. This message would still make sense when the method comes from a parent class, interface or trait.

Granted, that is not really related to this change, so I'm happy if you move the abstract method to a parent class.

In reality, we should probably also change the error message for this:

class Foo {
    public abstract function bar();
}

Fatal error: Class Foo contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (Foo::bar)

Implementing Foo::bar() is a weird suggestion, given the abstract declaration comes from this class.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should disallow abstract functions in anonymous classes in the compiler.

I can work on a patch for that

I'm happy if you move the abstract method to a parent class.

done

?>
--EXPECTF--
Fatal error: Class class@anonymous must implement 1 abstract method (class@anonymous::f) in %sgh15994.php on line 3
2 changes: 1 addition & 1 deletion Zend/tests/gh7792_1.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ enum B implements A {}

?>
--EXPECTF--
Fatal error: Enum B must implement 1 abstract private method (A::a) in %s on line %d
Fatal error: Enum B must implement 1 abstract method (A::a) in %s on line %d
2 changes: 1 addition & 1 deletion Zend/tests/traits/abstract_method_6.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ class D extends C {

?>
--EXPECTF--
Fatal error: Class C must implement 1 abstract private method (C::method) in %s on line %d
Fatal error: Class C must implement 1 abstract method (C::method) in %s on line %d
4 changes: 2 additions & 2 deletions Zend/zend_inheritance.c
Original file line number Diff line number Diff line change
Expand Up @@ -2981,7 +2981,7 @@ void zend_verify_abstract_class(zend_class_entry *ce) /* {{{ */
const zend_function *func;
zend_abstract_info ai;
bool is_explicit_abstract = (ce->ce_flags & ZEND_ACC_EXPLICIT_ABSTRACT_CLASS) != 0;
bool can_be_abstract = (ce->ce_flags & ZEND_ACC_ENUM) == 0;
bool can_be_abstract = (ce->ce_flags & (ZEND_ACC_ENUM|ZEND_ACC_ANON_CLASS)) == 0;
memset(&ai, 0, sizeof(ai));

ZEND_HASH_MAP_FOREACH_PTR(&ce->function_table, func) {
Expand Down Expand Up @@ -3022,7 +3022,7 @@ void zend_verify_abstract_class(zend_class_entry *ce) /* {{{ */
);
} else {
zend_error_noreturn(E_ERROR,
"%s %s must implement %d abstract private method%s (" MAX_ABSTRACT_INFO_FMT MAX_ABSTRACT_INFO_FMT MAX_ABSTRACT_INFO_FMT ")",
"%s %s must implement %d abstract method%s (" MAX_ABSTRACT_INFO_FMT MAX_ABSTRACT_INFO_FMT MAX_ABSTRACT_INFO_FMT ")",
zend_get_object_type_uc(ce),
ZSTR_VAL(ce->name), ai.cnt,
ai.cnt > 1 ? "s" : "",
Expand Down
Loading