Doctrine: String keys with Array hydration
According to their documentation, you should use开发者_Go百科 Array hydration rather than record hydration when retrieving data for read-only purposes.
However, this means I have to access the attributes of the retrieved object using arrays and string keys:
$user['Phonenumbers'][0]['number']
instead of the OO style:
$user->PhoneNumbers[0]->number
Now I'm kinda new to PHP, but in other languages I've worked with the 2nd notation would be preferable because typos would be caught at compile time while typos in string literals would not be noticed until runtime. Does this apply to PHP/Doctrine?
Typos in both notations will NOT be caught at compile time. Typos in both notations will produce only PHP_NOTICE
and application will continue running unless you try to do some illegal operation (e.g. calling undefined method).
Consider misstyping 'Phonenumbers' to 'Phonenombers':
$number = $user['Phonenombers'][0]['number']; // Notice: Undefined index
$number = $user->PhoneNombers[0]->number; // Notice: Undefined property
Or in case of misstyping 'user' to 'userr':
$number = $userr->PhoneNombers[0]->number;
// Notice: Undefined variable
// Notice: Trying to get property of non-object
In all cases above the variable $number
will be set to NULL
.
On the other hand following code will cause PHP_ERROR
and script will stop executing
$userr->free(); //Fatal error: Call to undefined method
All examples above will produce PHP_NOTICE
or PHP_ERROR
respectively only when executed while script is running. For example the following will NOT produce any PHP_NOTICE
or PHP_ERROR
:
if(false) {
$userr->free();
}
So, finally, the answer is NO, this does not apply to PHP/Doctrine. If you don't need any methods of Doctrine_Collection
object, you should use array hydration instead, because it's faster and uses less memory.
精彩评论