PHP: isset () returns false for dynamic properties of an object

I have a hierarchy of classes that are initialized from a database. I use __get () and __set () with an array in the common base class Item to support different numbers of properties. One class derived from Item, UserProfile is used to store user data. My database has a table of phone numbers with a one-to-many relationship to the user table to store multiple phone numbers for one user. If there is a mobile phone, the UserProfile object has a field for mobile devices. The same goes for home and business. If the number does not exist, the property does not exist.

The problem occurs when I want to check for the presence of these properties. when i use code like

if (isset($clientData->home_phone)) { $this->startElement('phone'); $this->writeAttribute('phone_number_type','home'); $this->writeRaw($clientData->home_phone); $this->endElement(); } 

The function always returns false. I tried using brackets to tell php to get the variable first, but I just got errors. Do I need a special attributeExists () function to simulate the isset () function, or is there a simpler approach?

How can I check for a dynamic property without any error?

+4
source share
2 answers

__ isset () is triggered by isset () or empty () call for inaccessible properties.

You can define the magic __isset() method to do this. See here .

+3
source

All Articles