试图在php中获得一堆类,试图将carClass.php包含到new_file.php中。使用php包括功能类
carClass.php
<?php
class carClass
{
private $color;
private $gear;
private $model;
private $gas;
function paintCar($carColor) {
$this->color = $carColor;
}
function findCarColor() {
echo "$color";
}
function shiftGear($newGear) {
$this->gear=$newGear;
}
function findGear() {
echo "$gear";
}
function chooseModel($newModel) {
$this->model = $newModel;
}
function findModel() {
echo"$model";
}
function fillCar($gasAmount) {
$this->gas = $gasAmount;
}
function lookAtGauge() {
echo "$gas";
}
}
?>
它只是一堆getter和setter。我试着去包括该类new_file.php
new_file.php
<?php
include("carClass.php");
$car = new carClass;
$car->chooseModel("Mustang");
$car->paintCar("black");
$car->shiftGear("5th");
$car->fillCar("half");
$car->findModel();
$car->findCarColor();
$car->findGear();
$car->lookAtGuage();
?>
当我尝试执行这个文件,我得到这些错误信息
警告:包括(carClass.php):失败打开流:没有这样的文件或目录在C:\ xampp \ htdocs \ testFile \ new_file.php在线4
警告:包括():失败打开'carClass.php'包含(include_path ='。第4行的C:\ xampp \ htdocs \ testFile \ new_file.php中的C:\ xampp \ php \ PEAR')
致命错误:类“carClass”未发现在C:\ XAMPP \ htdocs中\ TESTFILE \ new_file.php第6行
我相信,这两个文件都在TESTFILE目录,所以我不知道怎么回事。我感谢你们像往常一样帮助你们。
你是如何调用/执行new_file.php?除非php的当前工作目录与'new_file.php'目录相同,否则它不会在'carClass.php'中寻找,你需要使用一个相对路径来指向cwd,或者使用绝对路径。 –