{"action":"created","issue":{"url":"https://api.github.com/repos/laravel/framework/issues/16662","repository_url":"https://api.github.com/repos/laravel/framework","labels_url":"https://api.github.com/repos/laravel/framework/issues/16662/labels{/name}","comments_url":"https://api.github.com/repos/laravel/framework/issues/16662/comments","events_url":"https://api.github.com/repos/laravel/framework/issues/16662/events","html_url":"https://github.com/laravel/framework/issues/16662","id":193514609,"number":16662,"title":"Use \"callable\" typehint instead of \"\\Closure\"","user":{"login":"foglcz","id":767839,"avatar_url":"https://avatars.githubusercontent.com/u/767839?v=3","gravatar_id":"","url":"https://api.github.com/users/foglcz","html_url":"https://github.com/foglcz","followers_url":"https://api.github.com/users/foglcz/followers","following_url":"https://api.github.com/users/foglcz/following{/other_user}","gists_url":"https://api.github.com/users/foglcz/gists{/gist_id}","starred_url":"https://api.github.com/users/foglcz/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/foglcz/subscriptions","organizations_url":"https://api.github.com/users/foglcz/orgs","repos_url":"https://api.github.com/users/foglcz/repos","events_url":"https://api.github.com/users/foglcz/events{/privacy}","received_events_url":"https://api.github.com/users/foglcz/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2016-12-05T14:39:27Z","updated_at":"2016-12-05T16:46:44Z","closed_at":null,"body":"- Laravel Version: 5.1, 5.2, 5.3, master\r\n- PHP Version: 5.6, 7.0, 7.1\r\n- Database Driver & Version: n/a\r\n\r\n### Description:\r\nReal world use-case. I'm building flexible models in Lumen framework, while using `Illuminate\\Database` - and I have a base model, which returns a query builder.\r\n\r\nThis query builder does some basic mapping between column names in database, and column names in entities, so I wanted to generate the schema from within the model itself:\r\n\r\n```php\r\n// SomeMigration.php\r\nuse App\\Domain\\Models;\r\nclass MyMigration extends Migration\r\n{\r\n    public function up()\r\n    {\r\n        Schema::create('some_table', [Models\\SomeModel::class, 'generateSchema']);\r\n    }\r\n}\r\n\r\n// App\\Domain\\Models\\SomeModel.php\r\nnamespace App\\Domain\\Models;\r\nclass SomeModel extends Base\r\n{\r\n    /**\r\n     * @param Blueprint $table\r\n     */\r\n    public static function buildSchema(Blueprint $table)\r\n    {\r\n        $table->charset = 'utf8mb4';\r\n        $table->collation = 'utf8mb4_general_ci';\r\n\r\n        $table->increments('id');\r\n        $table->integer('number');\r\n        $table->char('code', 4);\r\n        $table->char('type', 1);\r\n        $table->string('full_name');\r\n        $table->timestamps();\r\n\r\n        $table->unique(['number']);\r\n        $table->index(['code']);\r\n    }\r\n}\r\n```\r\n\r\nThis throws an exception:\r\n```                                                                                                                            \r\n  [Symfony\\Component\\Debug\\Exception\\FatalThrowableError]\r\n  Type error: Argument 2 passed to Illuminate\\Database\\Schema\\Builder::create() must be\r\n  an instance of Closure, array given, called in\r\n  /var/www/someproject/vendor/illuminate/support/Facades/Facade.php on line 237\r\n                                        \r\n```\r\n\r\n## Why it doesn't work\r\nIt doesn't work. because `\\Illuminate\\Database\\Schema\\Builder::create` expects type of `\\Closure`. However, as of PHP 5.4 you can use typehint `callable`. So the fix, in this case, would be to fix `illuminate/database/Schema/Builder.php`, **line 140**:\r\n\r\n```php\r\n    /**\r\n     * Create a new table on the schema.\r\n     *\r\n     * @param  string    $table\r\n     * @param  callable  $callback\r\n     * @return void\r\n     */\r\n    public function create($table, callable $callback) // <-- note the different typehint\r\n    {\r\n        $blueprint = $this->createBlueprint($table);\r\n\r\n        $blueprint->create();\r\n\r\n        $callback($blueprint);\r\n\r\n        $this->build($blueprint);\r\n    }\r\n```\r\n\r\nThis will allow the user to submit anything, that is invokable - whereas `\\Closure` **requires** anonymous function.\r\n\r\nsee https://secure.php.net/manual/en/functions.anonymous.php\r\n\r\n> Anonymous functions, also known as closures, allow the creation of functions which have no specified name. **They are most useful as the value of callback parameters**, but they have many other uses.\r\n\r\nCallback is a pre-5.4 pseudo type https://secure.php.net/manual/en/language.pseudo-types.php#language.types.callback\r\n\r\n5.4 introduces `callable` as a correct typehint.\r\n\r\nyay or nay?\r\n\r\n"},"comment":{"url":"https://api.github.com/repos/laravel/framework/issues/comments/264906780","html_url":"https://github.com/laravel/framework/issues/16662#issuecomment-264906780","issue_url":"https://api.github.com/repos/laravel/framework/issues/16662","id":264906780,"user":{"login":"vlakoff","id":544424,"avatar_url":"https://avatars.githubusercontent.com/u/544424?v=3","gravatar_id":"","url":"https://api.github.com/users/vlakoff","html_url":"https://github.com/vlakoff","followers_url":"https://api.github.com/users/vlakoff/followers","following_url":"https://api.github.com/users/vlakoff/following{/other_user}","gists_url":"https://api.github.com/users/vlakoff/gists{/gist_id}","starred_url":"https://api.github.com/users/vlakoff/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/vlakoff/subscriptions","organizations_url":"https://api.github.com/users/vlakoff/orgs","repos_url":"https://api.github.com/users/vlakoff/repos","events_url":"https://api.github.com/users/vlakoff/events{/privacy}","received_events_url":"https://api.github.com/users/vlakoff/received_events","type":"User","site_admin":false},"created_at":"2016-12-05T16:46:43Z","updated_at":"2016-12-05T16:46:43Z","body":"Refs some previous PRs : #10450, #9565,\r\nand the \"current\" discussion on internals: https://github.com/laravel/internals/issues/25."}}
{"actor":{"display_login":"vlakoff"}}