php类学习 汽车类

发布时间 2023-04-17 16:09:09作者: paoPaoLong_liu
<?php
class Car
{
    var $color;
    function Car($color="red") {
      $this->color = $color;
    }
    function what_color() {
      return $this->color;
    }
}

function print_vars($obj) {
   foreach (get_object_vars($obj) as $prop => $val) {
     echo "    $prop = $val";
   }
}

// instantiate one object
$herbie = new Car("white");
$herbie1 = new Car("grey");

// show herbie properties
echo "herbie: Properties";
print_vars($herbie);
echo ' 方法:';
echo $herbie->what_color();
//print_vars($herbie1);

?>