Is this always safe or could it cause errors in some cases?
if(isset($myVar) && $myVar->myProp !== 'error') {
...
Yes, it will cause an error if the property doesn't exist. Check it exists with property_exists
$myVar = new myVar();
if( (isset($myVar) && property_exists('myVar', 'myProp'))
&& $myVar->myProp !== 'error' ) {
}
This seems to be a case of defining properties on the fly. While it's possible (and valid) to use property_exists()
, it would be much better to actually enforce the existance of the property in the class definition:
Class someExample {
public $myProp = false; // now it will ALWAYS exist for any instance of someExample
}