Summary of php Variable Function Usage

  • 2021-10-16 01:19:07
  • OfStack

Variable 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. Variable functions can be used to achieve a number of purposes including callback functions and function tables.

Variable functions cannot be used for language constructs 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 pseudo code 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 one parameter of $isPage. The original intention is that passing get is to get all the data, and paginate is paging. After writing, I feel something wrong. In our usual way of writing, look up all the data $this->model->orderBy($order, $sort)->get(); Well, I haven't seen get () replaced by variables either. In actual operation, the program executes normally. Then ask about this writing in the forum. Next, we will 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 can make sense. $isPage is replaced by get while the program is running, and there is a parenthesis after $isPage, then the program will look for the function with the same name. And then 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'); //  Execute  bar(); Output from the command line: In bar(); argument was 'test'.<br />

Variable function syntax to call the method of 1 object.


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

When calling static methods, function calls take precedence over static properties. Examples of Variable methods and static properties.


<?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


Related articles: