This question already has answers here:
Is there a call_user_func() equivalent to create a new class instance?
(2 answers)
Closed 8 years ago.
I would like something like this but dynamically from an array like this:
$array = array("first","second","third");
So class would be called like this:
$class = new class("first","second","third");
You can use reflection for it:
$refClass = new ReflectionClass('SomeClass');
$instance = $refClass->newInstanceArgs($args);
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 pass a variable inside a function?
(1 answer)
How to access array element inside another array [duplicate]
(1 answer)
Closed 5 years ago.
I have an API's Function :
$transaction=$tran[4];
function coinpayments_api_call($cmd, $req = array(),$transaction) {
curl_init($transaction);
}
$transaction variable not passing into function coinpayments_api_call.
function not taking values from out side.
I also make $transaction varible GLOBAL ,but still same problem ,
Please Help
Make your code like this
$transaction=$tran[4];
function coinpayments_api_call($transaction,$cmd, $req = array(),$txnid) {
curl_init($transaction);
}
This question already has answers here:
How to concatenate PHP variable name?
(4 answers)
Closed 6 years ago.
I have an object and can access one of its properties by
$book = new Book(); print $book->price;
I can also call
$prop = "price"; print $book->$prop.
But I cannot figure out how to call in a mixed way, it gives error:
$book->id_$prod
as a shortened of $book->id_products, being $prod = "products".
$book = new Book();
$id_prod = "id_$prod";
print $book->$id_prod;
Mix the 2 first options.
$prop = 'id_'.$prod;
$book->$prop;
This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 7 years ago.
many times I've seen this symbol ->
What is it? seems a logical operator or something
Complete example:
get_categories->category_count
It's called the object operator.
For example:
$myObject = new stdClass();
$myObject->Hello = "world";
echo $myObject->Hello; //Returns world
You can also use this operator to call properties or methods from classes. For example:
Class MyClass {
public function hello(){
return "world";
}
}
echo (new MyClass())->hello(); //Returns world
It is called as PHP object operator (T_OBJECT_OPERATOR).
Example:
$car = new Car();
$car->color = 'Red';
echo $car->color; // returns the color of the car.
This question already has answers here:
Only variables should be passed by reference
(12 answers)
Closed 7 years ago.
I have the following code :
$_SESSION['aParties'] = time();
$aParties[] = $_SESSION['aParties'];
error_log(print_r($aParties,true), 3, "/var/tmp/error.log");
$first = reset(ksort($aParties));
The array aParties is like this :
Array
(
[0] => 1433841062
)
But I get the error :
'Only variables should be passed by reference in the method ksort'
Help me please! Thx in advance
You need to do it as
ksort($aParties);
$first = reset($aParties);
Note: There is no reference sign on a function call - only on function definitions. Function definitions alone are enough to correctly pass the argument by reference.
Check Docs