Question

I have a php object that has a key=>value with something like [ipAddress] = 'NULL'

and if I do:

if(isset($object->ipAddress)){
     echo "I am set!!!";
}

It never echoes. because apparently it's not "Set." I was under them impression that it is set, because oft he word NULL.

Is there a away to get around this to say, you are set? with out actually giving it a value? I ask because I attempted to write a function like this: (Don't mind the debugging, its the debugging that lead me to this question)

private function checkForColumnInModelObject($modelObject, $column, $custom_name){
    $relationship = array();
    foreach($modelObject as $model){
        if(isset($model->$column)){
            $value_returned = $model->$column;
            var_dump($column);
            var_dump($custom_name);
            var_dump($value_returned);
            //$relationship[$custom_name] = $value_returned;
        }
    }

    //return $this->toObj($relationship);
}

So what I am trying to do here is check for a column in a model object. Now you might be given an array of columns, which we walk through in a function that calls this one, and an array of different model objects. were trying to see if the model object has that column.

So for example:

 Does equipmentModel have ipAddress Column? yes? fetch me the value.

and the way we do this is by saying "is the column on this model set". The problem is, we might have columns with NULL value ... hypothetically their set, their value is just null, but PHP's isset() is all like NO, you are not set.

Any ideas on how I could write this to keep the same logic, BUT allow values of null to pass through assuming that model has that particular column?

Was it helpful?

Solution

If you want to know if an object property exists, regardless of its value, you can use property_exists.

if (property_exists($model, $column) {
    ...
}

OTHER TIPS

isset returns true whenever you do an assignment to some variable. When you do $somevar=NULL; (In this case it is an assignment), if(isset($somevar) { echo "Inside"; } , The "Inside" will never print. Since NULL is never considered a value.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top