This question already has answers here:
Check if a function exists in a class before calling call_user_func()
(4 answers)
Closed 6 years ago.
I receive a response from API in form of array object, but sometimes i receive different data in form of object data
for example :
//first response
$response->getBody();
//second response
$response->getMessage();
so if i call one of the response above that doesnt have the object , it's gonna have an error, what i've done so far is like this:
if(empty($response->getBody())){
//do something
}
in conclusion i want to detect if the array of object has the object i want to call or use
use method_exists() to check if the method of a class exits.
<?php
$directory = new Directory('.');
var_dump(method_exists($directory,'read'));
?>
or use is_callable()
class someClass {
function someMethod() { }
}
$anObject = new someClass();
$methodVariable = array($anObject, 'someMethod');
var_dump(is_callable($methodVariable, true, $callable_name)); //
bool(true)
echo $callable_name, "\n"; // someClass::someMethod
Related
This question already has an answer here:
How to get the string name of the argument's type hint?
(1 answer)
Closed 2 years ago.
I have method which cast array to object by using
$class = get_class($object);
$methodList = get_class_methods($class);
But now I need had information about expected type of variable too. For example from this method:
public function setFoo(int $foo)
{
}
I need get int too. There is any option to get it?
You can use Reflection. Specifically ReflectionParameter::getType().
function someFunction(int $param, $param2) {}
$reflectionFunc = new ReflectionFunction('someFunction');
$reflectionParams = $reflectionFunc->getParameters();
$reflectionType1 = $reflectionParams[0]->getType();
$reflectionType2 = $reflectionParams[1]->getType();
assert($reflectionType1 instanceof ReflectionNamedType);
echo $reflectionType1->getName(), PHP_EOL;
var_dump($reflectionType2);
The above example will output:
int
NULL
This question already has answers here:
How to call a function from a string stored in a variable?
(18 answers)
Closed 1 year ago.
I found question from here. But I need to call function name with argument.
I need to be able to call a function, but the function name is stored in a variable, is this possible? e.g:
function foo ($argument)
{
//code here
}
function bar ($argument)
{
//code here
}
$functionName = "foo";
$functionName($argument);//Call here foo function with argument
// i need to call the function based on what is $functionName
Anyhelp would be appreciate.
Wow one doesn't expect such a question from a user with 4 golds. Your code already works
<?php
function foo ($argument)
{
echo $argument;
}
function bar ($argument)
{
//code here
}
$functionName = "foo";
$argument="Joke";
$functionName($argument); // works already, might as well have tried :)
?>
Output
Joke
Fiddle
Now on to a bit of theory, such functions are called Variable Functions
PHP supports the concept of variable functions. This means that if a variable name has parentheses appended to it, PHP will look for a function with the same name as whatever the variable evaluates to, and will attempt to execute it. Among other things, this can be used to implement callbacks, function tables, and so forth.
If you want to call a function dynamically with argument then you can try like this :
function foo ($argument)
{
//code here
}
call_user_func('foo', "argument"); // php library funtion
Hope it helps you.
you can use the php function call_user_func.
function foo($argument)
{
echo $argument;
}
$functionName = "foo";
$argument = "bar";
call_user_func($functionName, $argument);
if you are in a class, you can use call_user_func_array:
//pass as first parameter an array with the object, in this case the class itself ($this) and the function name
call_user_func_array(array($this, $functionName), array($argument1, $argument2));
This question already has answers here:
Dynamically create PHP object based on string
(5 answers)
Closed 8 years ago.
I have a bunch of functions depending on a variable, I want to be able to do something like this:
(It returns an error hence the problem I'm unable to solve)
function($x) {
fetch.$x.() // if x=Name I would like it to execute fetchName()...and so on
}
and something like this
function($x) {
$article = new \Cc\WebBundle\Entity\$X();
// if x='name' to be executed Cc\WebBundle\Entity\name()
}
Sure, you could do that:
$basename = "fetch";
$key = ...; // your logic for generating the rest of function's name
$functionName = $basename . $key;
$functionName(); // execute function
Now, the tricky part would be if functions contain arbitrary set of arguments. In that case you should use call_user_func_array (docs).
As for creating of objects, meagar explained here please clear how to achieve that.
P.S. This, in fact, has very little to do with Symfony2. This is a pure PHP question ;)
Personally, I use the handy call_user_func_array() function like this:
<?php
$class = 'MyClassName';
$method = 'someMethod';
$parameters = array('foo', 'bar', 'baz');
call_user_func_array(array($class, $method), $parameters);
I imagine you would need to escape back-slashes in any name-spaced class names though, i.e. MCB\\MyClassName.
This question already has answers here:
Calling a function within a Class method?
(10 answers)
Closed 9 years ago.
please help me with this example.. I want to call the function inside the class..
FUNCTION
class myTest(){
function Me(){
$x = 2;
}
}
is this the answer?
$myval = new myTest();
echo $myval;
thank you
You need to first instantiate a new object of type 'myTest' like so:
$myObj = new myTest;
And then you can use the function like so:
$myObj->Me();
Note that at present, the function returns nothing so you'll get a 'blank screen'.
If you changed your function to read:
return "Hello";
Then you would get 'Hello' printed on screen by using the above.
I hope that helps?
$myval = new myTest(); here $myval is object of the your Class .
You can call the inside function with just $myval->Me();
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Checking if an associative array key exists in Javascript
I have a PHP code block . For a purpose I am converting this to a JavaScript block.
I have PHP
if(array_key_exists($val['preferenceIDTmp'], $selected_pref_array[1]))
now I want to do this in jQuery. Is there any built in function to do this?
Note that objects (with named properties) and associative arrays are the same thing in javascript.
You can use hasOwnProperty to check if an object contains a given property:
o = new Object();
o.prop = 'exists'; // or o['prop'] = 'exists', this is equivalent
function changeO() {
o.newprop = o.prop;
delete o.prop;
}
o.hasOwnProperty('prop'); //returns true
changeO();
o.hasOwnProperty('prop'); //returns false
Alternatively, you can use:
if (prop in object)
The subtle difference is that the latter checks the prototype chain.
In Javascript....
if(nameofarray['preferenceIDTmp'] != undefined) {
// It exists
} else {
// Does not exist
}
http://phpjs.org/functions/array_key_exists:323
This is a great site for PHP programmers moving to js.