php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #44011 self doesn't refer to the class from which i called a static function
Submitted: 2008-02-01 06:46 UTC Modified: 2008-02-01 10:40 UTC
From: ms419 at freezone dot co dot uk Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 5.2.5 OS: Debian
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: ms419 at freezone dot co dot uk
New email:
PHP Version: OS:

 

 [2008-02-01 06:46 UTC] ms419 at freezone dot co dot uk
Description:
------------
When I call Bar::aaa(); I expect the self keyword to refer to the Bar class, even if aaa() is defined in a class which Bar extends.

The behavior I observe is that, if aaa() is defined in a class Foo which Bar extends, even when I call Bar::aaa(), self refers to Foo.

In this case the self keyword is not actually that much use, since if I type "self" in the definition of the Foo class, I know the class name and might as well type "Foo".

I had hoped that the self keyword would enable me to override some static functions from a parent class, without forcing me to override the static functions which call them.

Reproduce code:
---------------
<?php

class Foo
{
  public static function aaa()
  {
    return self::bbb();
  }

  public static function bbb()
  {
    return 'Foo';
  }
}

class Bar extends Foo
{
  public static function bbb()
  {
    return 'Bar';
  }
}

var_dump(Bar::aaa());


Expected result:
----------------
string(3) "Bar"


Actual result:
--------------
string(3) "Foo"


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-02-01 10:40 UTC] felipe@php.net
In this case, you will need use 'return static::bbb();'. (As of PHP 5.3.0)

http://docs.php.net/manual/en/language.oop5.late-static-bindings.php
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Jun 17 18:01:31 2024 UTC