php - A way to add Multiple object operator in a condition

93

In PHP, is there a way to correspond multiple object operator to check a certain condition

For example

if( !empty($this->user()->city && !empty($this->user()->name && !empty($this->user->phone) ) 

Here, I have to check condition for the city, name, phone and lot's of other properties, is there a way to add a shortcut like this

if(!empty($this->user()->{city,name,phone})
279

Answer

Solution:

I think the exact thing you're trying to do really isn't possible. The closest thing I can think of is to define an array of properties and check them in a loop.

$required = ['city', 'name', 'phone', 'lots', 'of', 'other', 'properties'];

$complete = true;
foreach ($required as $property) {
    if (empty($this->user()->$property)) {
        $complete = false;
        break;
    }
}

This can help to avoid a giant complexif condition like that; instead you just check

if ($complete) ...

after the loop.

It seems like the specific thing you're trying to do with this is entity validation. If you use one of the various PHP frameworks, there are utilities that can make this easier for you. You can just add arequired annotation to the property or something like that.

128

Answer

Solution:

As per my knowledge there is no function to test this you have to write your own custom function for example check given entries in array is blank or not (All entries send to it are mendatory)

function checkEmpty($arr)
{
  foreach($arr as $value)
  {
    if(empty(trim($value)))
    {
      return false;
    }
  }
  return true;
}
$arr = array('name'=>'nilesh','age'=>' ');
if(checkEmpty($arr))
 echo "true part here";
else
 echo 'False Part Here';
644

Answer

Solution:

There is no built-in for that sort of multiple-field check. Depending on the values in your variables and what you consider to be empty, you might be able to just concatenate all the fields together and check for the emptiness of that:

if (empty($user->city . $user->name . $user->phone)) {
    // all fields are empty
} else {
    // at least one field is not empty
}

... but that's rather... ugly.

and lot's of other properties,

Well, that's what loops are for. I'd simply write a method to verify that you have the required fields. Something like this maybe:

class User
{

    const REQUIRED_FIELDS = ['name', 'city', 'state'];

    public function hasRequiredFields(): bool
    {
        foreach (self::REQUIRED_FIELDS as $field) {
            if (empty($this->$field)) {
                return false;
            }
        }
        return true;
    }

}

Then you can just do:

if ($user->hasRequiredFields()) {
    ...
}

If your user object already has all the required fields defined as public properties, then you can just iterate over the object itself to get them:

class User
{

    public $name;
    public $city;
    public $state;

    public function hasRequiredFields(): bool
    {
        foreach ($this as $key => $value) {
            if (empty($value)) {
                return false;
            }
        }
        return true;
    }

}

People are also looking for solutions to the problem: php - WordPress Protected Page doesn't redirect after reCaptcha

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.