php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #50296 Automatically translate "self" in method_exists
Submitted: 2009-11-25 17:24 UTC Modified: 2021-02-18 11:33 UTC
Votes:2
Avg. Score:3.0 ± 2.0
Reproduced:0 of 1 (0.0%)
From: soapergem at gmail dot com Assigned:
Status: Wont fix Package: Scripting Engine problem
PHP Version: 5.3.1 OS: Windows XP
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: soapergem at gmail dot com
New email:
PHP Version: OS:

 

 [2009-11-25 17:24 UTC] soapergem at gmail dot com
Description:
------------
In reference to bug #50289:

If I call method_exists("self", "method") from within a class, it 
should automatically translate "self" to the classname. Right now the 
behavior is not obvious; the behavior does not adhere to the KISS 
principle.

1. method_exists already can check for static methods.
2. "self" can already automatically translate to the current class 
name, for instance in call_user_func.
3. The most logical, simple and obvious expectation is that if you 
combine these two--you call method_exists with "self"--that it will 
automatically translate "self" to the current class name, and check 
for the method. Currently it does not. The current behavior is 
counterintuitive and confusing.

Making this change is not dangerous. Making this change will not cause 
any backwards-compatibility issues.

1. "self" is a reserved word. You cannot declare a class named "self"-
-this will issue a fatal error. So there is no possibility of any 
ambiguity. You cannot confuse "self" with any other class, because no 
other class can be named "self".

It does not make sense to have "self" work in some contexts but not in 
others. It would be better if it were consistent.

Reproduce code:
---------------
<?php
class Test
{
    public static function check()
    {
        var_dump(method_exists("self", "method"));
    }
    public static function method() { }
}
Test::check();
?>

Expected result:
----------------
bool(true)

Actual result:
--------------
bool(false)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-12-01 16:29 UTC] jani@php.net
-Package: Feature/Change Request +Package: Scripting Engine problem
 [2011-01-12 20:04 UTC] m dot grundner at delegate dot at
Why don't you use method_exists(__CLASS__, "method")?
 [2021-02-18 11:33 UTC] nikic@php.net
-Status: Open +Status: Wont fix
 [2021-02-18 11:33 UTC] nikic@php.net
Support for self/parent/static in callables is nowadays considered to have been a mistake, as it makes them context-dependent. I think I can safely say that this will not be supported for method_exists(). I think that (with the exception of callables) PHP fairly consistenty rejects "self" etc for dynamic class names.

The class name can be obtained using self::class, or __CLASS__.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 16 05:01:29 2024 UTC