Reputation: 6292
I am passing $row
which has been assigned $statement->fetchAll();
to it I am passing it to another class and I want to check if it's an stdClass Object within the method, for example if I wanted to check if it was an array I would do
public function hello(array $row)
how would I do it to check if it was a stdClass Object ?
Finally is this called type hinting ?
Upvotes: 12
Views: 18420
Reputation: 1581
Here is a little script that can help you to check if a variable is an object or if is a specific type of object, if not want to cast parameter function to desired type of object.
<?php
function text($a){
if(is_object($a)){
echo 'is object';
}else{
echo 'is not object';
}
//or if you want to be more accurate
if($a instanceof stdClass){
echo 'is object type stdClass';
}else{
echo 'is not object of type stdClass';
}
}
$b = new stdClass();
text($b);
Upvotes: 30
Reputation: 19889
Whenever you're wanting to explicitly state what kind of object a function receives as a parameter:
If you're expecting a Person object:
public function talkTo(Person $person){
}
If you're expecting a stdClass object:
public function printOut(stdClass $row){
}
Need a Database object?
public function save(Database $db){
}
Upvotes: 0
Reputation: 2606
Well solution look like easy one.
public function hello(stdClass $row)
I checked with php version 5.3 it works. like passing a stdclass object works but all other types gave cacheable fatal error.
Upvotes: 5