php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #79925 Magic method __set_state signature check does not support covariance
Submitted: 2020-08-02 21:35 UTC Modified: 2020-08-02 21:42 UTC
From: ondrej at mirtes dot cz Assigned:
Status: Closed Package: Class/Object related
PHP Version: 8.0Git-2020-08-02 (Git) OS: All
Private report: No CVE-ID: None
 [2020-08-02 21:35 UTC] ondrej at mirtes dot cz
Description:
------------
Magic methods from this RFC https://wiki.php.net/rfc/magic-methods-signature should support covariance but don't. You should be able to narrow down a return type but currently can't.

Test script:
---------------
<?php

class Foo
{
	/**
	 * @param mixed[] $properties
	 * @return Type
	 */
	public static function __set_state(array $properties): self
	{
		return new self();
	}
}

Expected result:
----------------
nothing

Actual result:
--------------
Fatal error: Foo::__set_state(): Return type must be object when declared in...

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-08-02 21:42 UTC] requinix@php.net
-Status: Open +Status: Verified -Package: *General Issues +Package: Class/Object related
 [2020-08-03 07:16 UTC] ondrej at mirtes dot cz
Looks like the parameter types should also be given some care, I tried to see if they support covariance and so far it looks like they're not enforced at all:

public static function __set_state(int $properties): object

leads to no error on 3v4l.

This also doesn't but I expect that:

public static function __set_state(mixed $properties): object
 [2020-08-03 07:18 UTC] ondrej at mirtes dot cz
*contravariance
 [2020-08-03 08:35 UTC] nikic@php.net
Automatic comment on behalf of nikita.ppv@gmail.com
Revision: http://git.php.net/?p=php-src.git;a=commit;h=bb8b95be636528ebfc6f00b2153254d06e27ead9
Log: Fix bug #79925
 [2020-08-03 08:35 UTC] nikic@php.net
-Status: Verified +Status: Closed
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Dec 26 10:01:29 2024 UTC