0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

PHP 8.0 で FuelPHP の DB migrate が実行できない

Last updated at Posted at 2025-01-30

結論: 解決策

PHP 8.0 で FuelPHP の DB migrate を使いたい時は、メソッドに static を付ける

詳細

昨今のPHPフレームワークと言えば Laravel が人気ですが、
FuelPHP を使ったシステムに触れる機会があり、

Laravel でいうところの $ php artisan migrate みたいな事ができないかなぁと思い、

公式サイト http://fuelphp.jp/docs/1.9/general/migrations.html を見て
下記のようなコードをコピペ、
いざ $ FUEL_ENV=development php oil refine migrate:current

fuel/app/migrations/001_example.php
<?php

namespace Fuel\Migrations;

class Example
{

    function up()
    {
        \DBUtil::create_table('posts', array(
            'id' => array('type' => 'int', 'constraint' => 5),
            'title' => array('type' => 'varchar', 'constraint' => 100),
            'body' => array('type' => 'text'),
        ), array('id'));
    }

    function down()
    {
       \DBUtil::drop_table('posts');
    }
}

... ところが、どっこい。

Uncaught exception Fuel\Core\FuelException: 0 -
 Migration class "default" must include public methods "up" and "down"
 in /xxx/fuel/core/classes/migrate.php on line xxx

というエラーが発生... why?

問題の fuel/core/classes/migrate.php を読んだところ、

if ( ! is_callable(array($class, 'up')) or ! is_callable(array($class, 'down')))

is_callable で false が返ってきてるのね...?なぜ?

原因

ズバリこれ → https://www.php.net/manual/ja/function.is-callable.php#126199

PHP 8.0 以降は非静的メソッドで定義した場合は is_callable は動作しない と。

public function ではダメなんですよ... public static function じゃなきゃね...

という事で、公式サイトのサンプルコードを下記に変更して無事 migrate できました。

- function up()
+ public static function up()

- function down()
+ public static function down()

ちなみに

マイグレーションコマンドは $ php oil refine migrate という記事や、
公式ドキュメントにもそのような記載がありますが、

下記のように、 FUEL_ENV=(development|staging|production) を付けないと危険です。
$ FUEL_ENV=development php oil refine migrate:current


以上、現場からお伝えしました。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?