rfc:pass_scope_to_magic_accessors
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionLast revisionBoth sides next revision | ||
rfc:pass_scope_to_magic_accessors [2023/01/19 15:24] – nicolasgrekas | rfc:pass_scope_to_magic_accessors [2023/01/19 17:39] – nicolasgrekas | ||
---|---|---|---|
Line 3: | Line 3: | ||
* Date: 2023-01-19 | * Date: 2023-01-19 | ||
* Author: Nicolas Grekas < | * Author: Nicolas Grekas < | ||
- | * Status: | + | * Status: |
* First Published at: https:// | * First Published at: https:// | ||
* Implementation: | * Implementation: | ||
Line 14: | Line 14: | ||
This RFC proposes to pass their calling scope to magic accessors: '' | This RFC proposes to pass their calling scope to magic accessors: '' | ||
- | This would help to properly implement visibility-related logic. Right now, we have to call '' | + | |
+ | The calling scope is defined as the name of the class that is calling a magic method. It's a string. It can also be null when a magic method is called outside of any class context, as is the case when calling them from the global scope or from the body of a named function. For closures, their scope is the class name they were created in (if any), or their rebound scope when using '' | ||
+ | |||
+ | Here are some examples: | ||
+ | |||
+ | < | ||
+ | class Foo { | ||
+ | private $abc; | ||
+ | |||
+ | public function __construct() | ||
+ | { | ||
+ | unset($this-> | ||
+ | } | ||
+ | |||
+ | public function __get($name) { | ||
+ | var_dump(debug_backtrace()[1][' | ||
+ | } | ||
+ | } | ||
+ | |||
+ | $foo = new Foo(); | ||
+ | |||
+ | $foo-> | ||
+ | |||
+ | class Bar { | ||
+ | public function __construct($foo) { | ||
+ | $foo-> | ||
+ | } | ||
+ | } | ||
+ | |||
+ | $bar = new Bar($foo); // string(3) " | ||
+ | |||
+ | $f = fn ($foo) => $foo-> | ||
+ | |||
+ | $f($foo); // NULL | ||
+ | |||
+ | $g = \Closure:: | ||
+ | |||
+ | $g($foo); // string(3) " | ||
+ | </ | ||
+ | |||
+ | Passing the calling scope to magic accessors | ||
Right now, the logic to access the calling scope is quite involving. This example doesn' | Right now, the logic to access the calling scope is quite involving. This example doesn' | ||
Line 57: | Line 97: | ||
The PHP engine will pass the calling scope to magic accessors even if they don't declare the corresponding argument. The corresponding value will always be accessible via '' | The PHP engine will pass the calling scope to magic accessors even if they don't declare the corresponding argument. The corresponding value will always be accessible via '' | ||
- | This ensures that userland will be able to write code that works on both PHP <= 8.2 and PHP >= 8.3. (As a reminder, the engine forbids adding any extra arguments to the signature of magic methods: it's not allowed to declare a '' | + | This ensures that userland will be able to write code that works on both PHP <= 8.2 and PHP >= 8.3. (As a reminder, the engine forbids adding any extra arguments to the signature of magic methods: it's not allowed to declare a '' |
For consistency with the checks in place for the currently accepted arguments, the engine will throw a fatal error when the new argument is declared with a type that is not compatible with '' | For consistency with the checks in place for the currently accepted arguments, the engine will throw a fatal error when the new argument is declared with a type that is not compatible with '' |
rfc/pass_scope_to_magic_accessors.txt · Last modified: 2023/05/09 16:25 by nicolasgrekas