Defaults using the ternary operator

It might come as obvious to many experienced PHP programmers (I’m not one of them) but it’s been a while I’ve gotten into the habit of using PHP ternary operator to set arguments to default values. Type hinting takes care of that for some types but leaves others, like scalar and strings, out in the cold.
If an approach based on exceptions is taken then if based checks are the solution but sometimes a more forgiving approach might be the best and there the ternary operator works marvels.
An example of the first approach might be

public function setSomeString($someString)
{
    if (!is_string($someString)){
        throw new BadArgumentException("Some string must be a string", 1);
    }
    if(!preg_match('/^foo\\.*baz$/', $someString)){
        throw new BadArgumentException("Some string must be like 'foo ... baz'", 2);
    }
    $this->someString = $someString;
}

while an example of the second approach might be

public function setSomeString($someString)
{
   $this->someString = (is_string($someString) and preg_match('/^foo\\.*baz$/', $someString)) ? $someString : 'default value';
}

I will use the first approach when dealing with developers (read “people that can make something out of an exception”) and will use the second approach when dealing with user input that might be disruptive of an application flow.

Probably related

I appreciate your input