Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to check if methods parameter is a stdClass Object

Tags:

php

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 ?

like image 743
Oliver Bayes-Shelton Avatar asked Oct 04 '12 09:10

Oliver Bayes-Shelton


1 Answers

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);
like image 85
Moldovan Daniel Avatar answered Oct 19 '22 23:10

Moldovan Daniel