Witam, stworzyłem klasę Car, która ma posiadać zmienną statyczną $ile, która zwiększa się za każdym stworzeniem obiektu za pomocą operatora new. Czy jest jakiś sposób, aby tą ilość później wyświetlić w metodzie __toString, albo jakkolwiek inaczej?
<?php
class Car{
private static int $ile = 0;
private $model;
private $cena;
private $kurs;
public function __construct(){
self::$ile++;
}
public function __get($property) {
if ($property === 'model') {
return $this->model;
}
if ($property === 'cena'){
return $this->cena;
}
if ($property === 'kurs'){
return $this->kurs;
}
}
public function __set($property, $value) {
if ($property === 'model') {
$this->model = $value;
}
if ($property === 'cena'){
$this->cena = $value;
}
if ($property === 'kurs'){
$this->kurs = $value;
}
}
public function value(){
return $this->cena * $this->kurs;
}
public function __toString(): string
{
return "Ile: $this->ile\n". // to nie działa :(
"Model: $this->model\n".
"Cena: $this->cena\n".
"Kurs: $this->kurs\n";
}
}
?>