Is there a way to overload the empty() logic for custom classes on PHP?
for example:
class X implements Stringable {
private string $value;
public function __toString(): string {
return ''; # not even checking the values, always empty
}
# doesn't matter for the obj, only for protected properties, but
# will not cause error on the private here...
public function __isset(string $value): bool {
return false;
}
}
And a simple test
public function testXisEmpty(): void {
$actual = new X();
$this->assertTrue( empty($actual) );
}
✘ X is empty
┐
├ Failed asserting that false is true.
Is there any way to make empty()
respect the toString
value? or is there something like toBool
? or isset
for the object and not just a field?
Could not find anything on the manual but I also spend a lot of time looking at property overloads before i learned class overloads are called “magic” methods, so I might just be missing the right nomenclature…