PHP对象数组::不能使用<myobject>类型的对象作为数组</myobject>

时间:2014-11-28 23:30:29

标签: php mysql arrays

我有一个类代表数据库中的一个表 我希望将表填充到以下对象数组中:

$subCat = array();
$count=0;
while($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
    $subCatName = $line["sub_cat_name"];
    $subCatShortDescription = $line["short_description"];
    $subCatLongDescription = $line["long_description"];
    $subCat = new SubCat($countryId, $catName, $subCatShortDescription, $subCatLongDescription);
    $subCat[$count++] = $subCat;
}

我收到以下错误:

Fatal error: Cannot use object of type SubCat as array in C:\AppServ\www\MyWebSite\classes\SubCat.php on line 34

由于

1 个答案:

答案 0 :(得分:2)

您将对象用作数组:

$subCat = array():
// ... code
$subCat = new SubCat($countryId, $catName, $subCatShortDescription, $subCatLongDescription);
$subCat[$count++] = $subCat;

当您将新对象分配到$subCat时,它不再是数组,因此$subCat[$index]

而是使用类似的东西:

$subCat = array();
// ... code
$subCat[$count++] = new SubCat($countryId, $catName, $subCatShortDescription, $subCatLongDescription);