0

PHP を使用して、クラスのインスタンスを呼び出すと、未定義の変数通知が返されます。私は他の言語でプログラミングしていますが、PHP にはあまり詳しくありません。私のコードを見て、クラス変数の定義の間違いを教えてもらえますか? ありがとう。

<?php
    // create class Bike
    class Bike
    {
        // create variables for class Bike of price, max_speed, and miles
        var $price;
        var $max_speed;
        var $miles;

        // create constructor for class Bike with user set price and max_speed
        function __construct($price, $max_speed)
        {
            $this->price = $price;
            $this->max_speed = $max_speed;
        }

        // METHODS for class Bike:
        // displayInfo() - have this method display the bike's price, maximum speed, and the total miles driven
        function displayInfo()
        {
            echo "Price: $" . $price .  "<p>Maximum Speed: " . $max_speed . "</p>" . "<p>Total Miles Driven: " . $miles . "</p>";
        }

        // drive() - have it display "Driving" on the screen and increase the total miles driven by 10
        function drive()
        {
            $miles = $miles + 10;
            echo "<p>Driving!</p>" . "<p>Total Miles Driven: " . $miles . "</p>";
        }

        // reverse() - have it display "Reversing" on the screen and decrease the total miles driven by 5...
        function reverse()
        {
            // What would you do to prevent the instance from having negative miles?
            if($miles >= 5)
            {
                $miles = $miles - 5;
                echo "<p>Reversing</p><p>Total Miles Driven: " . $miles . "</p>";
            }
            else
            {
                echo "<p>Total Miles Driven is less than 5 miles, unable to reverse!</p>";
            }
        }
    }
?>

<?php
    // Create 3 bike object class instances
    $Schwinn = new Bike(50, '10mph');
    $Specialized = new Bike(500, '25mph');
    $Cannondale = new Bike(1000, '50mph');

    // Have the first instance drive three times, reverse one and have it displayInfo().
    var_dump($Schwinn); // shows instance is created with proper variable assignments
    $Schwinn -> drive();
    // $Schwinn -> drive();
    // $Schwinn -> drive();
    $Schwinn -> reverse();
    $Schwinn -> displayInfo();
?>

エラー:

注意: 未定義変数: 27 行目の /home/heidi/Desktop/CodingDojo/OOP/php_oop_basic1.php のマイル数
注意: 未定義変数: 35 行目の /home/heidi/Desktop/CodingDojo/OOP/php_oop_basic1.php のマイル数
注意:未定義変数: price in /home/heidi/Desktop/CodingDojo/OOP/php_oop_basic1.php 行 21
Notice: 未定義変数: max_speed in /home/heidi/Desktop/CodingDojo/OOP/php_oop_basic1.php 行 21
Notice: 未定義変数: /home/heidi/Desktop/CodingDojo/OOP/php_oop_basic1.php の 21 行目のマイル

4

2 に答える 2

5

PHP (および javascript、python など) では、$thisクラス変数を参照するときに明示的です。

// local variable
$price
// class variable
$this->price

マニュアルもご覧ください。

于 2013-08-22T18:37:10.167 に答える
2

使用する必要があるオブジェクトの変数 (プロパティ) を割り当てようとするすべてのインスタンス$this->var_name。コードは次のように更新する必要があります。

<?php
    // create class Bike
    class Bike
    {
        // create variables for class Bike of price, max_speed, and miles
        var $price;
        var $max_speed;
        var $miles;

        // create constructor for class Bike with user set price and max_speed
        function __construct($price, $max_speed)
        {
            $this->price = $price;
            $this->max_speed = $max_speed;
        }

        // METHODS for class Bike:
        // displayInfo() - have this method display the bike's price, maximum speed, and the total miles driven
        function displayInfo()
        {
            echo "Price: $" . $this->price .  "<p>Maximum Speed: " . $this->max_speed . "</p>" . "<p>Total Miles Driven: " . $this->miles . "</p>";
        }

        // drive() - have it display "Driving" on the screen and increase the total miles driven by 10
        function drive()
        {
            $this->miles = $this->miles + 10;
            echo "<p>Driving!</p>" . "<p>Total Miles Driven: " . $this->miles . "</p>";
        }

        // reverse() - have it display "Reversing" on the screen and decrease the total miles driven by 5...
        function reverse()
        {
            // What would you do to prevent the instance from having negative miles?
            if($this->miles >= 5)
            {
                $this->miles = $this->miles - 5;
                echo "<p>Reversing</p><p>Total Miles Driven: " . $this->miles . "</p>";
            }
            else
            {
                echo "<p>Total Miles Driven is less than 5 miles, unable to reverse!</p>";
            }
        }
    }
?>

<?php
    // Create 3 bike object class instances
    $Schwinn = new Bike(50, '10mph');
    $Specialized = new Bike(500, '25mph');
    $Cannondale = new Bike(1000, '50mph');

    // Have the first instance drive three times, reverse one and have it displayInfo().
    var_dump($Schwinn); // shows instance is created with proper variable assignments
    $Schwinn -> drive();
    // $Schwinn -> drive();
    // $Schwinn -> drive();
    $Schwinn -> reverse();
    $Schwinn -> displayInfo();
?>


この出力:

object(Bike)#1 (3) { ["price"]=> int(50) ["max_speed"]=> string(5) "10mph" ["miles"]=> NULL }

Driving!

Total Miles Driven: 10

Reversing

Total Miles Driven: 5
Price: $50

Maximum Speed: 10mph

Total Miles Driven: 5


また、プロパティとメソッドVisibilityの定義を調べてください。オブジェクト内でプロパティを定義するとvar、それが「パブリック」可視性スコープに設定されていると想定されます。例:

class Bike
{
    var $price; //var was almost depreciated in objects (early PHP 5.0)

    public $price; //Now the commonly accepted method, same behavior as above.

    protected $price; //protected means only this class, parent class or extended class can use this $price

    private $price; //Private means only this class may use this $price property.

    public $price = ''; //You can also define the variable when declaring it.

同じ可視性スコープでメソッド (クラス関数) を宣言することもできます。また、 を使用して、クラス定義の外部でのみパブリック プロパティ (オブジェクト変数) を取得できます$class->var_name。プライベートおよび保護されたプロパティは、直接アクセスできないため、致命的なエラーをスローします。

于 2013-08-22T18:41:58.543 に答える