[php-src] Issue #19121: unwanted errors magical methods in trait
| From: | rino1 | Date: | Mon, 14 Jul 2025 07:55:21 +0000 |
| Subject: | [php-src] Issue #19121: unwanted errors magical methods in trait | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-250428@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/19121
Author: rino1
### Description
# PREMISE
To simplify things, only the __invoke() method will be considered.
# Here's my scenario
```
+ tree
├── MyTrait.php
├── use-as-public.php
└── use.php
+ cat MyTrait.php
<?php
trait MyTrait {
private function __invoke(){}
//protected function __invoke(){} // present the same problems
/*
* other magical methods
* (as __toString(), __clone(), __debugInfo(), etc.)
* present the same problems
*/
}
+ cat use.php
<?php require "MyTrait.php";
new class {
use MyTrait;
};
+ cat use-as-public.php
<?php require "MyTrait.php";
new class {
use MyTrait {
__invoke as public;
}
};
```
Let's do some tests
# CASE 1
```
+ php MyTrait.php
PHP Warning: The magic method MyTrait::__invoke() must have public visibility in MyTrait.php on
line 3
```
But we're in a trait, and in my opinion, certain things should be evaluated differently than a
class.
The error we saw shouldn't appear!!!
# CASE 2
```
+ php use.php
PHP Warning: The magic method MyTrait::__invoke() must have public visibility in MyTrait.php on
line 3
```
If anything
PHP Warning: The magic method class@anonymous::__invoke() must have public visibility in use.php on
line 3
# CASE 3
```
+ php use-as-public.php
PHP Warning: The magic method MyTrait::__invoke() must have public visibility in MyTrait.php on
line 3
```
If anything
PHP Warning: The magic method class@anonymous::__invoke() must have public visibility in
use-as-public.php on line 3
but also no
```
+ grep 'as public' use-as-public.php
__invoke as public;
```
and even in this case there, is no need to add anything else.
# CONCLUSION
In a future version of PHP, I think certain aspects should be revised.
What do you think?
Would anyone be willing to create a dedicated official RFC?
https://wiki.php.net/rfc/howto