$functionName() or call_user_func($functionName)
If you need to provide parameters stored in another variable (in the form of array), use array unpacking operator:
$function_name = 'trim';
$parameters = ['aaabbb','b'];
echo $function_name(...$parameters); // aaa
To dynamically create an object and call its method use
$class = 'DateTime';
$method = 'format';
echo (new $class)->$method('d-m-Y');
or to call a static method
$class = 'DateTime';
$static = 'createFromFormat';
$date = $class::$static('d-m-Y', '17-08-2023');
Answer from knittl on Stack OverflowVideos
$functionName() or call_user_func($functionName)
If you need to provide parameters stored in another variable (in the form of array), use array unpacking operator:
$function_name = 'trim';
$parameters = ['aaabbb','b'];
echo $function_name(...$parameters); // aaa
To dynamically create an object and call its method use
$class = 'DateTime';
$method = 'format';
echo (new $class)->$method('d-m-Y');
or to call a static method
$class = 'DateTime';
$static = 'createFromFormat';
$date = $class::$static('d-m-Y', '17-08-2023');
My favorite version is the inline version:
${"variableName"} = 12;
$className->{"propertyName"};
$className->{"methodName"}();
StaticClass::${"propertyName"};
StaticClass::{"methodName"}();
You can place variables or expressions inside the brackets too!
Yes, you can do ajax request to server with your data in request parameters, like this (very simple):
Note that the following code uses jQuery
jQuery.ajax({
type: "POST",
url: 'your_functions_address.php',
dataType: 'json',
data: {functionname: 'add', arguments: [1, 2]},
success: function (obj, textstatus) {
if( !('error' in obj) ) {
yourVariable = obj.result;
}
else {
console.log(obj.error);
}
}
});
and your_functions_address.php like this:
<?php
header('Content-Type: application/json');
$aResult = array();
if( !isset($_POST['functionname']) ) { $aResult['error'] = 'No function name!'; }
if( !isset($_POST['arguments']) ) { $aResult['error'] = 'No function arguments!'; }
if( !isset($aResult['error']) ) {
switch($_POST['functionname']) {
case 'add':
if( !is_array($_POST['arguments']) || (count($_POST['arguments']) < 2) ) {
$aResult['error'] = 'Error in arguments!';
}
else {
$aResult['result'] = add(floatval($_POST['arguments'][0]), floatval($_POST['arguments'][1]));
}
break;
default:
$aResult['error'] = 'Not found function '.$_POST['functionname'].'!';
break;
}
}
echo json_encode($aResult);
?>
Try This
<script>
var phpadd= <?php echo add(1,2);?> //call the php add function
var phpmult= <?php echo mult(1,2);?> //call the php mult function
var phpdivide= <?php echo divide(1,2);?> //call the php divide function
</script>
If you are using a class, then you can use $this for calling the function:
class Test {
public function say($a) {
return $a ;
}
public function tell() {
$c = "Hello World" ;
$a = $this->say($c) ;
return $a ;
}
}
$b= new Test() ;
echo $b->tell() ;
If you are using a normal function, then use closure:
function tell(){
$a = "Hello" ;
return function($b) use ($a){
return $a." ".$b ;
} ;
}
$s = tell() ;
echo $s("World") ;
try this:
class test
{
public function hookActionValidateOrder($params)
{
$invoice = new Address((int)$order->id_address_invoice);
$myStreet = $invoice->address1;
$myCity = $invoice->city;
$myPostcode = $invoice->postcode;
// ... SOME IRRELEVANT CODE HERE ...
$Tid = send($myStreet, $myCity, $myPostcode); /* Calling function send($a, $b, $c) */
}
}
class test1 extends test
{
public function send($a, $b, $c) /* function send($a, $b, $c) */
{
// ... CODE TO DO SOMETHING USING VARIABLES $a, $b, $c ...
}
}
You can solve this with extends one class to another class .
The simplest way is:
echo debug_backtrace()[1]['function'];
As noted in the comments below, this can be further optimized by passing arguments to:
- omit both the
objectandargsindices - limit the number of stack frames returned
echo debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS,2)[1]['function'];
The debug_backtrace() function is the only way to know this, if you're lazy it's one more reason you should code the GetCallingMethodName() yourself. Fight the laziness! :D
Define the function above your return value, otherwise it never gets executed.
<?php
function foo($i){
function bar($i){
return $i*4;
}
return bar($i)*4;
}
echo foo(4);
?>
It doesn't work as you are calling bar() before it has been created. See example 2 here:- http://www.php.net/manual/en/functions.user-defined.php