Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP issue with get_class

I am working on a Zend project and it has been well over 12 months since I touched Zend, I am getting an error on one of my functions, and I cannot work out why, I think it may be down to the site being originally built in an earlier version of PHP (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 is as follows,

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

Any advice on the issue would be good.

like image 860
sea_1987 Avatar asked Apr 14 '26 13:04

sea_1987


2 Answers

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

It might help to check if $mixedObject is an object first:

$debugObject->className = is_object($mixedObject) ? get_class($mixedObject) : '';
like image 157
Shiki Avatar answered Apr 16 '26 04:04

Shiki


Have you already checked if "$mixedObject" is really an object? Because the error exactly says that it is not.

You could put a 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, the get_class returns a string so your check on that line would always be "true" (or false because you are negating it) and then empty string would be set. Try it like the example above.

like image 25
enricog Avatar answered Apr 16 '26 04:04

enricog