Home > Backend Development > PHP Tutorial > Detailed explanation of how to use variable functions in PHP

Detailed explanation of how to use variable functions in PHP

黄舟
Release: 2023-03-16 06:38:02
Original
1626 people have browsed it

This article mainly introduces relevant information about the detailed examples of variable functions in PHP. I hope this article can help everyone understand and master variable functions. Friends in need can refer to it

Detailed explanation of examples of variable functions in php





##PHP supports the concept of variadic functions. This means that if there are parentheses after a variable name, PHP will look for a function with the same name as the variable's value and try to execute it. Variable functions can be used to implement some purposes including callback functions and function tables. #########Variable functions cannot be used for echo, print, unset(), isset(), empty(), include, require and similar language constructs. Your own wrapper function is required to use these structures as variadic functions. ###############
class 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()
 
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.
Copy after login

The above is the detailed content of Detailed explanation of how to use variable functions in PHP. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template