<?php class Cars { var $wheel_count = 4; var $door_count = 4; public function car_Detail() { return "This car has " . $this->wheel_count . " wheels."; } } $bmw = new Cars(); $mercedes = new Cars(); echo $bmw->wheel_count . "<br>"; echo $mercedes->wheel_count = 10; echo $mercedes->car_Detail(); echo $bmw->car_Detail();
<?php class Cars { public $wheel_count = 4; private $door_count = 4; //available only inside the class; protected $seat_count = 2; //available only inside the class and in the inheritance classes public function car_Detail() { echo $this->wheel_count; echo $this->door_count; echo $this->seat_count; } } class Trucks extends Cars { } $bmw = new Cars(); //echo $bmw->wheel_count; //se afiseaza //echo $bmw->door_count; //nu se afiseaza pt ca nu este in ineriorul clasei // echo $bmw->seat_count; $bmw->car_Detail(); //asta le afiseaza pe toate pt ca sunt folosite in interiorul clasei
static function display() { echo parent::car_Detail(); }
<?php class Cars { public $wheel_count = 4; static $door_count = 4; static function car_Detail() { echo Cars::$door_count; } } class Trucks extends Cars { } $bmw = new Cars(); //instantiem doar pt varibilele publice, cele statice nu au nevoie de instantiere echo $bmw->wheel_count; echo Cars::$door_count; //la static folosim $ si numele clasei urmata de dublu doua puncte Cars::car_Detail();