Jump to content

What did he mean when he wrote this?


Go to solution Solved by requinix,

Recommended Posts

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."

Link to comment
https://forums.phpfreaks.com/topic/303959-what-did-he-mean-when-he-wrote-this/
Share on other sites

  • Solution

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...
NULL
If 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
  • Like 1

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.

  • Like 1

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.