[英]Fatal error: Uncaught Error: Cannot use object of type Entrada as array
Who help me with this problem? 谁来帮助我解决这个问题?
56 public static function mostrar_entradas_busqueda($entradas) {
57 $count = count($entradas);
58 for ($i = 1; $i <= $count; $i++) {
59 if ($i % 3 == 1) {
60 ?>
61 <div class="row">
62 <?php
63 }
64 $entradas = $entradas[$i - 1];
65 self::mostrar_entrada_busqueda($entradas);
66
67 if ($i % 3 == 0) {
68 ?>
69 </div>
70 <?php
71 }
72 }
73 if ($i % 3 !== 0) {
74 ?>
75 </div>
76 <?php
77 }
78 }
Fatal error: Uncaught Error: Cannot use object of type Entrada as array in C:\\xampp\\htdocs\\blog\\app\\EscritorEntradas.inc.php:64 Stack trace: #0 C:\\xampp\\htdocs\\blog\\vistas\\buscar.php(81): EscritorEntradas::mostrar_entradas_busqueda(Object(Entrada)) #1 C:\\xampp\\htdocs\\blog\\index.php(117): include_once('C:\\xampp\\htdocs...') #2 {main} thrown in C:\\xampp\\htdocs\\blog\\app\\EscritorEntradas.inc.php on line 64
致命错误:未捕获错误:无法在C:\\ xampp \\ htdocs \\ blog \\ app \\ EscritorEntradas.inc.php:64中将Entrada类型的对象用作数组:#0 C:\\ xampp \\ htdocs \\ blog \\ vistas \\ buscar .php(81):EscritorEntradas :: mostrar_entradas_busqueda(Object(Entrada))#1 C:\\ xampp \\ htdocs \\ blog \\ index.php(117):include_once('C:\\ xampp \\ htdocs ...')#2 {main}在第64行的C:\\ xampp \\ htdocs \\ blog \\ app \\ EscritorEntradas.inc.php中引发
The first time you execute it, this line... 第一次执行时,此行...
$entradas = $entradas[$i - 1];
... rewrites the value of $entradas
to an object (value of the very zeroth element of $entradas
array). ...将
$entradas
的值重写为一个对象( $entradas
数组的第零个元素的值)。 The next time you try to execute it, you essentially query that object as an array, hence the error. 下次尝试执行该命令时,实际上是将该对象作为数组查询,因此会出现错误。
Solution: just rename that variable, and you're done: 解决方案:只需重命名该变量,即可完成:
$entrada = $entradas[$i - 1]; // and now it's single
self::mostrar_entrada_busqueda($entrada);
And yes, variables are scoped per function in PHP, not per block. 是的,变量是PHP中每个函数的作用域,而不是每个块。 But even if the latter were the case, you'd still have to rewrite that line;
但是,即使是后者,您仍然必须重写该行。 I'm not aware of any language that has different rules of scope resolution for right and left parts of assignment statements.
我不知道任何语言对赋值语句的左右部分具有不同的范围解析规则。
As a sidenote, I'm really not sure why you check for i % 3 == 0
inside if (i % 3 == 1)
branch. 附带说明,我真的不确定为什么要在
if (i % 3 == 1)
分支中检查i % 3 == 0
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.