PHP/function variable

from HTYP, the free directory anyone can edit if they can prove to me that they're not a spambot
< PHP
Revision as of 16:49, 10 April 2022 by Woozle (talk | contribs) (Created page with "==About== A variable can store a reference to a function. The thing that isn't explained very well in the documentation is that this just consists of naming the function in a...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

About

A variable can store a reference to a function. The thing that isn't explained very well in the documentation is that this just consists of naming the function in a callable way.

function Example1() { echo "Example1 was called!\n"; }

#$fEx1 = Example1;  // does not work; sees "Example1" as a constant
$fEx1 = 'Example1';

$fEx1();  // prints "Example1 was called!"
class cExample2 {
    static public function Test1() { echo "Test1 was called!\n"; }
}

#$fEx2 = cExample2::Test1;  // does not work; sees "Test1" as a class constant
$fEx2 = 'cExample2::Test1';
$fEx2(); // prints "Test1 was called!"

Documentation