PHP 使用摘要时出错 Class "Class Not Found"

PHP Error Using Abstract Class "Class Not Found"

我有一个简单的脚本,其中包括:

出于某种原因我得到这个错误:

( ! ) Fatal error: Uncaught Error: Class 'Products' not found in 

D:\xampp\htdocs\ds2\classes\GetAllProducts.php on line 4
( ! ) Error: Class 'Products' not found in D:\xampp\htdocs\ds2\classes\GetAllProducts.php on line 4
Call Stack
#   Time    Memory  Function    Location
1   0.4119  410648  {main}( )   ...\index.php:0
2   0.4129  414904  include_once( 'D:\xampp\htdocs\ds2\classes\GetAllProducts.php' )    ...\index.php:3

这是我的代码:

index.php

include_once 'abstract/Products.php';
include_once 'classes/GetAllProducts.php';


$customer = new GetAllProducts();
$customer->allData();

abstract/Products.php

<?

    abstract class Products {
        public $data = array(
            array(
                'id' => 1,
                'title' => 'title 1',
                'desc' => 'desc 1'
            ),
            array(
                'id' => 2,
                'title' => 'title 2',
                'desc' => 'desc 2'
            ),
            array(
                'id' => 3,
                'title' => 'title 3',
                'desc' => 'desc 3'
            )
        );
     
    }

classes/GetAllProducts.php

<?php

class GetAllProducts extends Products {

    public function allData() {
        $this->data;
        foreach($this->data as $product){
            echo '
            <article id="product-'.$product['id'].'" class="product">
                <h2 product__title="">'.$product['id'].'</h2>
                <picture product__image="'.$product['title'].'">
                    <source media="(min-width:768px)" srcset="https://picsum.photos/200/300">
                    <img src="https://picsum.photos/200" alt="Flowers" style="width:auto;">
                </picture>
                <button class="product__button" role="button">לצפיה במוצר</button>
            </article>';
        }
    }
}

您必须将摘要 class 包含到 GetAllProducts.php 文件中,因为您在该文件中而不是 index.php.[=13 中使用摘要 class =]

<?php
include_once 'abstract/Products.php';

class GetAllProducts extends Products {}

解决问题的是 abstract/Products.php 文件以 <? 开头,而不是 <?php