PHP Variable Functions (original) (raw)
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:
5
Code language: PHP (php)
How it works.
- First, define a variable
$f
and initialize its value to the'strlen'
literal string. - Second, use the
$f
as 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:5
Code 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)
{
<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>t</mi><mi>h</mi><mi>i</mi><mi>s</mi><mo>−</mo><mo>></mo><mi>s</mi><mo>=</mo></mrow><annotation encoding="application/x-tex">this->s = </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.7778em;vertical-align:-0.0833em;"></span><span class="mord mathnormal">t</span><span class="mord mathnormal">hi</span><span class="mord mathnormal">s</span><span class="mord">−</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">></span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:0.4306em;"></span><span class="mord mathnormal">s</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span></span></span></span>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 <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>t</mi><mi>h</mi><mi>i</mi><mi>s</mi><mo>−</mo><mo>></mo></mrow><annotation encoding="application/x-tex">this-></annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.7778em;vertical-align:-0.0833em;"></span><span class="mord mathnormal">t</span><span class="mord mathnormal">hi</span><span class="mord mathnormal">s</span><span class="mord">−</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">></span></span></span></span>format();
}
}`Code language: PHP (php)
How it works:
- First, define a
Str
class that has three methods for converting a string to lowercase, uppercase, and title case. - Second, define the
convert()
method that accepts a string. If theformat
argument 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 There
Code 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)
{
<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>t</mi><mi>h</mi><mi>i</mi><mi>s</mi><mo>−</mo><mo>></mo><mi>s</mi><mo>=</mo></mrow><annotation encoding="application/x-tex">this->s = </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.7778em;vertical-align:-0.0833em;"></span><span class="mord mathnormal">t</span><span class="mord mathnormal">hi</span><span class="mord mathnormal">s</span><span class="mord">−</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">></span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:0.4306em;"></span><span class="mord mathnormal">s</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span></span></span></span>s;
}
public function __toString()
{
return $this->s;
}
public static function compare(Str <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>s</mi><mn>1</mn><mo separator="true">,</mo><mi>S</mi><mi>t</mi><mi>r</mi></mrow><annotation encoding="application/x-tex">s1, Str </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8778em;vertical-align:-0.1944em;"></span><span class="mord mathnormal">s</span><span class="mord">1</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord mathnormal">St</span><span class="mord mathnormal" style="margin-right:0.02778em;">r</span></span></span></span>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); // 0`Code 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.
Did you find this tutorial useful?