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:56] – nicolasgrekas | rfc:pass_scope_to_magic_accessors [2023/01/19 17:39] – nicolasgrekas | ||
---|---|---|---|
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: '' | ||
- | The calling scope 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 '' | + | |
+ | 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 would help to properly implement visibility-related logic. Right now, we have to call '' | Passing the calling scope to magic accessors would help to properly implement visibility-related logic. Right now, we have to call '' |
rfc/pass_scope_to_magic_accessors.txt · Last modified: 2023/05/09 16:25 by nicolasgrekas