PHP类实例调用中未定义的变量通知

PHP类实例调用中未定义的变量通知,php,debugging,Php,Debugging,使用PHP,当我调用一个类的实例时,我得到了一个未定义的变量通知。我用其他语言编程,但对PHP不太熟悉,你能看看我的代码并告诉我在定义类变量时的错误吗?谢谢 <?php // create class Bike class Bike { // create variables for class Bike of price, max_speed, and miles var $price; var $max_speed

使用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();
?>
在PHP(以及javascript、python和许多其他语言)中,
$this
在引用类变量时是显式的:

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

还可以查看。

您尝试分配对象变量(属性)的每个实例,您需要使用
$this->var\u 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();
?>

此外,还要研究定义属性和方法。在对象中使用
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
获取类定义之外的公共属性(对象变量)。私有和受保护的属性将抛出致命错误,因为它们无法直接访问。

运行此操作时收到的消息是什么?注意:未定义变量:miles in/home/heidi/Desktop/CodingDojo/OOP/php_OOP_basic1.php第27行注意:未定义变量:miles in/home/heidi/Desktop/CodingDojo/OOP/php_basic1.php第35行注意:未定义变量:第21行的price in/home/heidi/Desktop/CodingDojo/OOP/php_OOP_basic1.php注意:未定义变量:第21行的max_speed in/home/heidi/Desktop/CodingDojo/OOP/php_OOP_basic1.php注意:未定义变量:第21行的miles in/home/heidi/Desktop/CodingDojo/OOP/php_OOP_basic1.php一点也不坏
$price
本身就是方法中的局部变量。非常普遍的做法。问题是OP试图将它们用作全局变量,而
$price
未在该方法的范围内定义,因此出现了错误/警告。我的意思是这不利于访问类变量。我已将
bad
更改为
local variable