2014-03-29 57 views
0

试图在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目录,所以我不知道怎么回事。我感谢你们像往常一样帮助你们。

+0

你是如何调用/执行new_file.php?除非php的当前工作目录与'new_file.php'目录相同,否则它不会在'carClass.php'中寻找,你需要使用一个相对路径来指向cwd,或者使用绝对路径。 –

回答

0

包含路径设定对服务器配置(php.ini中),但包括路径指定是相对于路径,以便在你的情况下,包括路径(在Windows实际路径):

<?php 

    include_once dirname(__FILE__) . '/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(); 

    ?> 
0

您可以使用PHP的自动加载功能,该功能会在创建对象时自动加载类。如果你有很多类,那么你可以使用它的头文件,所以你不必担心每次都使用这个包含。

<?php 
function __autoload($class_name) { 
    include $class_name . '.php'; 
} 

$obj = new MyClass1(); 
$obj2 = new MyClass2(); 
?> 
0

试试这个:

<?php 
    class carClass 

    { 
     private $color; 
     private $gear; 
     private $model; 
     private $gas; 

     function paintCar($carColor) { 
      $this->color = $carColor; 
     } 

     function findCarColor() { 
      echo $this->color; 
     } 

     function shiftGear($newGear) { 
      $this->gear=$newGear; 
     } 

     function findGear() { 
      echo $this->gear; 
     } 

     function chooseModel($newModel) { 
      $this->model = $newModel; 
     } 

     function findModel() { 
      echo $this->model; 
     } 

     function fillCar($gasAmount) { 
      $this->gas = $gasAmount; 
     } 

     function lookAtGauge() { 
      echo $this->gas;   
     } 

    } 

?> 
// 
    <?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->lookAtGauge(); 

?> 
+0

它对我有用: 输出在我身边是: Mustangblack5thhalf –