PHP类实例调用中的未定义变量通知 [英] Undefined variable notices in PHP class instance call
问题描述
使用PHP,当我调用类的实例时,我得到的是未定义的变量通知。我使用其他语言编程,但是对PHP不太熟悉,能否请您查看我的代码并告知我定义类变量时的错误?
Using PHP, when I call an instance of a class I am getting an undefined variable notice back. I program in other languages, but am less familiar with PHP, can you please look at my code and inform me as to my mistake in defining my class variables? Thanks.
<?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中的英里
注意:未定义变量:/ home / heidi / Desktop / CodingDojo /中的英里第35行的OOP / php_oop_basic1.php
注意:未定义的变量:第21行的/home/heidi/Desktop/CodingDojo/OOP/php_oop_basic1.php的价格
注意:未定义的变量:在第21行的/home/heidi/Desktop/CodingDojo/OOP/php_oop_basic1.php中的max_speed注意:未定义变量:在/home/heidi/Desktop/CodingDojo/OOP/php_oop_basic1.php中的英里21
Notice: Undefined variable: miles in /home/heidi/Desktop/CodingDojo/OOP/php_oop_basic1.php on line 27
Notice: Undefined variable: miles in /home/heidi/Desktop/CodingDojo/OOP/php_oop_basic1.php on line 35
Notice: Undefined variable: price in /home/heidi/Desktop/CodingDojo/OOP/php_oop_basic1.php on line 21
Notice: Undefined variable: max_speed in /home/heidi/Desktop/CodingDojo/OOP/php_oop_basic1.php on line 21
Notice: Undefined variable: miles in /home/heidi/Desktop/CodingDojo/OOP/php_oop_basic1.php on line 21
推荐答案
在PHP(以及javascript,python和许多其他语言)中, $ this
在引用类变量时是显式的:
In PHP (and in javascript, python and many others), $this
is explicit when referencing class variables:
// local variable
$price
// class variable
$this->price
还可以看看手册。
这篇关于PHP类实例调用中的未定义变量通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!