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 revision | ||
rfc:pass_scope_to_magic_accessors [2023/01/19 14:54] – nicolasgrekas | rfc:pass_scope_to_magic_accessors [2023/05/09 16:25] (current) – nicolasgrekas | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== PHP RFC: Pass Scope to Magic Accessors ====== | + | This RFC has moved to [[rfc:access_scope_from_magic_accessors]] |
- | * Version: 1.0 | + | |
- | * Date: 2023-01-19 | + | |
- | * Author: Nicolas Grekas < | + | |
- | * Status: Draft | + | |
- | * First Published at: https:// | + | |
- | * Implementation: | + | |
- | + | ||
- | ===== Introduction ===== | + | |
- | When using magic methods to access actual properties, respecting their declared visibility is often desired. Yet, accessing the calling scope to emulate the visibility restrictions is unreasonably difficult at the moment. | + | |
- | + | ||
- | ===== Proposal ===== | + | |
- | + | ||
- | 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 '' | + | |
- | + | ||
- | Right now, the logic to access the calling scope is quite involving. This example doesn' | + | |
- | + | ||
- | < | + | |
- | class Foo extends Bar | + | |
- | { | + | |
- | public function __get($name): | + | |
- | { | + | |
- | $frame = debug_backtrace(\DEBUG_BACKTRACE_PROVIDE_OBJECT | \DEBUG_BACKTRACE_IGNORE_ARGS, | + | |
- | + | ||
- | $callingScope = $frame[' | + | |
- | + | ||
- | if (\ReflectionProperty::class === $callingScope) { | + | |
- | $callingScope = $frame[' | + | |
- | } | + | |
- | + | ||
- | // Now we can implement visibility-related logic for accessing $name | + | |
- | + | ||
- | // Let's say we want to call parent:: | + | |
- | // scope, it's now broken because instead of getting frame `[1]`, it should use frame `[2]`. | + | |
- | // But knowing this requires more non trivial logic. | + | |
- | } | + | |
- | } | + | |
- | </ | + | |
- | + | ||
- | If this RFC is accepted, we'll be able to do this instead: | + | |
- | + | ||
- | < | + | |
- | class Foo extends Bar | + | |
- | { | + | |
- | public function __get($name, | + | |
- | { | + | |
- | // $callingScope is populated with the value know by the engine | + | |
- | + | ||
- | // Calling parent:: | + | |
- | // without resorting to any specific logic | + | |
- | } | + | |
- | } | + | |
- | </ | + | |
- | + | ||
- | 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 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 '' | + | |
- | + | ||
- | ===== Proposed PHP Version ===== | + | |
- | + | ||
- | PHP 8.3 | + | |
- | + | ||
- | ===== RFC Impact ===== | + | |
- | + | ||
- | ===== Unaffected PHP Functionality ===== | + | |
- | + | ||
- | Existing magic accessors won't be required to declare the new argument. | + | |
- | + | ||
- | ==== Backward Incompatible Changes ==== | + | |
- | + | ||
- | None | + | |
- | + | ||
- | ==== To Existing Extensions ==== | + | |
- | + | ||
- | Because the engine forbids calling internal methods with extra arguments, extensions that declare magic accessors (e.g. soap) will need to declare the new argument. | + | |
- | + | ||
- | ===== Proposed Voting Choices ===== | + | |
- | + | ||
- | Pass calling scope to magic accessors: yes/no? | + | |
- | + | ||
- | ===== Implementation ===== | + | |
- | After the project is implemented, | + | |
- | - the version(s) it was merged into | + | |
- | - a link to the git commit(s) | + | |
- | - a link to the PHP manual entry for the feature | + | |
- | - a link to the language specification section (if any) | + |
rfc/pass_scope_to_magic_accessors.1674140066.txt.gz · Last modified: 2023/01/19 14:54 by nicolasgrekas