I’m wondering why the PHP language allows implicit property declaration when most languages need to define properties within the class declaration itself (see code below). Is there a practical use for this type of coding style?
$user1 = new User();
$user1->name = "Kylie";
echo $user1->name;
class User{}
The use-case are value objects
This allows to create some objects, that just carry around structured data (something like
structsin other languages), without the need to define a class for it.Another point is, that — because PHP is weakly-typed anyway — there is no real reason to forbid it. If you need something stronger, overweite
__set()