1

因此,我认为将是一个简单的 50 多行代码已经发展成比我以前做过的任何事情都要大得多的东西,我决定将我的代码移动到类中。

这对我来说是第一次,所以我不是 100% 认为新结构应该如何组合在一起......

所以我创建了一个名为 weather in 的类weather.php

class weather
{
    // various variables and functions like getWeather() go here
}

我在 index.php 中引用它如下:

require_once("weather.php");

// instantiate the weather class.
$w = new weather();

// get the weather by calling the getWeather() function
$weather = $w->getWeather();

根据我对我在 SO 和其他网站上看到的内容的理解(也去过 PHP 手册),这应该可以工作,但是,我不断收到这个错误:

致命错误:在第 4 行的 C:\usbwebserver\root\weather\index.php 中找不到类“天气”

第 4 行是我在$w.

我不确定我在这里做错了什么,有人可以向我解释一下吗?

编辑
在类似乎没有向浏览器写入任何内容之前 在顶部添加回声weather.php,但是,写入浏览器的内容似乎是类中存在的代码......

4

1 回答 1

0

您似乎缺少weather.php文件中的 php 开始标记。

于 2013-03-13T13:36:19.950 回答