Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
理解PHP类OOP结构_Php_Class - Fatal编程技术网

理解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我能做的最好,我不再做了。你现在的工作就是清理逻辑。