du31992 2014-06-15 00:47 采纳率: 0%
浏览 40
已采纳

理解PHP类OOP结构[关闭]

I'm trying to create a class function but somehow it won’t work and I can’t figure out what’s the problem. Is it the way I declare the variable or what so ever?

<?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 :   
";
    echo "Model      :"  $this-> model "
";
    echo "Make       :"  $this-> make  "

";
}

function get_speed()
{
    return $this->speed;
}
}
?>

<?php
$car1 = new Car();

$car1 -> get_status("Vios", "Toyota");
for( $i = 0; $i < 5 ; $i++)
{
    echo "Accelerating... <br> 
";
    echo "Current speed : accelerate(5) km/h <br>";
}
?>
  • 写回答

3条回答 默认 最新

  • dqcuq4138 2014-06-15 00:58
    关注

    Lots of issues with your code. But at least this cleaned up version should work without the script dying completely. Here is a breakdown of what I did:

    • Set the variables that were set as var to public since that is the preferred method of setting variables.
    • In function Car, I set default values for $model, $make & $speed so if they are not passed—like in your example—there is at least a default value to act on.
    • Your echo lines in get_status did not have the . for concatenation so they were not properly concatenating the strings.
    • Then you setting $this-> make and $this-> model with empty spaces is syntactically incorrect. So set those to $this->make and $this->model.
    • Then when you are calling the class, you set this "Current speed : accelerate(5) km/h <br>"; which is syntactically incorrect as well. So set that to echo "Current speed : " . $car1->accelerate(5) . " km/h <br>"; so it can actually echo values.

    But that said, unclear on what the output is for this code. The logic is a bit of a mess. But at least it’s not completely dying like it did before!

    And here is the cleaned up code:

    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 :   
    ";
        echo "Model      :" . $this->model . "
    ";
        echo "Make       :" . $this->make  . "
    
    ";
      }
    
      function get_speed () {
        return $this->speed;
      }
    
    }
    
    $car1 = new Car();
    $car1->get_status("Vios", "Toyota");
    
    for( $i = 0; $i < 5 ; $i++) {
        echo "Accelerating... <br> 
    ";
        echo "Current speed : " . $car1->accelerate(5) . " km/h <br>";
    }
    

    And—like I said before—since the way you are calling the class makes little sense to the structure you have, I slightly reworked the code above so it loops through an array of car values like this:

    $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> 
    ";
        echo "Current speed : " . $car1->accelerate(5) . " km/h <br>";
      }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)