Skip to content

Parent class hooks don't work from child class #17946

@sergeant13

Description

@sergeant13

Description

The following code:

<?php
abstract class BarAbstract
{
    abstract public string $foo {
        get;
        set;
    }
}

class Bar extends BarAbstract
{
    public string $foo {
        get => $this->foo.' (Bar)';
        set => $value;
    }
}

class BarChild extends Bar
{
    public string $foo {
        set {
            parent::$foo::set($value);
        }
    }
}

$barChild = new BarChild;
try {
    $barChild->foo = 'qwerty';
    echo $barChild->foo;
} catch (\Throwable $e) {
    echo $e->getMessage();
}

Resulted in this output:

Maximum call stack size of 8339456 bytes (zend.max_allowed_stack_size - zend.reserved_stack_size) reached. Infinite recursion?

But I expected this output instead:

qwerty (Bar)

The get and set hooks of the foo property of the Bar class do not work when called from the BarChild class. If the Bar class does not extend BarAbstract, no error occurs.

PHP Version

PHP 8.4.4

Operating System

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions