I know difference between these two, and I've checked the manual.
I'm stll have some confusion. I can use __FUNCTION__
in method of class, this represents the name of this method. When I echo it, it just output the name. Here is clear.
But Why I can use __METHOD__
in a non-calss method. This also just represents the name of the normal funciton. Is the normal function is in some some container? and when echo __METHOD__
in the normal function, the container is nothing?
Code:
<?php
class dog {
private $name = 'dog';
public static function name() {
echo __METHOD__;
echo"\n";
echo __FUNCTION__;
echo"\n";
}
}
function test() {
echo __FUNCTION__;
echo"\n";
echo __METHOD__;
}
dog::name();
test();
output:
ei@localhost:~$ php test.php
dog::name
name
test
test
Any help will appreciated. Thanks.
There really is no technical difference within php. But in my mind, a function is a more mathematical thing; it doesn't change state, only returns a value (like f(x) = 2x). A method modifies state (like outputing "hello world") and may return a value. This is basically a dupe of: stackoverflow.com/questions/43777/…
The __FILE__ constant returns full path and name of the PHP file that's being executed. If used inside an include, the name of the included file is returned.
Magic constants are the predefined constants in PHP which get changed on the basis of their use. They start with double underscore (__) and ends with double underscore. They are similar to other predefined constants but as they change their values with the context, they are called magic constants.
"Method" is basically just the name for a function within a class (or class function). Therefore __METHOD__
consists of the class name and the function name called (dog::name
), while __FUNCTION__
only gives you the name of the function without any reference to the class it might be in.
When __METHOD__
is called outside of a class it's the same as __FUNCTION__
because there is no class part to be used as a prefix. You can use __METHOD__
outside of a class because it's a magic constant and they're always available (at worst case they will return empty string).
http://php.net/manual/en/language.constants.predefined.php
Let me clarify your doubt..
First I will explain about CLASS & OBJECT(what is the difference..)
Then I will clarify about FUNCTION & METHOD(what is the difference..)
talking about Class :: It is a your logical implementation..
object :: It is a instance of your class (instance ~ COPY ~ clone ~ ...)
when you are using --NEW-- operator ---> you are creating an OBJECT (nothing but the COPY) OF YOUR CLASS.
CLASS =============> ~NEW~ =======> OBJECT (New Copy of your class..) similarly
FUNCTION ==========> ~NEW~ =======> METHOD (Belongs to Object not Class)
Class is the program you are writing while object is the copy of your class that is executing..
class resides in ROM(permanent) where as object resides in RAM(temporary memory area..)
& when you are declaring an function as static it belongs only to class not to object & you can call the same(static function) by using ClassName::staticFunctionName()
That is why there is no existence of STATIC_METHOD Rather STATIC_FUNCTION !!!
code example::
<?php
class Dog extends CI_Controller {
public function __construct() {
parent::__construct();
}
public function index($params = '') {
}
private $name = 'dog';
public static function name() {
echo 'Inside static function & inside class' . '<br>';
echo __METHOD__ . '<br>';
echo __FUNCTION__ . '<br>';
}
public function test2() {
echo 'Inside NON STATIC function & inside class' . '<br>';
echo __FUNCTION__ . '<br>';
echo __METHOD__ . '<br>';
}
}
function test() {
echo 'Inside NON STATIC function & OUTSIDE class' . '<br>';
echo __FUNCTION__ . '<br>';
echo __METHOD__ . '<br>';
}
Dog::name();
test();
$somevar = new Dog;
$somevar->test2();
OUTPUT::
Inside static function & inside class
Dog::name //Class::function
name
Inside NON STATIC function & OUTSIDE class
test
test
Inside NON STATIC function & inside class
test2
Dog::test2 //Object::method
IN Your case as you are using METHOD IN static context it showing the class name if the it would be a non static(Simple Function) it would show only function name only..
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With