static function display() { /* The 'parent' keyword references the class that the current class is extending. It can be used to call upon a parent class's methods or properties. */ echo parent::car_detail(); }
// property static $wheel_count = 4; static $door_count = 4; // methods static function car_detail() { /* With a static function, you do not use the "$this" pseudo-variable because you are not using an instance. You format the properties the same way you would with a static property. */ echo Cars::$wheel_count; echo Cars::$door_count; } } $bmw = new Cars(); /* echo $bmw->door_count; echo $bmw->wheel_count; */ // Browser sees these static properties as undefined // because they're being called with an instance. echo Cars::$door_count; /* To call a static property, use the class name followed by two colons ( : ) and the property name, after a dollar sign ( $ ). */ Cars::car_detail(); /* To call a static function, use the class name followed by two colons and the function name WITHOUT a dollar sign. */
<?php class Cars { // Properties - Access Control Modifiers /* Access modifiers are not used for privacy's sake but instead to achieve better control of variables throughout a document */ /* public property can be used throughout the whole program */ public $wheel_count = 4; /* private property can be used within the class it's defined */ private $door_count = 4; /* protected property is only available inside it's class or subclass (extends '...') */ protected $seat_count = '2'; function car_detail() { echo $this->wheel_count; echo $this->door_count; echo $this->seat_count; } } $bmw = new Cars(); $bmw->car_detail();
<?php class Cars { //property var $wheel_count = 4; var $door_count = 4; //method function car_detail() { return "This car has " . $this->wheel_count . " wheels"; } } $bmw = new Cars(); $mercedes = new Cars(); echo $bmw->wheel_count = 10; echo "<br>"; echo $mercedes->wheel_count . "<br>"; echo $mercedes->car_detail() . "<br>"; echo $bmw->car_detail();