Summary: In this tutorial, you will learn about PHP variable functions and how to use them to call a function, an object’s method, and a class’s static method.
Introduction to PHP variable functions #
Variable functions allow you to use a variable like a function. When you append parentheses () to a variable, PHP will look for the function whose name is the same as the variable’s value and execute it. For example:
<?php
$f = 'strlen';
echo $f('Hello');Code language: PHP (php)Output:
5Code language: PHP (php)How it works.
- First, define a variable
$fand initialize its value to the'strlen'literal string. - Second, use the
$fas a function by passing the string'Hello'to it.
When PHP sees $f(), it looks for the strlen() function. Because the strlen() is a built-in function, PHP just invokes it.
If PHP cannot find the function name, it’ll raise an error. For example:
<?php
$f = 'len';
echo $f('Hello');Code language: PHP (php)Error:
Fatal error: Uncaught Error: Call to undefined function len() in index.php:5Code language: plaintext (plaintext)In this example, it issues an error because PHP cannot find the len() function.
Using variable functions to call a method #
The variable functions allow you to call the methods of an object. The syntax for calling a method using a variable function is as follows:
$this->$variable($arguments)Code language: PHP (php)Notice that you need to prefix the variable name with the $ sign. In this case, you’ll have the $ sign before the this keyword and the variable name. For example:
<?php
class Str
{
private $s;
public function __construct(string $s)
{
$this->s = $s;
}
public function lower()
{
return mb_strtolower($this->s, 'UTF-8');
}
public function upper()
{
return mb_strtoupper($this->s, 'UTF-8');
}
public function title()
{
return mb_convert_case($this->s, MB_CASE_TITLE, 'UTF-8');
}
public function convert(string $format)
{
if (!in_array($format, ['lower', 'upper', 'title'])) {
throw new Exception('The format is not supported.');
}
return $this->$format();
}
}Code language: PHP (php)How it works:
- First, define a
Strclass that has three methods for converting a string to lowercase, uppercase, and title case. - Second, define the
convert()method that accepts a string. If theformatargument is not one of the method names: lower, upper, and title, theconvert()method will raise an exception. Otherwise, it’ll call the corresponding methodlower(),upper()ortitle().
The following shows how to use the convert() method of the Str class:
<?php
require_once 'Str.php';
$str = new Str('Hello there');
echo $str->convert('title');Code language: PHP (php)Output:
Hello ThereCode language: PHP (php)2) Using variable functions to call a static method example #
The following example uses a variable function to call a static method:
<?php
class Str
{
private $s;
public function __construct(string $s)
{
$this->s = $s;
}
public function __toString()
{
return $this->s;
}
public static function compare(Str $s1, Str $s2)
{
return strcmp($s1, $s2);
}
}Code language: PHP (php)The Str class has a constructor that accepts a string. It implements the toString() method that converts the Str instance to a string.
The Str class has the compare() static method that compares two instances of the Str class. To call the compare() static method using a variable function, you use the following:
$str1 = new Str('Hi');
$str2 = new Str('Hi');
$action = 'compare';
echo Str::$action($str1, $str2); // 0Code language: PHP (php)Summary #
- Append parentheses () to a variable name to call the function whose name is the same as the variable’s value.
- Use the
$this->$variable()to call a method of a class. - Use the
className::$variable()to call a static method of a class.