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 16:13] – 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' |
rfc/pass_scope_to_magic_accessors.txt · Last modified: 2023/05/09 16:25 by nicolasgrekas