moring1 Posted May 18, 2017 Share Posted May 18, 2017 I read a book and the author explains that in PHP variables and properties are created on the fly so if we misspell a name, a new property will be created. So he recommends to set initial value to properties. I don't get how this recommendation helps? If I call a misspelled property it still will be created, even if I set a value to the original property, So I cant understand what is his point?"Program design recommendation—Properties are created on the fly in PHP. Properties are created the first time you use them. This can both be a help and a pain. If you misspell a property name, PHP will not produce an error. Instead it will create a new property with the misspelled name. It is recommended (when possible) that properties be created with initial values at the top of your program (or method, or class) to more easily determine what your property name is and whether it has been created." Quote Link to comment Share on other sites More sharing options...
Solution requinix Posted May 19, 2017 Solution Share Posted May 19, 2017 He's mostly correct but should have been more precise. If you try to assign to a variable (or property) that does not exist then PHP will (generally) create it for you no problem. In fact for variables this is how you're supposed to do it because there's no way to define them beforehand, unlike properties. <?php class Example { } $e = new Example(); $e->no_such_property = 123; var_dump($e); object(Example)#1 (1) { ["no_such_property"]=> int(123) }If you try to read from a variable (or property) that does not exist then PHP will issue a warning and pretend the value is null. <?php var_dump($no_such_variable); Notice: Undefined variable: no_such_variable in... NULL <?php class Example { } $e = new Example(); var_dump($e->no_such_property); Notice: Undefined property: Example::$no_such_property in... NULLIf you define the property in the class then that doesn't happen. <?php class Example { public $no_such_property; // defines the property with a default value of NULL } $e = new Example(); var_dump($e->no_such_property); NULL 1 Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted May 19, 2017 Share Posted May 19, 2017 Even in the cases where PHP itself doesn't care about correct property declarations, external tools like IDEs do care. For example, my IDE keeps track of all property declarations and immediately warns me whenever I create a property on the fly. So I can in fact recognize misspellings. At the same time, I get detailed information about the declared properties like the type, the location of the original definition, the usages etc., which is very helpful. The author's advice is still somewhat sloppily written, though: What matters is the declaration, not the initialization with a value. Properties are automatically initialized with default values, so there's no need to do that yourself -- unless you want a different value, of course. 1 Quote Link to comment Share on other sites More sharing options...
moring1 Posted May 19, 2017 Author Share Posted May 19, 2017 Even in the cases where PHP itself doesn't care about correct property declarations, external tools like IDEs do care. For example, my IDE keeps track of all property declarations and immediately warns me whenever I create a property on the fly. So I can in fact recognize misspellings. At the same time, I get detailed information about the declared properties like the type, the location of the original definition, the usages etc., which is very helpful. The author's advice is still somewhat sloppily written, though: What matters is the declaration, not the initialization with a value. Properties are automatically initialized with default values, so there's no need to do that yourself -- unless you want a different value, of course. I know, that's why I asked about the benefit of initialization as he recommends..thank you. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.