In PHP, how do I create objects with strong typing?

In PHP, you can create objects with strong typing using the class declaration and type hints. This allows you to specify the types of properties and method parameters, ensuring that they only accept certain data types.

Keywords: PHP, strong typing, objects, type hints, classes
Description: This example demonstrates how to create PHP objects with strong typing using class definitions and type hints.

class Person {
    private string $name;
    private int $age;

    public function __construct(string $name, int $age) {
        $this->name = $name;
        $this->age = $age;
    }

    public function getName(): string {
        return $this->name;
    }

    public function getAge(): int {
        return $this->age;
    }
}

// Creating a new Person object
$person = new Person("John Doe", 30);
echo "Name: " . $person->getName(); // Output: Name: John Doe
echo "Age: " . $person->getAge(); // Output: Age: 30

Keywords: PHP strong typing objects type hints classes