PHP variable function

墨辰丷
Release: 2023-03-26 12:38:02
Original
1361 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 who need it can refer to it

Detailed explanation of examples of variable functions in php

The variable functions of php, today I have a general understanding, I read the summary in the php manual, I think it is not very useful;

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

Related recommendations:

Sharing examples of variable functions and anonymous functions in PHP

php variable function analysis

php variable function example detailed explanation

The above is the detailed content of PHP variable function. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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