|  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

Standard PHP 7.2.10 installation on Windows 7x64.

Test script:


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 


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2018-09-19 17:59 UTC]
-Status: Open +Status: Not a bug
 [2018-09-19 17:59 UTC]
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at and the instructions on how to report
a bug at
> 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-2020 The PHP Group
All rights reserved.
Last updated: Wed Jul 08 08:01:26 2020 UTC