首页 > 解决方案 > 包括一个带有 PHP 的文件 - 避免找不到?

问题描述

我是 php 新手,我有两个文件,一个是我想保留我的类和大量代码的地方,另一个是我与一些 HTML 集成的地方。我的代码正在运行,但现在我收到错误消息:

PHP 致命错误:未捕获的错误:在 C:\MAMP\htdocs\index.view.php:84 中找不到类 'bardrink' 堆栈跟踪:> #0 {main} 在 C:\MAMP\htdocs\index.view 中抛出。第 84 行的 php

这是我的 index.php 文件:

<?php

class bardrink {
    public $drink_name;
    public $drink_desc;
    public $drink_strength;
    public $drink_price;

    public function __construct($drink_name,$drink_desc,$drink_strength,$drink_price)

    {
      $this->drink_name = $drink_name;
      $this->drink_desc = $drink_desc;
      $this->drink_strength = $drink_strength;
      $this->drink_price = $drink_price;
    }
  }

require 'index.view.php';

 ?>

在我的 index.view.php 我添加:

<?php

    $barselection = [
      new bardrink('Water','A light refreshing drink.',0,1),
      new bardrink('Light Ale','A pale ale brewed locally.',2,4),
      new bardrink('Mulled Wine','A warm festive wine, perfect for a chilly evening!',3,6),
      new bardrink('Dark Ale','A strange frothy, dark liquid...',5,10)
    ];

    var_dump($barselection)

?>

我在索引文件中需要 index.view.php,但我是否需要在我的视图文件中添加一个 require 才能使其工作?

标签: php

解决方案


推荐阅读