Function Name In A Method

<?php

class X {
    function foo() {
        print __METHOD__.PHP_EOL;
        print __CLASS__.PHP_EOL;
        print __FUNCTION__.PHP_EOL;
    }
}

(new x)->foo();

    function foo() {
        print __METHOD__.PHP_EOL;
        print __CLASS__.PHP_EOL;
        print __FUNCTION__.PHP_EOL;
    }

foo();

The magic constants give information on the context of execution of the code. __METHOD__ gives the name of the method, and its related class. __CLASS__ gives the name of the current class, whatever the method. And __FUNCTION__ gives the name of the current function, or, also, the name of the method, though without the name of the class.

Basically, __METHOD__ === __CLASS__ .'::'. __FUNCTION__.

Also, just for fun, you can also use __METHOD__ in a function, and you’ll get the same as __FUNCTION__. The opposite, as we can see, is not true.

See Also

PHP Features

Last updated: 14 July 2026