How to make function from string? I am new to php programming and wanted solution for below code.
If I want to access page for test2.php?page=test for below class:
class test{
public static function getTest(){
//code here....
};
}
and wanted to access test2.php?page=test using variable so what should I do?
require_once "test.php";
$variabe = $_GET['page'];
test::get . ucfirst($variable) . ();
You can do it like this:
<?php
class test{
public static function getTest(){
echo 'getTest()!!';
}
}
$variable = 'Test';
// since we are interpolating a raw string, input from $_GET, and a function we need to let PHP know to fully complete this string before using it as a function call
test::{"get".ucfirst($variable)}();
// Build the full method name ahead of time and you can just call it using the variable
$variable2 = 'getTest';
test::$variable2();
Output:
getTest()!!