class User { public $name; public $email; public function setName($name) { $this->name = $name; } public function setEmail($email) { $this->email = $email; } public function getName() { return $this->name; } public function getEmail() { return $this->email; } } $user = new User(); $user->setName('John Doe'); $user->setEmail('johndoe@example.com'); echo $user->getName(); // Output: John Doe echo $user->getEmail(); // Output: johndoe@example.com
class Car { private $model; private $color; public function __construct($model, $color) { $this->model = $model; $this->color = $color; } public function getInfo() { return "This car is a " . $this->color . " " . $this->model . "."; } } $car1 = new Car('Sedan', 'Red'); echo $car1->getInfo(); // Output: This car is a Red Sedan. $car2 = new Car('SUV', 'Blue'); echo $car2->getInfo(); // Output: This car is a Blue SUV.This example creates a `Car` class that has two private properties (`model` and `color`) and a constructor method that sets these properties when a new instance of the class is created. It also has a `getInfo()` method that returns a string containing the `model` and `color` properties. Package Library: There is no specific package library used in this example. In conclusion, PHP object data allows developers to work with object-oriented concepts in PHP to create more structured and reusable code. There are various package libraries available that provide prebuilt classes and methods to simplify development and speed up the coding process.