This error occurs whenever you attempt to access a PHP object that does not exist. For example:
If the variable $object is not an object, then we will receive the following error: Fatal error: Call to a member function getList() on a non-object on line […]
This can happen because of two reasons:
- The variable was never set.
- The variable exists, but it is not an object. i.e. It is an integer or a string.
In some cases, the instantiation of an object might be outside of your control. For whatever reason, you might not know if the object exists or not before you attempt to use it. In those cases, you’ll need to make use of the is_object function. An example:
Although the above piece of code will sort out the fatal error, it does not take into account the fact that the variable might not exist. i.e. We’re leaving ourselves open to nasty “Undefined Variable” notices. To fix this, we can use the isset function in conjuction with the is_object function.
The function isset checks to see if the variable exists or not. If it does exist, we then check to see if it is an object.
0 comments:
Post a Comment