php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #76199 __METHOD__ and Paamayim Nekudotayim
Submitted: 2018-04-08 18:29 UTC Modified: 2018-04-09 02:25 UTC
From: valentiny510 at gmail dot com Assigned:
Status: Wont fix Package: *General Issues
PHP Version: 7.2.4 OS: X
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: valentiny510 at gmail dot com
New email:
PHP Version: OS:

 

 [2018-04-08 18:29 UTC] valentiny510 at gmail dot com
Description:
------------
__METHOD__ return the double colon inside the classes no matter what the scope is. I think it should return -> for non static methods.

I know, I know.. is a very long shot and is somehow useless this scenario but...
imagine the script below.

Maybe would also avoid some confusions, for instance, if you want to debug and you just want to return the name of the method without executing it...

Is not a big deal but anyways... Thank you


Test script:
---------------
class Test {
    function n($n, $return_method = false) {
        if ($return_method)
            return __METHOD__;
        return "N is $n";
    }
}
$test = new Test;
$method = $test->n(5, true);
print $method . PHP_EOL;
print $method(5) . PHP_EOL;


Expected result:
----------------
Test::n
N is 5

Actual result:
--------------
Test::n
Deprecated: Non-static method Test::n() should not be called statically
N is 5

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2018-04-09 02:25 UTC] requinix@php.net
-Status: Open +Status: Wont fix
 [2018-04-09 02:25 UTC] requinix@php.net
That won't work. https://3v4l.org/vav7G

The double colon does not mean a static method call. It's called the "scope resolution operator", not the "static call operator". Whether a method call is instanced or static depends on how you call it.
And the problem is that you're trying to call the method without using a particular instance. Consider:

  $test1 = new Test;
  $test2 = new Test;
  $method = $test1->n(5, true);

If $method has "Test->n" then that doesn't do anything to tell PHP which instance of Test it should use to call the method.

If you want to use a magic constant, use __FUNCTION__. https://3v4l.org/X42fU
 [2018-07-20 05:31 UTC] valentiny510 at gmail dot com
Apparently is working but the only issue is that is throwing the deprecated error... you should already know that the __FUNCTION__ constant only gives you the methods's name and not the class name too...
"
$test1 = new Test;
$method = $test1->n(5, true);
If $method has "Test->n" then that doesn't do anything to tell PHP which instance of Test it should use to call the method.
"
that is not true, the class is already instantiated... but if you need/want to create a new instance you can always create and anonymous function behind the scene,
Ex: $method = (new Test)->n... or similar

btw... why down't remove the "Non-static methods" errors all together ?
Ik that may sound very strange but has a reasonable answer, in short, PHP can detect if there is any instance for that called function/method and act accordingly...
will be very very very useful in many cases to be able to call the same method statically and not-statically... simple example that came to mind right now:

class a {
    private $something = null;
    private static $default = 'default';
    function construct($something){ $this->something = $something; }
    function b( ){ return $this->something ?? self::$default; }
}

$i = new a('something');
print $i->b( ) . PHP_EOL;

print a::b( ) . PHP_EOL;

Expected result is
something
default

I bet there are hundreds of better examples, but we should focus more in the productivity and functionality... so what if I call it statically if/when I need ? or AT LEAST allow having methods with the same names, non and static...
class X {
    function Y(){}
    static function Y(){}
}

You have no idea how many time I need it to pass info/data from static variables to non static...
and there is no easy way to do it...
 [2018-07-20 05:31 UTC] valentiny510 at gmail dot com
Apparently is working but the only issue is that is throwing the deprecated error... you should already know that the __FUNCTION__ constant only gives you the methods's name and not the class name too...
"
$test1 = new Test;
$method = $test1->n(5, true);
If $method has "Test->n" then that doesn't do anything to tell PHP which instance of Test it should use to call the method.
"
that is not true, the class is already instantiated... but if you need/want to create a new instance you can always create and anonymous function behind the scene,
Ex: $method = (new Test)->n... or similar

btw... why down't remove the "Non-static methods" errors all together ?
Ik that may sound very strange but has a reasonable answer, in short, PHP can detect if there is any instance for that called function/method and act accordingly...
will be very very very useful in many cases to be able to call the same method statically and not-statically... simple example that came to mind right now:

class a {
    private $something = null;
    private static $default = 'default';
    function construct($something){ $this->something = $something; }
    function b( ){ return $this->something ?? self::$default; }
}

$i = new a('something');
print $i->b( ) . PHP_EOL;

print a::b( ) . PHP_EOL;

Expected result is
something
default

I bet there are hundreds of better examples, but we should focus more in the productivity and functionality... so what if I call it statically if/when I need ? or AT LEAST allow having methods with the same names, non and static...
class X {
    function Y(){}
    static function Y(){}
}

You have no idea how many time I need it to pass info/data from static variables to non static...
and there is no easy way to do it...
 [2018-07-20 05:35 UTC] valentiny510 at gmail dot com
ups, very sorry for the double post, the cookies/captcha are the culprits..
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 16 15:01:29 2024 UTC