php - 使用抽象类“找不到类”的 PHP 错误
问题描述
我有一个简单的脚本,其中包括:
- 抽象类
- 普通班
- 和一个 indes 页面
出于某种原因,我收到此错误:
( ! ) 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
索引.php
include_once 'abstract/Products.php';
include_once 'classes/GetAllProducts.php';
$customer = new GetAllProducts();
$customer->allData();
摘要/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'
)
);
}
类/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>';
}
}
}
解决方案
您必须将抽象类包含到GetAllProducts.php
文件中,因为您在该文件中使用抽象类,而不是在index.php
.
<?php
include_once 'abstract/Products.php';
class GetAllProducts extends Products {}
推荐阅读
- angular - 什么时候用Subject, BehaviorSubject 和实例
- ruby-on-rails - 如何检查传递给 `link_to` 的 url 是否有效
- phpstorm - 仅限 PhpStorm 大写 SQL 关键字
- vba - 奇怪的 VBA 演示文稿关闭运行时错误 #91
- html - 为什么当我为 doc 文件替换 HTML 模板中的字符串时,Python 会更改字体设置
- c++ - VS2017:VS 显示错误的项目平台?
- google-cloud-firestore - 集成批量提交和 Cloud Firestore 触发器
- swift - 无法使用匹配类型转换类型的值 - 我是否遇到了编译器限制?
- arrays - 在单元格上单击标签的值应该增加和减少
- javascript - 如何从插槽调用组件方法 - Vue