How do you guarantee that you don't get a “member function call for non-object” fatal?
Fox example, I often have something like this in my templates: (which I find very convenient and readable):
<?php echo $object->getRelatedObject()->getProperty()->formatProperty() ?>
However, this will only work if each method returns an object of the correct class. But it is not always the case. A linked object may not be in the database, so it returns null, and you encounter a fatal error. Then you go over and manually check the return values:
<?php if (is_object($object->getRelatedObject()) && is_object($object->getRelatedObject()->getProperty())):
<?php echo $object->getRelatedObject()->getPreperty()->formatProperty() ?>
<?php endif; ?>
But this is not so readable. How do you solve this problem?
source
share