简体   繁体   中英

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

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). 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM