php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #76905 Scope variable resolution
Submitted: 2018-09-19 17:52 UTC Modified: 2018-09-19 19:40 UTC
From: rawsrc at gmail dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 7.2.10 OS: Windows 7
Private report: No CVE-ID: None
 [2018-09-19 17:52 UTC] rawsrc at gmail dot com
Description:
------------
Hi,

Standard PHP 7.2.10 installation on Windows 7x64.


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

declare(strict_types=1);

namespace ns1 {
    class abc
    {

    }
};

namespace ns2 {
    interface interfaceXyz
    {
        public function getMe(): \ns1\abc;
    }
};

namespace ns3 {
    class def extends \ns1\abc
    {
        function checkInstance()
        {
            return ($this instanceof \ns1\abc)
                ? '\ns3\def is an instance of \ns1\abc'
                : '\ns3\def is different of \ns1\abc';
        }
    }

    echo (new def)->checkInstance();     // result = "\ns3\def is an instance of \ns1\abc"
};

namespace ns4 {
    class ghi implements \ns2\interfaceXyz
    {
        public function getMe(): \ns3\def
        {
            // here : \ns3\def (derivated from \ns1\abc ) is an instance of \ns1\abc but is not compliant with the interface that expect an \ns1\abc instance !!!
            return 'hello world';
        }
    }

    echo (new ghi)->getMe();

}

Expected result:
----------------
Hello world

Actual result:
--------------
\ns3\def is an instance of \ns1\abc

Fatal error: Declaration of ns4\ghi::getMe(): ns3\def must be compatible with ns2\interfaceXyz::getMe(): ns1\abc 

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2018-09-19 17:59 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2018-09-19 17:59 UTC] requinix@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

http://php.net/manual/en/functions.returning-values.php#functions.returning-values.type-declaration
> Note:
> When overriding a parent method, the child's method must match any return type declaration on the parent. If the
> parent doesn't define a return type, then the child method may do so.

Return types are currently invariant, meaning the type must exactly match the parent. That may change in the future.
 [2018-09-19 19:40 UTC] rawsrc at gmail dot com
Thanks for your quick answer :-)

"That may change in the future."

Great, i hope so.
 
PHP Copyright © 2001-2018 The PHP Group
All rights reserved.
Last updated: Fri Nov 16 12:01:26 2018 UTC