理解PHP类OOP结构
我试图创建一个类函数,但不知怎么的,它不起作用,我也不知道问题出在哪里。这是我声明变量的方式还是什么理解PHP类OOP结构,php,class,Php,Class,我试图创建一个类函数,但不知怎么的,它不起作用,我也不知道问题出在哪里。这是我声明变量的方式还是什么 <?php class Car{ var $model; var $make; var $speed; function Car ( $model, $make, $speed) { $this->model = $model; $this->make = $make; $this->speed = $speed; } function
<?php
class Car{
var $model;
var $make;
var $speed;
function Car ( $model, $make, $speed)
{
$this->model = $model;
$this->make = $make;
$this->speed = $speed;
}
function accelerate ($speed)
{
$add = 5;
$speed = $speed + $add;
return $speed;
}
function get_status()
{
echo "Car status : \n";
echo "Model :" $this-> model "\n";
echo "Make :" $this-> make "\n\n";
}
function get_speed()
{
return $this->speed;
}
}
?>
<?php
$car1 = new Car();
$car1 -> get_status("Vios", "Toyota");
for( $i = 0; $i < 5 ; $i++)
{
echo "Accelerating... <br> \n";
echo "Current speed : accelerate(5) km/h <br>";
}
?>
连接字符串时,必须使用点运算符:
echo "abcd" . "efgh";
你的代码有很多问题。但至少这个清理过的版本应该可以在脚本没有完全死掉的情况下工作。以下是我所做工作的细目:
- 将设置为
的变量设置为var
,因为这是设置变量的首选方法public
- 在
中,我为功能车
,$model
和$make
设置了默认值,因此,如果它们没有像您的示例中那样被传递,则至少有一个默认值要执行$speed
中的get\u status
行没有用于连接的echo
,因此它们没有正确连接字符串- 然后,将
和$this->make
设置为空空格在语法上是不正确的。因此,将它们设置为$this->model
和$this->make
$this->model
- 然后,当您调用该类时,您可以设置此
“当前速度:加速(5)km/h
”代码>这在语法上也是不正确的。因此,将其设置为echo“当前速度:”$car1->加速(5)。“公里/小时
”代码>这样它就可以实际回显值
class Car {
public $model;
public $make;
public $speed;
function Car ($model = 0, $make = 0, $speed = 0) {
$this->model = $model;
$this->make = $make;
$this->speed = $speed;
}
function accelerate ($speed) {
$add = 5;
$speed = $speed + $add;
return $speed;
}
function get_status () {
echo "Car status : \n";
echo "Model :" . $this->model . "\n";
echo "Make :" . $this->make . "\n\n";
}
function get_speed () {
return $this->speed;
}
}
$car1 = new Car();
$car1->get_status("Vios", "Toyota");
for( $i = 0; $i < 5 ; $i++) {
echo "Accelerating... <br> \n";
echo "Current speed : " . $car1->accelerate(5) . " km/h <br>";
}
等级车{
公帑模式;
公开募捐;
公共速度;
功能车($model=0,$make=0,$speed=0){
$this->model=$model;
$this->make=$make;
$this->speed=$speed;
}
功能加速($speed){
$add=5;
$speed=$speed+$add;
返回$speed;
}
函数get_status(){
回显“车辆状态:\n”;
echo“Model:”.$this->Model.\n;
echo“Make:”.$this->Make.\n\n;
}
函数get_speed(){
返回$this->speed;
}
}
$car1=新车();
$car1->get_status(“Vios”,“丰田”);
对于($i=0;$i<5;$i++){
回显“加速…
\n”;
回声“当前速度:”.$car1->加速(5)。“km/h
”;
}
正如我之前所说的,由于调用类的方式对现有的结构没有什么意义,我稍微修改了上面的代码,使其循环遍历一个car值数组,如下所示:
$car_array = array("Vios", "Toyota");
foreach ($car_array as $car_value) {
$car1 = new Car($car_value);
$car1->get_status();
for( $i = 0; $i < 5 ; $i++) {
echo "Accelerating... <br> \n";
echo "Current speed : " . $car1->accelerate(5) . " km/h <br>";
}
}
public function __construct($model = 0, $make = 0, $speed = 0) {
$this->model = $model;
$this->make = $make;
$this->speed = $speed;
}
$car_array=array(“Vios”、“Toyota”);
foreach($car\u数组作为$car\u值){
$car1=新车($Car\U值);
$car1->get_status();
对于($i=0;$i<5;$i++){
回显“加速…
\n”;
回声“当前速度:”.$car1->加速(5)。“km/h
”;
}
}
添加如下构造函数:
$car_array = array("Vios", "Toyota");
foreach ($car_array as $car_value) {
$car1 = new Car($car_value);
$car1->get_status();
for( $i = 0; $i < 5 ; $i++) {
echo "Accelerating... <br> \n";
echo "Current speed : " . $car1->accelerate(5) . " km/h <br>";
}
}
public function __construct($model = 0, $make = 0, $speed = 0) {
$this->model = $model;
$this->make = $make;
$this->speed = $speed;
}
是否有任何问题或?确保在所有属性(如var)和所有函数上声明public、private或protected。另外,请显示类->方法()的执行情况。您使用的是php4样式。您正在使用未定义的参数调用方法
get\u status
。加速时,更新的是局部变量,而不是对象成员。你实际上并没有在任何地方打电话。那么,您遇到的问题是哪一个?正如@philipp告诉您的,您没有正确地连接字符串。@DanFromGermany“有任何问题吗?”或者…?“所显示的所有代码都有100%的问题。我尽了最大努力做了基本的清理,并在我的答案中提供了注释,但一旦错误被消除,还有很多问题需要解决。谢谢你指出。。。逻辑是,每次循环运行时,它都应该以5的增量显示汽车加速。@user3544721我能做的最好,我不再做了。你现在的工作就是清理逻辑。