php / php/php-src

Property hooks do not call parent's magic `__get` and `__set` methods when referencing parent property

Open
#17,542 10 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug Category: Engine Status: Needs Triage
Dominant language
C
Stars
40.4k
Forks
8.1k
Avg merge
2d 13h
Merged PRs (30d)
96

Description

Description

I was playing around with property hooks and noticed the following (in my opinion) inconsistency when combining property hooks with inheritance and __get and __set:

The following code (for __get, the same applies for __set in https://3v4l.org/Q2OAj):

<?php

class ParentClass {
    public function __get(string $name) {
        echo "__get($name) was called\n";
    }
}

class ChildClass extends ParentClass {
    public $foo {
        get => parent::$foo::get();
    }
}

(new ParentClass)->foo;
(new ChildClass)->foo;

Resulted in this output:

__get(foo) was called

Fatal error: Uncaught Error: Undefined property ParentClass::$foo in /in/Y27mB:13

But I expected this output instead:

__get(foo) was called
__get(foo) was called

I tried searching the RFC and existing PHP issues, but I haven't found this example - apologies if I missed it. Barring something saying that this shouldn't work, I would expect it to.

PHP Version

PHP 8.4.3

Operating System

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the provided PHP 8.4.3 reproducer and compare the property-hook paths for parent::$foo::get and parent::$foo::set with direct magic access. The payload names no php-src files or tests, so trace the relevant property-hook and inheritance handling, then verify that both examples invoke the parent's magic methods without an undefined-property error.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.