I have just learned that you can set any property of a class (as long as it's not explicitly declared as a private one). For example:
class Test {}
$test = new Test;
$test->randomthing = 'Hello world';
var_export($test);
Works just fine to set the property randomthing
of the $test
object.
I am trying to find out what this behaviour is called, and if it's possible to eliminate/disable it (to minimise errors in code), because as it currently stands, i can be typoing my desired properties and not notice in time.
Ideally, i would want to have:
class User {
protected $name;
}
And then i want to be able to set a $user->name = ''
, but not $user->randomthing = ''
. This second call would ideally throw an exception or something.
I have tried to guess this convention by looking up "dynamic properties", but that seems to refer to stuff like the double-dollar syntax to reference a property by a value in a variable.
So, what do you call this behaviour? And where can i read up more about controlling it (or disabling it)?