[Osaka/Yokohama] Looking for infrastructure/server side engineers!

[Osaka/Yokohama] Looking for infrastructure/server side engineers!

[Deployed by over 500 companies] AWS construction, operation, maintenance, and monitoring services

[Deployed by over 500 companies] AWS construction, operation, maintenance, and monitoring services

[Successor to CentOS] AlmaLinux OS server construction/migration service

[Successor to CentOS] AlmaLinux OS server construction/migration service

[For WordPress only] Cloud server “Web Speed”

[For WordPress only] Cloud server “Web Speed”

[Cheap] Website security automatic diagnosis “Quick Scanner”

[Cheap] Website security automatic diagnosis “Quick Scanner”

[Low cost] Wasabi object storage construction and operation service

[Low cost] Wasabi object storage construction and operation service

[Reservation system development] EDISONE customization development service

[Reservation system development] EDISONE customization development service

[Registration of 100 URLs is 0 yen] Website monitoring service “Appmill”

[Registration of 100 URLs is 0 yen] Website monitoring service “Appmill”

[Compatible with over 200 countries] Global eSIM “beSIM”

[Compatible with over 200 countries] Global eSIM “beSIM”

[Compatible with Chinese corporations] Chinese cloud / server construction, operation and maintenance

[Compatible with Chinese corporations] Chinese cloud / server construction, operation and maintenance

[YouTube] Beyond official channel “Biyomaru Channel”

[YouTube] Beyond official channel “Biyomaru Channel”

[FuelPHP 1.7] Autoloaderクラスを拡張する

PHP

WEBチームの日下部です。

FuelPHP 1.7 で Autoloader クラスを拡張する方法で悩んだので書き記します!

 

やりたいこと

\Fuel\Core\Autoloaderクラスを拡張し、Autoloaderクラスとして定義する

 

FuelPHPのコアクラスを拡張するやり方は

http://fuelphp.jp/docs/1.7/general/extending_core.html (FuelPHP 1.7)

ここに書かれている通りです。

拡張するのが Autoloader ではなく、例えば Response クラスなら簡単です。

class Response extends FuelCoreResponse { }

を app/classes/response.php として保存し、app/bootstrap.php のadd_classesにクラス名とファイルパスの対応を書き込めばokですね。

Autoloader::add_classes(array(
    'Response' => APPPATH.'classes/response.php',
));

ところがこれと同じ手順でAutoloaderを拡張しようとしてもうまくいきません。
なぜですか? Autoloaderだけは例外だからだ!

 

悩んだところ

さっきのドキュメントの一番下にこう書かれています。

Autoloader

The Autoloader class is a special case, you can only extend it once as [Autoloader] and have it used. After extending it you have to require it manually in the [app/bootstrap.php] file after the original [FuelCoreAutoloader], don't forget to remove the line that aliases the core class to global.

すなわちこういうことです。

 

  1. FuelCoreAutoloader を拡張した Autoloader クラスファイルを app/bootstrap.php 内で手動で require する
  2. remove the line that aliases the core class to global (???)

 

1つめはわかる! こうすればいい。

app/classes/autoloader.php を作成して、

class Autoloader extends FuelCoreAutoloader { }

app/bootatrap.php の冒頭に

require APPPATH.'classes'.DIRECTORY_SEPARATOR.'autoloader.php';

を記述。

 

2つめの意味がわからなかった! わからなかったので無視してページを開いてみると

Cannot redeclare class Autoloader

こっからしばらく悩みましたが解決しました。

結論から言うと、 public/index.php の50行目付近

class_alias('Fuel\Core\Autoloader', 'Autoloader');

これをコメントアウトすればokです。

 

この行の意味するところは

「グローバル名前空間の Autoloader から、名前空間 FuelCore 内の Autoloader へのエイリアスを張る」

です。(class_alias() については http://php.net/manual/function.class-alias.php を参照)

このエイリアスがあったために、

class Autoloader extends FuelCoreAutoloader { }

とすると、Autoloader を二重定義かつ自分自身を継承という事態になっていました。

 

 

まとめると、

コアクラスの Autoloader を拡張するときは

  1. app/classes/autoloader.php を作成
    class Autoloader extends FuelCoreAutoloader { }
  2. app/bootstrap.php の冒頭に
    require APPPATH.'classes'.DIRECTORY_SEPARATOR.'autoloader.php';

    を記述

  3. public/index.php (50行目付近)の
    class_alias('Fuel\Core\Autoloader', 'Autoloader');

    をコメントアウト

の3点を抑えればokです。お後がよろしいようで。

 

この記事がお役に立てば【 いいね 】のご協力をお願いいたします!
0
読み込み中...
0 票, 平均: 0.00 / 10
1,630
X facebook はてなブックマーク pocket
[2024.6.30 CentOS support ended] CentOS server migration solution

[2024.6.30 CentOS support ended] CentOS server migration solution

[2025.6.30 Amazon Linux 2 support ended] Amazon Linux server migration solution

[2025.6.30 Amazon Linux 2 support ended] Amazon Linux server migration solution

[Osaka/Yokohama] Actively recruiting infrastructure engineers and server side engineers!

[Osaka/Yokohama] Actively recruiting infrastructure engineers and server side engineers!

The person who wrote this article

About the author