Difference between __METHOD__ and __FUNCTION__ Magic Variables in PHP

  • 2021-07-21 07:35:07
  • OfStack

This article tells the difference between magic variables __METHOD__ and __FUNCTION__ in PHP, and shares it for your reference. The specific analysis is as follows:

The method name of the __METHOD__ class (newly added to PHP 5.0. 0). Returns the name (case-sensitive) of the method when it was defined.
__FUNCTION__ function name (PHP 4.3. 0 newly added). Since PHP 5, this constant returns the name (case-sensitive) of the function when it was defined. This value is always lowercase in PHP 4.

Documentation is all about returning the name of the function (method). The difference is:

__FUNCTION__ just returns the name of the method;

__METHOD__ Returns the name of the class and the name of the method.


<?php
      class Test{
        public function doit(){
          echo __FUNCTION__;
        }
        public function doitAgain(){
          echo __METHOD__;
        }
      }
      $obj = new Test();
      $obj->doit();
      echo '<br>';
      $obj->doitAgain();
?>

The output is:


doit
Test::doitAgain

I hope this article is helpful to everyone's PHP programming.


Related articles: