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
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
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

Pull Requests

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-2025 The PHP Group
All rights reserved.
Last updated: Sun Jul 13 15:01:33 2025 UTC