Static properties and methods
So far, all the properties and methods were linked to a specific instance; so two different instances could have two different values for the same property. PHP allows you to have properties and methods linked to the class itself rather than to the object. These properties and methods are defined with the keyword static
.
private static $lastId = 0;
Add the preceding property to the Customer
class. This property shows the last ID assigned to a user, and is useful in order to know the ID that should be assigned to a new user. Let's change the constructor of our class as follows:
public function __construct( int $id, string $name, string $surname, string $email ) { if ($id == null) { $this->id = ++self::$lastId; } else { $this->id = $id; if ($id > self::$lastId) { self::$lastId = $id; } } $this->name = $name; $this->surname = $surname; $this->email = $email...