php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #52741 OOP late static bindings bug
Submitted: 2010-08-30 17:33 UTC Modified: 2011-07-12 15:43 UTC
From: flyguy dot by at gmail dot com Assigned: colder (profile)
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.3.3 OS: *
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: flyguy dot by at gmail dot com
New email:
PHP Version: OS:

 

 [2010-08-30 17:33 UTC] flyguy dot by at gmail dot com
Description:
------------
This is code prints "test2". Why ?

Test script:
---------------
class test1 {
 public static function testing_method() {
  var_dump(get_called_class());
 }
}
class test2 {
 public static function testing_method() {
  parent::testing_method();
 }
}


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-08-30 18:35 UTC] johannes@php.net
-Status: Open +Status: Assigned -Operating System: Windows Vista +Operating System: * -Assigned To: +Assigned To: colder
 [2010-08-30 18:35 UTC] johannes@php.net
Etienne, you are the LSB expert - what's your take on this? - In the given example I can see the reporter's point. (While there is a "extends test1" missing)

I wonder about this:

class test1 {
 public static function testing_method() {
  var_dump(get_called_class());
 }
}
class test2 extends test1 {
}
class test3 extends test2 {
 public static function testing_method() {
  parent::testing_method();
 }
}

Here parent might refer to test2, test2 inherits testing_method() from test1 so the called class might be test2 ... some might argue that test1 is correct.

I think the most simple thing is to keep the current behavior and define parent not to change the lsb scope.
 [2010-09-13 00:13 UTC] flyguy dot by at gmail dot com
-Status: Assigned +Status: Open
 [2010-09-13 00:13 UTC] flyguy dot by at gmail dot com
Ok. This situation is analogous to the one hand, but why then:
class my_parent {
 public static $field;
 public static function field_setup() {
  static::$field='asd';
 }
}
class my_child extends my_parent {
}
my_child::field_setup();
var_dump(my_child::$field);
var_dump(my_parent::$field);
----------
prints:
string 'asd' (length=3)
string 'asd' (length=3)
 [2010-09-13 21:42 UTC] colder@php.net
-Status: Open +Status: Feedback
 [2010-09-13 21:42 UTC] colder@php.net
parent:: forwards LSB information


In the initial case, it makes it perfectly normal for get_called_class to return 
test2, this is by design, if you don't want to pass the LSB info, reference your 
class by name, without keyword (i.e. test1::testing_method()).

---

Johannes:

on test3::testingmethod():
- parent::testing_method() will call parent (here test2), passing LSB info 
(test3)
- test2::testing_method falls back to test1::method, passing LSB info

test1::testing_method's get_called_class will be test3.

To me it sounds perfectly normal?

---

For the last comment:

static::$field is resolved to once my_child::$field and once my_parent::$field,

my_child::$field falls back to my_parent::$field, and updates it.

=> normal again

Am I missing something here?
 [2010-09-13 22:46 UTC] flyguy dot by at gmail dot com
-Status: Feedback +Status: Assigned
 [2010-09-13 22:46 UTC] flyguy dot by at gmail dot com
class my_parent  {
    public static $blabla='asd';
    public static function set_blabla($val) {
	static::$blabla=$val;
    }
}
class my_child extends my_parent {
}

my_child::set_blabla('qwerty');
var_dump(my_child::$blabla);
var_dump(my_parent::$blabla);
--------
prints: 
string 'qwerty' (length=6)
string 'qwerty' (length=6)
----
This is normal code too ?
Why changed extended propety on parent ?
By your logic inherited properties are not inherited properties.
Simply class-parent becomes part of the subclass.
 [2010-09-13 23:01 UTC] flyguy dot by at gmail dot com
Sory, my English is very bad :))
 [2010-09-14 09:52 UTC] colder@php.net
-Status: Assigned +Status: Open
 [2010-09-14 09:52 UTC] colder@php.net
You are dealing here with static properties (assigned to a class) and resolution 
fallbacks (reference to an undefined property in one class)

my_child::$blabla will fallback to the parent property, since strictly speaking 
no static property named "$blabla" exists in my_child. It will thus reference 
my_parent::$blabla.

No bug here.
 [2011-07-12 15:43 UTC] colder@php.net
-Status: Assigned +Status: Bogus
 [2011-07-12 15:43 UTC] colder@php.net
Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system is not the
appropriate forum for asking support questions.  Due to the volume
of reports we can not explain in detail here why your report is not
a bug.  The support channels will be able to provide an explanation
for you.

Thank you for your interest in PHP.

As explained, not a bug.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Jun 18 03:01:31 2024 UTC