简体   繁体   中英

How to do covert to this code block to php v7.4 or fix the syntax error

Currently I'm working with a laravel base ecommerce project and I got a domain mapping plugin which they provide. But the problem is it's only working on php8 or 8+, but my application runs on php v7.4.

So I wanted to convert this code block to php v7.4. There is no error while running using php v8 but run with v7.4 it gives a syntax error. Does anyone know how to fix it?

class Domain extends BaseModel
{
    use Crud;
    
   
    protected function url(): Attribute
    {
        return Attribute::make(
            get:function ($value) {
                // Remove current protocol
                $value = preg_replace('#http[^:]*://#ui', '', $this->host);
                
                // Get the right protocol
                $protocol = ($this->https == 1) ? 'https' : 'http';
                
                // Use the right protocol instead
                return $protocol . '://' . $value;
            },
        );
    }
    
    /*
    |--------------------------------------------------------------------------
    | OTHER PRIVATE METHODS
    |--------------------------------------------------------------------------
    */
}

Here is the error message:

syntax error, unexpected ':', expecting ')' In the: /extras/plugins/domainmapping/app/Models/Domain.php file at line: 203

To prevent the syntax error you can just remove the get:

ie

return Attribute::make(
        function ($value) {
            // Remove current protocol

..etc.

Named arguments like this were introduced in PHP 8 . They allow you to pass in values to a function by name instead of relying on the order you specify them in. It also makes it easy to skip optional parameters.

Since this function call has only one parameter specified anyway, it hopefully won't make any difference and will work correctly - unless there are optional parameters before it in the function definition. You need to check that definition before proceeding with this change.

Here's a runnable demo showing it not causing the syntax error in 7.4: https://3v4l.org/nOU3l

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM