Skip to content
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

Allow Throwable to be extended outside of Exception #17385

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
44 changes: 44 additions & 0 deletions Zend/tests/bug_test.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
--TEST--
Test extending \Throwable directly
--FILE--
<?php

class MyThrowable implements Throwable {
public function getMessage(): string {
return "MyThrowable message";
}

public function getCode() {
return 0;
}

public function getFile(): string {
return __FILE__;
}

public function getLine(): int {
return __LINE__;
}

public function getTrace(): array {
return [];
}

public function getPrevious(): ?Throwable {
return null;
}

public function getTraceAsString(): string {
return "";
}
}

try {
throw new MyThrowable();
} catch (Throwable $e) {
echo $e->getMessage(), "\n";
}

?>
--EXPECT--
MyThrowable message
22 changes: 1 addition & 21 deletions Zend/zend_exceptions.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,27 +57,7 @@ static zend_object_handlers default_exception_handlers;
/* {{{ zend_implement_throwable */
static int zend_implement_throwable(zend_class_entry *interface, zend_class_entry *class_type)
{
/* zend_ce_exception and zend_ce_error may not be initialized yet when this is called (e.g when
* implementing Throwable for Exception itself). Perform a manual inheritance check. */
zend_class_entry *root = class_type;
while (root->parent) {
root = root->parent;
}
if (zend_string_equals_literal(root->name, "Exception")
|| zend_string_equals_literal(root->name, "Error")) {
return SUCCESS;
}

bool can_extend = (class_type->ce_flags & ZEND_ACC_ENUM) == 0;

zend_error_noreturn(E_ERROR,
can_extend
? "%s %s cannot implement interface %s, extend Exception or Error instead"
: "%s %s cannot implement interface %s",
zend_get_object_type_uc(class_type),
ZSTR_VAL(class_type->name),
ZSTR_VAL(interface->name));
return FAILURE;
return SUCCESS;
}
/* }}} */

Expand Down
Loading