class User { private $name; private $email; function __construct($name, $email) { $this->name = $name; $this->email = $email; } function getName() { return $this->name; } function getEmail() { return $this->email; } } $user1 = new User("John Doe", "johndoe@example.com"); echo $user1->getName() . "
"; echo $user1->getEmail();
class Animal { protected $name; function __construct($name) { $this->name = $name; } function getName() { return $this->name; } } class Cat extends Animal { function meow() { return "Meow!"; } } $cat1 = new Cat("Whiskers"); echo $cat1->getName() . "This code creates an Animal class with a constructor that accepts a name parameter, and a Cat class that inherits from Animal. The Cat class also has a meow() method that returns the string "Meow!". The Cat class does not have its own constructor, so it uses the constructor from the Animal class to set the name property. The $cat1 object is created using the Cat class, and the getName() and meow() methods are called. Package library: This example does not make use of any external package library, it only uses built-in PHP functionality.
"; echo $cat1->meow();