class Person { private $id; public function __construct() { $this->id = uniqid(); } public function setId($id) { $this->id = $id; } public function getId() { return $this->id; } } $person1 = new Person(); $person2 = new Person(); $person1->setId('person1'); $person2->setId('person2'); echo $person1->getId(); // outputs "person1" echo $person2->getId(); // outputs "person2"
class Person { private $id; private $name; private $age; public function __construct($name, $age) { $this->id = uniqid(); $this->name = $name; $this->age = $age; } public function getId() { return $this->id; } public function getName() { return $this->name; } public function getAge() { return $this->age; } public static function findById($id) { // query database to retrieve person by ID $personData = getPersonDataFromDb($id); if(!$personData) { return null; } $person = new Person($personData['name'], $personData['age']); $person->setId($personData['id']); return $person; } public function save() { // save person data to database savePersonDataToDb($this->id, $this->name, $this->age); } } // create a new person instance $person = new Person('John Doe', 30); // save the person data to database $person->save(); // retrieve the person data from database using getById method $retrievedPerson = Person::findById($person->getId()); echo $retrievedPerson->getName(); // outputs "John Doe" echo $retrievedPerson->getAge(); // outputs "30"Package library: Not mentioned.