php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #76581 Multi-level parent/self does not get correctly resolved
Submitted: 2018-07-04 23:32 UTC Modified: 2020-06-25 08:16 UTC
Votes:2
Avg. Score:4.0 ± 1.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: levim@php.net Assigned: levim (profile)
Status: Closed Package: Class/Object related
PHP Version: 7.3.0alpha2 OS:
Private report: No CVE-ID: None
 [2018-07-04 23:32 UTC] levim@php.net
Description:
------------
This affects all PHP 7.X versions to date (7.3 alpha 1).

Test script:
---------------
https://3v4l.org/Od72V

<?php

interface A {
	function m(B $s): self;
}

class B implements A {
	function m(self $s): parent {
		return $this;
	}
}

class C extends B {
	function m(parent $s): A {
		return $this;
	}
}
?>

Expected result:
----------------
No error.

Actual result:
--------------
Fatal error: Declaration of C::m(B $s): A must be compatible with B::m(B $s): parent in % on line %d

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2018-07-04 23:32 UTC] levim@php.net
-Assigned To: +Assigned To: levim
 [2018-07-04 23:46 UTC] levim@php.net
Looking at this more I think perhaps the real bug is when defining `B::m(): parent` that it doesn't error. In most (all?) other contexts `parent` refers to class inheritance, not interface inheritance.
 [2018-07-05 18:02 UTC] kalle@php.net
I agree that the decl of B::m(B $s) : parent should error out if B does not extend another class, the decl of C::m(parent $s) : A makes sense, but the error should happen earlier to show the real issue.
 [2020-06-25 08:16 UTC] nikic@php.net
-Status: Assigned +Status: Closed
 [2020-06-25 08:16 UTC] nikic@php.net
Behavior since PHP 7.4 is:

Deprecated: Cannot use "parent" when current class scope has no parent

Fatal error: Could not check compatibility between B::m(B $s): parent and A::m(B $s): A, because class parent is not available

I think that's pretty clear. And in PHP 8:

Fatal error: Cannot use "parent" when current class scope has no parent
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Dec 04 03:01:30 2024 UTC