• Source
    1. <?php
    2. class vehicle{
    3. /*** define public properties ***/
    4.  
    5. /*** the color of the vehicle ***/
    6. public $color;
    7.  
    8. /*** the number of doors ***/
    9. public $num_doors;
    10.  
    11. /*** the price of the vehicle ***/
    12. public $price;
    13.  
    14. /*** the shape of the vehicle ***/
    15. public $shape;
    16.  
    17. /*** the brand of vehicle ***/
    18. public $brand;
    19.  
    20. /*** the constructor ***/
    21. public function __construct(){
    22. echo 'About this Vehicle.<br />';
    23. }
    24.  
    25. /*** define some public methods ***/
    26.  
    27. /*** a method to show the vehicle price ***/
    28. public function showPrice(){
    29. echo 'This vehicle costs '.$this->price.'.<br />';
    30. }
    31.  
    32. /*** a method to show the number of doors ***/
    33. public function numDoors(){
    34. echo 'This vehicle has '.$this->num_doors.' doors.<br />';
    35. }
    36.  
    37. /*** method to drive the vehicle ***/
    38. public function drive(){
    39. echo 'Drive!';
    40. }
    41.  
    42. } /*** end of class ***/
    43.  
    44. /*** create a new vehicle object ***/
    45. $vehicle = new vehicle;
    46.  
    47. /*** the brand of vehicle ***/
    48. $vehicle->brand = 'Porsche';
    49.  
    50. /*** the shape of vehicle ***/
    51. $vehicle->shape = 'Coupe';
    52.  
    53. /*** the color of the vehicle ***/
    54. $vehicle->color = 'Red';
    55.  
    56. /*** number of doors ***/
    57. $vehicle->num_doors = 2;
    58.  
    59. /*** cost of the vehicle ***/
    60. $vehicle->price = 100000;
    61.  
    62. /*** call the showPrice method ***/
    63. $vehicle->showPrice();
    64.  
    65. /*** call the numDoors method ***/
    66. $vehicle->numDoors();
    67.  
    68. /*** drive the vehicle ***/
    69. $vehicle->drive();
    70.  
    71.  
    72.  
    73. ?>