Skip to content

It is impossible to use parent:: on a method using a Generator. #17276

@SenseiTarzan

Description

@SenseiTarzan

Description

The following code:

<?php
class foo {
    public function test(): Generator {
        yield "hello";
    }
}

class bar extends foo {

    public function __construct(
        private array $exemple = [])
    {
    }

    public function test(): Generator {
        if (empty($this->exemple)) {
            parent::test();
        }else
        {
            yield $this->exemple;
        }
    }
}
# 1
$test = new foo();
foreach ($test->test() as $value) {
    var_dump($value);
}
//---------------------------------------------------------------

# 2
$bar = new bar();
foreach ($bar->test() as $value) {
    var_dump($value);
}
//---------------------------------------------------------------
# 3
$bar = new bar([
    "hello" => "world",
]);

foreach ($bar->test() as $value) {
    var_dump($value);
}

Resulted in this output:

# 1
## Result:
### string(5) "hello"

# 2
## Result:
### string(5) "hello"

# 3
## Result:
### array(1) {
###    ["hello"]=>
###  string(5) "world"
### }

But I expected this output instead:

# 2
## Result:
### No existent but normally i should have string(5) "hello"

The only way to execute the parent is to use yield from parent::{method}(). I'm not sure if this was intended, but it seems logical, albeit strange, because we expect parent:: to return the yield 'hello', but it doesn't and must be executed in a different manner.

PHP Version

PHP 8.2.27

Operating System

Ubuntu 22.04LTS and Windows 11

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