Consider the following code, in which the setter is deliberately broken due to a mundane programming error that I have made for real a few times in the past:
<?php
class TestClass {
private $testField;
function setField($newVal) {
$testField = $newVal;
// deliberately broken; should be `$this->testField = $newVal`
}
function getField() {
return $this->testField;
}
}
$testInstance = new TestClass();
$testInstance->setField("Hello world!");
// Actually prints nothing; getField() returns null
echo $testInstance->getField();
?>
The fact that I declared $testField
at the top of the class helps conceal that programming error from me. If I hadn't declared the field, then I would get something similar to the following warning printed to my error log upon calling this script, which would potentially be valuable to helping my debugging - especially if I were to make an error like this in a large and complicated real-world application:
PHP Notice: Undefined property: TestClass::$testField in /var/www/test.php on line 13
With the declaration, there is no warning.
Perhaps I'm missing something, but I'm aware of only two reasons to declare class fields in PHP: firstly, that the declarations act as documentation, and secondly, that without declarations one can't use the private
and protected
access modifiers, which are arguably useful. Since the latter argument doesn't apply to public fields - assigning to an undeclared field of an object makes it public - it seems to me that I ought to at least comment out all my public field declarations. The comments will provide the exact same documentation value, but I will benefit from warnings if I try to read an uninitialized field.
On further thought, though, it doesn't seem to make sense to stop there. Since in my experience trying to read an uninitialized field is a much more common cause of error than trying to inappropriately read or modify a private or protected field (I've done the former several times already in my short programming career, but never the latter), it looks to me like commenting out all field declarations - not just public ones - would be best practice.
What makes me hesitate is that I've never seen anybody else do it in their code. Why not? Is there a benefit to declaring class fields that I'm not aware of? Or can I modify PHP's configuration in some way to change the behavior of field declarations so that I can use real field declarations and still benefit from "Undefined property" warnings? Or is there anything else at all that I've missed in my analysis?
getField
to work you must first initialize the field. Might as well do that in the property declaration! – Feb 08 '13 at 19:04private
orprotected
field on an object in any language that had access modifiers. I would think this is quite rare? I viewprivate
/protected
modifiers as being primarily useful as documentation. If avoiding accidental access of private or protected properties is a concern, perhaps I should combine commenting out my declarations with adopting Python-style leading underscores on private field names to reduce the chance of mishap? – Mark Amery Feb 08 '13 at 19:06TRUE === $variable
to prevent yourself from accidentally assigning instead of comparing. – Michael Feb 08 '13 at 19:140
, ornull
, or""
) at declaration time provokes exactly the same error-concealing behaviour as declaring without assignment. – Mark Amery Feb 08 '13 at 19:19getField()
should throw an exception if the field is uninitialized, not a mere notice. – Feb 08 '13 at 19:37property_exists
returnsfalse
. – Mark Amery Feb 08 '13 at 19:43