What is the use of a variable function in PHP? - php

I understand how to implement a variable function though i don't understand it's use. Why call a function using a variable than to call the function itself?
Unless to dynamically call functions from user input or returned database results?

EXAMPLE : if you have an input like /?do=something
require_once('do.php');
$fun = 'do_'.$_GET['do'];
if (function_exists($fun)) {
$fun(); //variable function
} else {
not_found();
}
so in this case I just add a function to my do.php file and it will be ready to use
do.php :
<?php
function do_getkey() {
// do something when do=getkey
}
function do_sendkey() {
// do something when do=sendkey
}
function not_found() {
// when not found
}
?>

Related

what is efficient way to access the data inside the function in my case?

I have a php page on which I get some data like this
$data = $_GET["data"];
$data is used on the page and inside a function i.e.
function myfunction() {
some code here
}
this function is called many times on this page if needed
my question is what is efficient way to access the data inside the function?
1.
function myfunction() {
$data = $_GET["data"];
}
or
2.
function myfunction() {
global $data;
}
most efficient way it to use $_GET["data"] directly where needed without declaring and assigning it to a new variable
e.g
//do your isset() checks here, not within the function
function myfunction(){
if($_GET['data']==="whatever"){ //note that === checks is faster than ==, if you are sure of the type being checked
//do something
}
}

ajax response inside a function by ob clean

I'm looking for a way to calling a function by a function and get it's content executed in the function like:
function response($execute){
ob_clean();
$execute();
die();
}
so when i call, i want to give it a process as argument, like:
response(echo("hi"));
PHP using anonymous function.
PHP code demo
function response($execute)
{
if(is_callable($execute))
{
$execute("some-value");
}
else
{
echo "Not a function";
}
}
response(function($someVariable){
echo "Hi i am in anonymous function with 1st argument ".$someVariable;
});
response("Hi");

Why function is undefined in ajax file - does function order matter in AJAX file?

I am really very surprised to see my function comes out to be undefined on AJAX request. I am wondering does function order matters in AJAX file? Following code will show you my problem -
This is the ajax.php file which is called by jquery/ajax request from index.php. It is supposed to simply print the name -
<?php
if(isset($_POST))
{
$name = 'admin';
echo display_name($name);
function display_name($name)
{
return $name;
}
}
?>
But when this file is called, i get -
Fatal error: Call to undefined function display_name()
But when i change the order of code i.e. function like this -
<?php
if(isset($_POST))
{
$name = 'admin';
function display_name($name)
{
return $name;
}
echo display_name($name);
}
?>
then it displays -
admin
How strange it is!
Thus if really function availability order matters then how the following code works -
It is simple file and it is NOT called by AJAX request. I am simply loading the file and doesn't matter where the function is written. It is working by all using any order of line code -
<?php
$name = 'admin';
echo display_name($name);
function display_name($name)
{
return $name;
}
?>
The following snippet is also working -
<?php
$name = 'admin';
function display_name($name)
{
return $name;
}
echo display_name($name);
?>
So please tell my the reason behind this difference. In page loading code works and on ajax request it doesn't. Why the function display_name() is undefined if it exists?
This has nothing to do with Ajax.
From the PHP manual:
Functions need not be defined before they are referenced, except when a function is conditionally defined
The order matters in your first example because the function is inside an if statement.
I think this issue is not related to calling file as Ajax request. This is more related to PHP's function declaration scope.
Consider the following situation:
if(true)
{
function bar()
{
}
$functions = get_defined_functions();
print_r($functions["user"]);
}
function foo()
{
}
this code will give bar and foo as defined functions.
But the following situation produces just foo function:
if(true)
{
$functions = get_defined_functions();
print_r($functions["user"]);
function bar()
{
}
}
function foo()
{
}
From this we see that all the function in a file are defined and available imediately when the file is loaded, but if blocks are interpreted as the execution passes step by step.
Hope this helps.

Having problems on fetching variables on a class in PHP

I just want to ask if its possible to call variables on class to another page of the site. I have tried calling the function's name and inside the parenthesis. I included the variable found inside that function e.g:
<?php
$loadconv -> loadmsg($msgReturn);
echo $loadconv;
?>
But it didn't work.
Do you want something like this?
class Load
{
public $msgReturn;
__construct()
{
}
public function loadMsg($param)
{
$this->msgReturn = $param;
}
}
Then you could do
$loadConv = new Load();
$loadConv->loadMsg('just a string');
echo $loadConv->msgReturn; // 'just a string'

Copy a function on the fly PHP

I'm working on a project which requires a function to be copied & executed on the fly and variables in it needs to be replaced on the fly too.
A simple example will be like this:
function myfunction()
{
$abc = $_SESSION['abc'];
return $abc;
}
I want to be able to call myfunction1() which does NOT physically exist in the code but does exactly the samething as the one above except it now take values from my custom variable so it'll look like this:
function myfunction1()
{
$abc = $myCustomVariable;
return $abc;
}
Any one help pls?
The more you describe how convoluted your function is, the more it sounds like a perfect candidate for an object with injected dependencies.
For instance, you could have (just going to describe the basic interfaces here):
class myClass
{
public function __construct($provider DataProvider)
{
$this->provider = $provider;
}
// Please name this something better
public function doStufferer()
{
if ($this->provider->hasParam('foo'))
{
return $this->provider->getParam('foo');
}
}
}
class SessionProvider implements DataProvider
{
// Session specific stuff
}
class OtherProvider implements DataProvider
{
// Other provider stuff
}
interface DataProvider
{
public function getParam($key);
public function hasParam($key);
public function setParam($key, $value);
}
You can then use it like this:
$dataProcessor = new myClass(new SessionProvider);
// OR $dataProcessor = new myClass(new OtherProvider);
$dataProcessor->doStufferer();
Please take a look at PHP Classes and Objects and the other related topics.
This is what parameters are for, I think your looking todo something like this:
$myCustomVariable = 'Some value';
function myfunction($var=$_SESSION['abc'])
{
$abc = $var;
return $abc;
}
myfunction(); //returns $_SESSION['abc']
myfunction($myCustomVariable); //returns "Some Value"
The direct answer is eval which I do not recommend.
You could have your function accept a parameter, like this.
function myfunction1($some_var)
{
$abc = $some_var;
return $abc;
}
// call it like...
myfunction1($myCustomVariable);
If you need to access a variable, but the name is generated by dynamic code, you can use $GLOBALS.
function myfunction1($name_of_var)
{
$abc = $GLOBALS[$name_of_var];
return $abc;
}
// call it like...
$myCustomVariable = 'a value'
myfunction1('myCustom' + 'Variable');

Categories