Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

nolros's avatar
Level 23

PHP Question

Sorry if this is a dumb question, but in PHP can you use the returning value without casting it to a variable. Example,

INSTEAD OF THIS*

    public function isPermissionLevel( $permissionLevel )
    {
        $result = $this->isString($permissionLevel);

        if ($result instanceof PermissionLevelException)
        {
            throw $result;
        }
        
    }

CAN YOU DO SOMETHING LIKE THIS

    public function isPermissionLevel( $permissionLevel )
    {
        if ($this->isString($permissionLevel) instanceof PermissionLevelException)
        {
            throw // return value i.e. $this->isString($permissionLevel;
        }
    }

CALLING THIS METHOD

    public function isString( $string )
    {
        try
        {
            $string = new Stringy( $string );
            return (string) $string;
        }
        catch ( \ InvalidArgumentException $failed )
        {
            return( new PermissionLevelException( 'Not a valid string . ' . $failed->getMessage() . ' code . ' . $failed->getCode() ) );
        }
    }

Much appreciated

Nolan

0 likes
1 reply
michaeldyrynda's avatar

Is throwing the PermissionLevelException within the isString method an option?

If you don't catch it within isPermissionLevel it'll bubble up anyway.

try
{
    $string = new Stringy($string);
    return (string) $string;
}
catch ( \InvalidArgumentException $failed)
{
    throw new PermissionLevelException('Not a valid string ' . $failed->getMessage() . ' code  ' . $failed->getCode());
}

Please or to participate in this conversation.