使用php包含类的函数

时间:2014-03-29 17:29:28

标签: php mysql apache include directory

试图在php中挂起类,尝试将carClass.php包含到new_file.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";        
        }

    }

?>

它只是一堆吸气剂和二传手。我试图把这个类包含到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();

?>

当我尝试执行此文件时,我收到这些错误消息

警告:include(carClass.php):无法打开流:第4行的C:\ xampp \ htdocs \ testFile \ new_file.php中没有此类文件或目录

警告:include():无法打开&#39; carClass.php&#39;在第4行的C:\ xampp \ htdocs \ testFile \ new_file.php中包含(include_path =&#39;。; C:\ xampp \ php \ PEAR&#39;)

致命错误:Class&#39; carClass&#39;在第6行的C:\ xampp \ htdocs \ testFile \ new_file.php中找不到

我相信这两个文件都在testFile目录中,所以我不知道最近会发生什么。我很感激你们可以像往常一样给我任何帮助。

3 个答案:

答案 0 :(得分: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();

    ?>

答案 1 :(得分:0)

您可以使用PHP的自动加载功能,该功能将在创建对象时自动加载类。如果您有许多类,那么您可以将它用作头文件,这样您就不必担心每次都使用此包含。

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

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

答案 2 :(得分: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();

?>