PHP issue with get_class

I am working on a Zend project and it has been over 12 months since I touched Zend, I get an error in one of my functions and I cannot figure out why, I think it might be up to the site originally built in an earlier PHP version (5.2) and I am now running 5.3.

The function looks like this:

public function addDebug($mixedObject, $title = "")
    {
        $debugObject = new stdClass();
        $debugObject->title       = $title;   
        $debugObject->type        = gettype($mixedObject);
        $debugObject->className   = (!get_class($mixedObject)) ? "" : gettype($mixedObject);<-- Line error is complaining about -->
        $debugObject->mixedObject = $mixedObject; 
        array_push($this->debugArr, $debugObject);
    }

      

The error message looks like this:

get_class() expects parameter 1 to be object, array given in /server/app/lib/View.php on line 449

Any advice on this would be good.

+2


a source to share


4 answers


Get_class function requires the parameter to be an object. The error says $ mixedObject is an array.

This can help to check if $ mixedObject is the first object:



$debugObject->className = is_object($mixedObject) ? get_class($mixedObject) : '';

      

+2


a source


Have you already checked if the $ mixedObject is actually an object? Because the error says for sure that it is not.

You can check if the given $ mixedObject is an object or not:



if (is_object($mixedObject)) { 
    $debugObject->className   = get_class($mixedObject);
} else {
    $debugObject->className   = gettype($mixedObject);
}

      

Edit: I also see some other error, get_class returns a string, so your check on that string will always be "true" (or false because you deny it) and then an empty string will be set. Try it like in the example above.

+2


a source


It expects an object to pass, but you set it to an empty string and pass it after the question mark.

+1


a source


It looks like you are passing like an $mixedObject

array.

Check this var with is_object

and then use gettype

(if false) or get_class

(if true).

0


a source







All Articles