This question already has answers here:
How to Check for a Specific Type of Object in PHP
(4 answers)
Closed 5 years ago.
how do I know what kind of class is a variable in php:
if($var == \Collection)
echo "congratulations";
?>
Try instanceof:
if($var instanceof \Collection) {
You can use get_class($object):
Returns the name of the class of an object
or use instanceof in if:
if ($object instanceof Collection) {
...
}
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:
Unsetting array values in a foreach loop [duplicate]
(9 answers)
Closed 9 years ago.
I think the code is obvious:
foreach ($programs as $program) {
if ($program->name == 'foo') {
unset($program);
}
}
But it's not working! Isn't it possible to unset current property? Where's the problem? Is there any alternatives?
foreach ($programs as $property => $program) {
// ^-----------^ added
if ($program->name == 'foo') {
unset($programs->$property);
// ^---------^ added
}
}
This question already has answers here:
How Can I get a Session Id or username in php?
(3 answers)
Closed 9 years ago.
I'm trying to get the member username from the session and can't find a real way
This is the code I reached:
<?php
session_start();
if( $session_name['member_username']="admin")
{//do something
} else { do smth}
Thank you
Use double equal to (==) for comparison inside if
<?php
session_start();
if( $_SESSION['member_username']=="admin")
{//do something
} else { do smth}
?>
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);