开发者

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/ap开发者_如何学Pythonp/lib/View.php on line 449

Any advice on the issue would be good.


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) : '';


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.


It's expecting an object passed but you are setting it as an empty string and passing it after the question mark.


It looks like you pass as $mixedObject an array.

Check this var with is_object and then use gettype (if false) or get_class (if true).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜