php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #66739 LSB scope on scoped call from incompatible context
Submitted: 2014-02-18 22:45 UTC Modified: 2016-05-05 10:00 UTC
Votes:1
Avg. Score:1.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: temenb at gmail dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.4Git-2014-02-18 (Git) OS: ubuntu
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: temenb at gmail dot com
New email:
PHP Version: OS:

 

 [2014-02-18 22:45 UTC] temenb at gmail dot com
Description:
------------
static::foo() in the method A::a1() in some cases calls method B::foo()

It is not late static bindings,  B doesn't extend A

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

class A
{
    public function a1()
    {             
        static::foo();
    }

    static public function a2()
    {       
        static::foo();
    }
    
    static public function foo()
    {
        echo 'A';
    }
}

class B
{
    public function b1()
    {
        A::a1();
        A::a2();
    }
    
    static public function b2()
    {
        A::a1();
        A::a2();
    }
    
    static public function foo()
    {    
        echo 'B';    
    }
}

B::b1();//A
B::b2();//A

$b = new B;
$b->b1();//B
$b->b2();//A


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-02-19 02:53 UTC] coolwust at gmail dot com
B::b1();//b1 is object method

$b->b2();//b2 is static method

I think the syntax you calling the method is not correct
 [2014-02-19 07:36 UTC] temenb at gmail dot com
Yes. It's true. not obvious. But php allows to call function this way
 [2014-06-27 12:36 UTC] arjen at react dot com
Exact output/warnings @ http://3v4l.org/ek1KG

Behaviour changed in 5.7 (phpng).
 [2014-06-27 12:42 UTC] pajoye@php.net
@ arjen at react dot com
phpng is an independent feature branch.

We have branches like for 5.4, 5.5, 5.6 or master. Which one do you use?
 [2014-07-09 14:12 UTC] arjen at react dot com
phpng is listed on 3v4l.org as php 5.7, which is compiled from the phpng branch.

In this branch, it returns AAAAAAAA. All other versions return AAAABAAA
 [2016-05-05 09:58 UTC] nikic@php.net
-Status: Open +Status: Not a bug
 [2016-05-05 09:58 UTC] nikic@php.net
So, what's happen here is this: On PHP 5.x we allow scoped calls to incompatible contexts, by adopting the $this from the incompatible scope. In this instance the A::a1() call in B::b1() will result the $this of A::a1() being the instance of B. If there is a $this, then static:: refers to get_class($this). As such you get a return value of B.

In PHP 7.0 we no longer allow calls with incompatible contexts, or rather they are still allowed, but $this will not be bound. As such the result will be A like for a normal static call.

So, the behavior is correct for the semantics of the respective PHP versions, even though the 5.x semantics are now obsolete.
 [2016-05-05 10:00 UTC] nikic@php.net
-Summary: strange behavior +Summary: LSB scope on scoped call from incompatible context
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Tue Oct 06 08:00:02 2026 UTC