I have a simple question (i guess).
I'm getting the name of the function inside a variable from database. After that, i want to run the specific function. How can i echo the function's name inside the php file?
The code is something like this:
$variable= get_specific_option;
//execute function
$variable_somesuffix();
The "somesuffix" will be a simple text. I tried all the things i had in mind but nothing worked.
You want call_user_func
function hello($name="world")
{
echo "hello $name";
}
$func = "hello";
//execute function
call_user_func($func);
> hello world
call_user_func($func, "byron");
> hello byron
You want variable variables.
Here's some sample code to show you how it works, and the errors produced:
function get_specific_option() {
return 'fakeFunctionName';
}
$variable = get_specific_option();
$variable();
// Fatal error: Call to undefined function fakeFunctionName()
$test = $variable . '_somesuffix';
$test();
// Fatal error: Call to undefined function fakeFunctionName_somesuffix()
There are two ways you can do this. Assuming:
function foo_1() {
echo "foo 1\n";
}
You can use call_user_func():
$var = 'foo';
call_user_func($foo . '_1');
or do this:
$var = 'foo';
$func = $var . '_1';
$func();
Unfortunately you can't do the last one directly (ie ($var . '_1')(); is a syntax error).
You can also do sprintf($string).
Related
how to define function name (in PHP) using variable, like this?
$a='myFuncion';
function $a() {.......}
or like that?
The only way I know to give a fixed name to a function is to use eval, which I would not suggest.
More likely, what you want is to stuff a function IN a variable, and then just call that.
Try this:
$a = function() {
echo 'This is called an anonymous function.';
}
$a();
EDIT:
If you want to be accessible from other files, then use GLOBAL variable:
$GLOBALS['variable_name'] = 'my_func_123';
${$GLOBALS['variable_name']} = function() {
echo 'This is called an anonymous function.';
};
// Executing my_func_123()
${$GLOBALS['variable_name']}();
See also: http://php.net/manual/en/functions.anonymous.php
I recently made a class in PHP
I am trying to declare a variable within class and using str_replace in a function but its show undefined variable
class Status{
$words = array(".com",".net",".co.uk",".tk","co.cc");
$replace = " ";
function getRoomName($roomlink)
{
echo str_replace($words,$replace,$roomlink);
}
}
$status = new Status;
echo $status->getRoomName("http://darsekarbala.com/azadari/");
Any kind of help would be appreciated thanks you
Your variables in the function getRoomname() aren't adressed properly. Your syntax assumes the variables are either declared within the function or passed while calling the function (which they aren't).
To do this within a class, do it while using $this->, like this:
function getRoomName($roomlink)
{
echo str_replace($this->words,$this->replace,$roomlink);
}
For further informations, please have a look into this page of the manual.
Maybe because of the version or something, when I tested your exact code, I got syntax error, unexpected '$words' (T_VARIABLE), expecting function (T_FUNCTION), so setting your variables to private or public should fix this one.
About the undefined varible, you have to use $this-> to access them from your class. Take a look:
class Status{
private $words = array(".com",".net",".co.uk",".tk","co.cc"); // changed
private $replace = " "; // changed
function getRoomName($roomlink){
echo str_replace($this->words, $this->replace, $roomlink); // changed
}
}
$status = new Status;
echo $status->getRoomName("http://darsekarbala.com/azadari/");
Also, since getRoomName isn't returning anything, echoing it doesn't do much. You could just:$status->getRoomName("http://darsekarbala.com/azadari/");.
or change to :
return str_replace($this->words, $this->replace, $roomlink);
Short Version: Why can't we access a function like this:
$b = "simple_print()";
$obj->$b;
Complete Version:
Suppose we have a class User defined like this:
class User {
public $name;
function simple_print() {
echo "Just Printing" . "<br>";
}
}
Now if a create an User object and set the name of it we can print its name using
$obj = new User;
$obj->name = "John";
echo $obj->name;
Although it is strange we also can do something like this in order to print "John":
$a = "name";
echo $obj->$a;
But we can't access a function using the same idea:
$b = "simple_print()";
$obj->$b;
Why? Shouldn't it work the same way?
Also, does anyone know what is it called? I tried to look for "accessing a member through a variable" and "using a method through a variable with the name of it" but I didn't find anything related to this.
Extra info: The version of PHP I'm using is: PHP version: 5.5.9-1ubuntu4.7
You were very close, but made a small logical mistake. Try this instead:
$b = 'simple_print';
$obj->$b();
This is because the method is accessed by it's name, which is simple_print, not simple_print(). The execution is triggered by the parenthesis, but that is not part of the name, so of how you access the method.
Here is a short example:
<?php
class Test
{
public function simple_print() {
echo "Hello world!\n";
}
}
$object = new Test;
$method = 'simple_print';
$object->$method();
As expected it creates the output Hello world! if executed on CLI.
Why is PHP Saying that my object is empty ?
Here is a simple use case.
It keeps returning the following error :
Fatal error: Cannot access empty property in C:\AdvancedApp\myApp.php on line 10.
class myClass
{
public $myFunction = "Hello World";
}
$class = new myClass();
echo $class->$myFunction;
The correct use of property is:
echo $class->myFunction;
What you did is used variable variables, the following will work:
$name = "myFunction" ;
echo $class->$name ;
Drop the $ sign from $myClass->$myFunction so it will be $myClass->myFunction, and what's with the variable name. Use something else, like myValue...
I keep getting an error that says function name must be in a string or it simply runs but doesn't print either string in the function. What am I missing?
<?php
$funName="{'$oneRun()'}";
echo "Hello You are running ".$funName."\n\n$";
function oneRun()
{ echo "Running Function One";return "one";}
function twoRun()
{ echo "Leave me alone, go bug Function One";return "two";}
?>
You mean callables or variable functions?
$funName = 'oneRun'; // save only name
echo "Hello You are running " . $funName() . "\n\n$";
You are calling an echo in another echo, that couldn't work.
I guess something like :
<?php
$funName="{'$oneRun()'}";
echo "Hello You are running ";
oneRun();
echo "\n\n$";
function oneRun()
{ echo "Running Function One";return "one";}
function twoRun()
{ echo "Leave me alone, go bug Function One";return "two";}
?>
Would do the job ?
I always use:
call_user_func($string_function_or_closure, ...);
call_user_func(array($object, 'Method'), ...);
call_user_func(array(StaticClass, 'Method'), ...);
or:
call_user_func_array($string_function_or_closure, array(...));
call_user_func_array(array($object, 'Method'), array(...));
call_user_func_array(array(StaticClass, 'Method'), array(...));
for string functions. It's a bit more explicit than:
$function();
and it also makes it better visible in your IDE.