Why doesn't empty() work in a constructor?

I tried to do something like this in the constructor within a class:

function __construct(Widget $widget)
{
	$this->widget = $widget;

	if (!empty($widget->optionA)) $this->option_a = $widget->optionA;
	if (!empty($widget->optionB)) $this->option_b = $widget->optionB;
}

I’ve discovered that the empty() function doesn’t seem to work inside a constructor, but not sure why. The properties optionA and optionB aren’t empty in the widget object I’m passing in, but it shows as empty inside the constructor.

Is there an alternative I should be using to test if a property in a passed object is empty or not?

I’d rather not have to do if ($widget->optionA != '')... because I like how !empty() works with either a string or an integer.

EDIT

I think that maybe the empty() function isn’t working because in the Widget object I am returning property values like this:

function __get($var)
{
	if (isset($this->prefs[$var])) {
		return $this->prefs[$var];
	} else {
		return null;
	}
}

The Widget class loads properties into a $prefs array, and then the above magic method returns the value from that array if it exists. Is that why empty() won’t work in the calling object? Is there a way around this? Thanks.

I think the more important question is why you’re copying this stuff to the class? Why not refer to them as $this->widget->optionA and $this->widget->optionB?

Copying them is a disaster waiting to happen in case you need to update it and only change one instead of two. I would strongly advice not to continue down this path.

1 Like

I think they are empty because they are being passed with empty values before actually being passed down to the widget constructor. Make sure that the values are actually being set or the arguments actually do have something in there. Also, I actually agree with what @rpkamp. You’re resetting the value if it doesn’t exist which isn’t ideal. You should make sure that the values actually are passed before you call the constructor.

1 Like

I’m not sure what you mean. I’m passing a Widget object into another object (let’s call it News) to provide the News object with information about the widget, specifically settings that could affect how the News object operates. The News object has default values for certain options, but the widget object has the ability to override those options with different values if they have been specified.

Why is this a disaster waiting to happen? I thought this was how you do it, you pass the object into another.

Maybe this will help clear some things up.

protected $option_a = "default value";
protected $option_b = 0; 

function __construct(Widget $widget)
{
	$this->widget = $widget;

	if (!empty($widget->optionA)) $this->option_a = $widget->optionA;
	if (!empty($widget->optionB)) $this->option_b = $widget->optionB;
}

What the Widget object does is override the default settings for the $option_a and $option_b properties if those properties are set in the Widget object. Those properties in the Widget are set by the user, they are preferences. But if the user doesn’t specify a preference, the default values are used in the above class.

I think it’s a pretty straightforward and a sound oop solution, but I get the feeling I’m doing something terrible, but I’m not sure what. Can you explain a better way to do the above?

Is there any reason why you can’t make the Widget class itself responsible for handling the default values of its own properties?

1 Like

Well now that you’ve mentioned it, no, there isn’t. I guess this is basically what @rpkamp mentioned above. Now it makes sense.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.