Question

I a bit confused here. I need to throw an exception when a value of an object is not set, however it throws my exception regardless of if it is or is not set.

I run

var_dump(isset($this->{$idName}));
var_dump($this->{$idName});

and the results are

bool(false) 
string(1) "1" 

I would expect the first to be true. Am I missing something obvious?

To clairfy, I am trying to check if the property of the object that is stored in $idName is set. In this case $idName = "id"
So $this->id is what I'm checking.

$this->id will be retrieved from __get()

Was it helpful?

Solution

Looks like you're missing the magic __isset() method.

public function __isset($name)
{
    return isset($this->data[$name]);
}

Change according to where you store the data.

From the Manual:

__isset() is triggered by calling isset() or empty() on inaccessible properties.

OTHER TIPS

If this property is private/protected, and if you are trying to access its value directly from outside using $this->property syntax, make sure it's not just comming from your class __get() magic method.

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