SoFunction
Updated on 2025-03-01

Summary of the use of php mutable functions

Variable functions

PHP supports the concept of variable functions. This means that if a variable name is followed by parentheses, PHP will look for a function with the same name as the value of the variable and try to execute it. Variable functions can be used to implement some uses including callback functions and function tables.

Variable functions cannot be used in language structures, such as echo(), print(), unset(), isset(), empty(), include(), require() and similar statements. You need to use your own wrapper function to use these structures as variable functions.

Write my pseudocode first.

 protected $model;
 public function __construct(Category $category)
 {
  $this->model = $category;
 }
 public function getLists($request, $isPage = 'get', $order = 'created_at', $sort = 'desc')
 {
  return $this->model->orderBy($order, $sort)->$isPage();
 }

In getLists, there is a $isPage parameter. The original meaning is to pass in get to get all the data, and paginate is paginate. After writing, I think something is wrong. In our usual writing style, find all data$this->model->orderBy($order, $sort)->get(); That's it, I've never seen variables replace get() either. In actual operation, the program executes normally. Then I asked about this writing method in the forum. Next, we need to introduce a concept, "Variable Function".

What is a mutable function?

PHP supports the concept of variable functions. This means that if a variable name is followed by parentheses, PHP will look for a function with the same name as the value of the variable and try to execute it.

After understanding this concept, the above program will be able to make sense. $isPage is replaced by get when the program is running, and there is a parentheses after $isPage, and the program will look for a function with the same name. Continue to execute.

Example:

<?php
function foo() {
 echo "In foo()<br />\n";
}
function bar($arg = '') {
 echo "In bar(); argument was '$arg'.<br />\n";
}
$func = 'foo';
$func();  // Execute foo(); Output from the command line: In foo()<br />$func = 'bar';
$func('test'); // implement bar();Output from the command line:In bar(); argument was 'test'.&lt;br /&gt;

The syntax of a mutable function to call an object's method.

&lt;?phpclass Foo
{
 function Variable()
 {
  $name = 'Bar';
  $this-&gt;$name(); // This calls the Bar() method
 }
 function Bar()
 {
  echo "This is Bar";
 }
}
$foo = new Foo();
$funcname = "Variable";
$foo-&gt;$funcname(); // This calls $foo-&gt;Variable()
// Command line execution output: This is Bar

When calling a static method, function calls take precedence over static properties. Variable method and static property examples.

<?php
class Foo
{
 static $variable = 'static property';
 static function Variable()
 {
  echo 'Method Variable called';
 }
}
echo Foo::$variable; // This prints 'static property'. It does need a $variable in this scope.
$variable = "Variable";
Foo::$variable(); // This calls $foo->Variable() reading $variable in this scope.

Summarize

The above is the summary of the use of php variadic functions introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support for my website!