警告:get_class()期望参数1是对象

时间:2013-04-30 05:42:54

标签: php arrays

我在一周前写了一个代码并且工作正常。但今天当我嘲笑时,它给了我一些像

这样的问题
Warning: get_class() expects parameter 1 to be object, array given in /home/ccc/public_html/horoscope/xml2json.php on line 182

Warning: get_class() expects parameter 1 to be object, string given in /home/ccc/public_html/horoscope/xml2json.php on line 182

Warning: get_class() expects parameter 1 to be object, string given in /home/ccc/public_html/horoscope/xml2json.php on line 182

Warning: get_class() expects parameter 1 to be object, string given in /home/ccc/public_html/horoscope/xml2json.php on line 182

Warning: get_class() expects parameter 1 to be object, string given in /home/ccc/public_html/horoscope/xml2json.php on line 182

我的代码部分是

$currentDate = date("n/j/Y"); 
 echo($hdate);
require_once("xml2json.php");

$testXmlFile = "http://www.findyourfate.com/rss/horoscope-astrology-feed.asp?mode=view&todate=$currentDate";
echo($testXmlFile);
$xmlStringContents = file_get_contents($testXmlFile); 
$jsonContents = "";
$jsonContents = xml2json::transformXmlStringToJson($xmlStringContents);
$obj =json_decode($jsonContents);
$rows = array();
foreach($obj->rss->channel->item as $item) 

xml2json中的182行是

if (get_class($simpleXmlElementObject) == SIMPLE_XML_ELEMENT_PHP_CLASS) {
    // Get a copy of the simpleXmlElementObject
    $copyOfsimpleXmlElementObject = $simpleXmlElementObject;
    // Get the object variables in the SimpleXmlElement object for us to iterate.
    $simpleXmlElementObject = get_object_vars($simpleXmlElementObject);
}       

这是simpleXMLElement对象

的var_dump的pastebin链接

http://pastebin.com/MPQfdQVx

有人可以帮我解决突然发生的事情吗?

由于

2 个答案:

答案 0 :(得分:5)

只需添加is_object检查

if (is_object($simpleXmlElementObject) && get_class($simpleXmlElementObject) == SIMPLE_XML_ELEMENT_PHP_CLASS) {

答案 1 :(得分:0)

我不会使用,因为你试图比较常数&只使用double equals(==)而不是tripple(===)

if (get_class($simpleXmlElementObject) == SIMPLE_XML_ELEMENT_PHP_CLASS) {

但相反,这就像......

if ($simpleXmlElementObject instanceof SIMPLE_XML_ELEMENT_PHP_CLASS) {

get_class 期望param成为一个对象&返回字符串PHP get_class