I have a class that contains a bunch of averages, but if there is no data all the fields will be NULL
, and in the eyes of my logic, "empty". However, wrapping my class in if(empty($myClassInstance))
returns false, which is correct, but I was wondering if there is any magic methods in PHP where I could say "If this objected is checked for being empty, do some stuff and return a bool"?
I realise I can add a isEmpty()
method, I am just curious if this is something that can be done.
No.
From the manual:
The following things are considered to be empty:
"" (an empty string)
0 (0 as an integer)
0.0 (0 as a float)
"0" (0 as a string)
NULL
FALSE
array() (an empty array)
$var; (a variable declared, but without a value)