class Animal { private $id; public function setId($id) { $this->id = $id; } } $cat = new Animal(); $cat->setId(1); echo $cat->getId(); // will output 1
class User { private $id; private $name; public function __construct($id, $name) { $this->id = $id; $this->name = $name; } public function setId($id) { $this->id = $id; } public function __toString() { return "$this->id - $this->name"; } } $user1 = new User(1, "John"); $user1->setId(2); echo $user1; // will output "2 - John"In this example, we have a more complex User class with a constructor to set the initial ID and name properties. The setId method is used to change the ID of the object. The __toString method is used to display the ID and name properties of the object in a formatted string. The package library for the self setId method is part of the PHP core and does not require any external libraries.