簡體   English   中英

未捕獲的錯誤:調用數組中的成員函數function()

[英]Uncaught Error: Call to a member function function() on array

我在使用php / oop時遇到問題,因為在調用對象的方法時出現此錯誤:“未捕獲的錯誤:在E數組中的數組中調用成員函數obtener_nombre():\\ xampp \\ htdocs \\ hackatonphp \\ App \\ RepositoryProducts .php:79“這是代碼

實例化對象的類

<?php 

class Producto {

    private $nombre;
    private $precio;
    private $categoria;

    //Constructor
    public function __construct($nombre,$precio,$categoria){
        $this -> nombre = $nombre;
        $this -> precio = $precio;
        $this -> categoria = $categoria;
    }

    //Setters
    public function definir_nombre($nombre){
        $this -> nombre = $nombre;
    }

    public function definir_precio($precio){
        $this -> precio = $precio;
    }

    public function definir_categoria($categoria){
        $this -> categoria = $categoria;
    }

    //Getters
    public function obtener_nombre(){
        return $this -> nombre;
    }

    public function obtener_precio(){
        return $this -> precio;
    }

    public function obtener_categoria(){
        return $this -> categoria;
    }
}

顯示產品的代碼

        public function obtener_productos($conexion,$categoria){

        $productos = array();

        if (isset($conexion)) {
            try {
                $sql = 'SELECT * FROM productos WHERE categoria = :categoria';

                $sentencia = $conexion -> prepare($sql);

                $sentencia -> bindParam(':categoria', $categoria);

                $sentencia -> execute();

                $productos = $sentencia -> fetchAll();

                foreach ($productos as $producto) {
                    $productos[] = new Producto($producto['nombre'],$producto['precio'],$producto['categoria']);
                }
            } catch (PDOException $ex) {
                print 'ERROR' . $ex->getMessage();
            }
        }

        return $productos;
    }

     public static function mostrar_productos($categoria) {
        Conexion::abrir_conexion();
        $productos = self::obtener_productos(Conexion::obtener_conexion(),$categoria);
        Conexion::cerrar_conexion();
        if (count($productos)) {

            foreach ($productos as $producto) {
                self::escribir_producto($producto);
            }
        }
    }

    public static function escribir_producto($producto) {
        if (!isset($producto)) {
            return;
        } else {
        ?>
            <div class='columns'>
                <!-- Inicio de la presentación -->
                <div class='card h1-strong column is-one-third'>
                    <a href='#'>
                        <center>
                            <img class='img-index' src='Vendor/img/higiene.png'>
                            <h1 style='font-size: 3em;'><?php $producto -> obtener_nombre(); ?></h1>
                            <h3 style='font-size: 2em;'><?php $producto -> obtener_precio(); ?></h3>
                        </center>
                        <br>
                    </a>
                </div>
                <!-- Fin de la presentación -->
            </div>
        <?php
        }
    }`

實現:

<!-- Linea de opciones -->

<?php

    RepositorioProductos::mostrar_productos("comida");

?>

問題是obtener_productos()fetchAll()返回的數組和返回的Producto數組都使用了變量$prodctos 因此,它返回的數組元素既包含數據庫中的行(關聯數組)又包含對象。

使用不同的變量。

public function obtener_productos($conexion,$categoria){

    $productos = array();

    if (isset($conexion)) {
        $productos = array();
        try {
            $sql = 'SELECT * FROM productos WHERE categoria = :categoria';
            $sentencia = $conexion -> prepare($sql);
            $sentencia -> bindParam(':categoria', $categoria);
            $sentencia -> execute();
            $results = $sentencia -> fetchAll();
            foreach ($results as $producto) {
                $productos[] = new Producto($producto['nombre'],$producto['precio'],$producto['categoria']);
            }
        } catch (PDOException $ex) {
            print 'ERROR' . $ex->getMessage();
        }
    }

    return $productos;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM