octane: Swoole + `defer()`, function already exists.

Laravel Version

11.23.4

PHP Version

8.3.10

Database Driver & Version

mysql5.7:latest docker

Description

Laravel Sail v1.32.0.

When using the Cache::flexible() method I get the following error:

defer() expects exactly 1 argument, 2 given at /var/www/html/vendor/laravel/framework/src/Illuminate/Cache/Repository.php:521)

I noticed this error only happens in the browser not the CLI - which I usually run form my host, not sail/docker. So I tried it in the sail container - same error.

So dumping a reflection of the function reveals:

$reflFunc = new ReflectionFunction('defer');
dd($reflFunc);

1 ReflectionFunction {#4 ▼
  +name: "defer"
  returnType: "void"
  parameters: {▼
    $callback: ReflectionParameter {#19 ▼
      +name: "callback"
      position: 0
      typeHint: "callable"
    }
  }
  extra: {▼
    extension: "swoole"
    isInternal: true
  }
}

Printing phpinfo() shows that Sail has Swoole installed. and I assume from this Swoole adds a global defer() function, so the Lavavel helper funciton is never defined - and would error if it did - when Swoole is installed.

Steps To Reproduce

Create a new Laravel 11.23+ project using Laravel Sail (or install Swoole).

run a controller or tinker:

// call it twice
Cache::flexible('test', [1, 5], fn () => 'test');
Cache::flexible('test', [1, 5], fn () => 'test');

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 16 (4 by maintainers)

Most upvoted comments

defer is a short named function in swoole that you can turn off via php.ini.

swoole.use_shortname=off

I have no plans to rename defer at this time. Swoole is used by a very small portion of the Laravel user base so I’d like to find alternative solutions.

So why install it with Sail?

Hello,

I use Forge Laravel Octane is disabled

I’m trying to use Cache::flexible()

And get this error:

defer() expects exactly 1 argument, 2 given {"exception":"[object] (ArgumentCountError(code: 0): defer() expects exactly 1 argument, 2 given 

I assume that Forge pre-installs Swoole (so that it is always ready to be turned on) … and as a result there is a naming conflict

@marco-silvestri Swoole is included in sail, I’ve managed to fix it by doing the following

  • create a file in your project ./docker/php/sail-php.ini
  • paste this into the file
swoole.use_shortname=off
  • map it to the container in docker-compose.yml
       volumes:
            - '.:/var/www/html'
            - './docker/php/sail-php.ini:/etc/php/8.3/cli/conf.d/99-sail.ini'
  • shut sail down and run it again, it should be fixed then
sail down
sail up -d

Running into this issue on an upgraded Laravel 11 project with Sail, where Swoole is clearly installed by default:

/docker/8.3/Dockerfile:33-34 && pecl install swoole-5.1.2 \ && echo "extension=swoole.so" > /etc/php/8.3/cli/conf.d/20-swoole.ini \

I believe the defer function name is already registered with the Swoole extension, which could cause conflicts with existing projects that rely on Swoole’s defer function or have the Swoole extension enabled. Unfortunately, I think Laravel should consider renaming the defer function to avoid these conflicts.