Share
Embed
ファイル先頭の use の意味、説明できますか? 〜PHP の namespace と autoloading の関係を正しく理解しよう〜 / namespace and autoloading in php
2024/03/07~09 開催の「PHPerKaigi 2024」(https://phperkaigi.jp/2024/ )の発表資料です。
詳細:https://fortee.jp/phperkaigi-2024/proposal/bc88c7e7-0169-4299-a471-9adb1a2e5bcc
2024/03/10 追記:スライド中の「use 演算子」という表現を、適切な「use 文」という表現に修正しました。
Shohei Okada
March 09, 2024
Transcript
74 slides
-
ファイル先頭の use の意味、 説明できますか? 〜PHP の namespace とautoloading の 関係を正しく理解しよう〜
2024/03/09 PHPerKaigi 2024 @okashoi -
1. はじめに 00:00~02:00 2. use 文とは 02:00~08:00 3. なぜ他ファイルを読み込めるのか 08:00~11:30
4. どうファイル名が解決されるのか 11:30~17:00 5. まとめ 17:00~19:00 6. 自己紹介 19:00~20:00 目次(※時間は目安) -
1. はじめに 00:00~02:00 2. use 文とは 02:00~08:00 3. なぜ他ファイルを読み込めるのか 08:00~11:30
4. どうファイル名が解決されるのか 11:30~17:00 5. まとめ 17:00~19:00 6. 自己紹介 19:00~20:00 目次(※時間は目安) -
フレームワークを使っていると遭遇するコード use App\Models\Flight; // Retrieve a model by its primary
key... $flight = Flight::find(1); // Retrieve the first model matching the query constraints... $flight = Flight::where('active', 1)->first(); // Alternative to retrieving the first model matching the query constraints... $flight = Flight::firstWhere('active', 1); 引用元:https://laravel.com/docs/10.x/eloquent#retrieving-single-models -
説明できますか? use App\Models\Flight; // Retrieve a model by its primary
key... $flight = Flight::find(1); // Retrieve the first model matching the query constraints... $flight = Flight::where('active', 1)->first(); // Alternative to retrieving the first model matching the query constraints... $flight = Flight::firstWhere('active', 1); 引用元:https://laravel.com/docs/10.x/eloquent#retrieving-single-models -
1. はじめに 00:00~02:00 2. use 文とは 02:00~08:00 3. なぜ他ファイルを読み込めるのか 08:00~11:30
4. どうファイル名が解決されるのか 11:30~17:00 5. まとめ 17:00~19:00 6. 自己紹介 19:00~20:00 目次(※時間は目安) -
そもそもこれはファイル名ではない use App\Models\Flight; // Retrieve a model by its primary
key... $flight = Flight::find(1); // Retrieve the first model matching the query constraints... $flight = Flight::where('active', 1)->first(); // Alternative to retrieving the first model matching the query constraints... $flight = Flight::firstWhere('active', 1); 引用元:https://laravel.com/docs/10.x/eloquent#retrieving-single-models -
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; class Flight extends Model {
// } 名前空間 + クラス名 定義元 引用元:https://laravel.com/docs/10.x/eloquent#eloquent-model-conventions -
<?php namespace Models { class Flight { public const NAME
= 'flight model'; } } namespace Actions { class Flight { public const NAME = 'flight action'; } } namespace { use Models\Flight as FlightModel; use Actions\Flight as FlightAction; echo FlightModel::NAME . PHP_EOL; // flight model echo FlightAction::NAME . PHP_EOL; // flight action } 1ファイル内で こういう書き方をしても 文法上問題ない -
<?php namespace Models { class Flight { public const NAME
= 'flight model'; } } namespace Actions { class Flight { public const NAME = 'flight action'; } } namespace { use Models\Flight as FlightModel; use Actions\Flight as FlightAction; echo FlightModel::NAME . PHP_EOL; // flight model echo FlightAction::NAME . PHP_EOL; // flight action } 1ファイル内で こういう書き方をしても 文法上問題ない -
<?php namespace Models { class Flight { public const NAME
= 'flight model'; } } namespace Actions { class Flight { public const NAME = 'flight action'; } } namespace { use Models\Flight as FlightModel; use Actions\Flight as FlightAction; echo FlightModel::NAME . PHP_EOL; // flight model echo FlightAction::NAME . PHP_EOL; // flight action } 1ファイル内で こういう書き方をしても 文法上問題ない -
Flight → App\Models\Flight というエイリアス use App\Models\Flight; // Retrieve a model
by its primary key... $flight = Flight::find(1); // Retrieve the first model matching the query constraints... $flight = Flight::where('active', 1)->first(); // Alternative to retrieving the first model matching the query constraints... $flight = Flight::firstWhere('active', 1); 引用元:https://laravel.com/docs/10.x/eloquent#retrieving-single-models -
Flight → App\Models\Flight というエイリアス use App\Models\Flight as Flight; // Retrieve
a model by its primary key... $flight = Flight::find(1); // Retrieve the first model matching the query constraints... $flight = Flight::where('active', 1)->first(); // Alternative to retrieving the first model matching the query constraints... $flight = Flight::firstWhere('active', 1); -
つまりこのコードは use App\Models\Flight as Flight; // Retrieve a model by
its primary key... $flight = Flight::find(1); // Retrieve the first model matching the query constraints... $flight = Flight::where('active', 1)->first(); // Alternative to retrieving the first model matching the query constraints... $flight = Flight::firstWhere('active', 1); -
こう書いているのと同じ // Retrieve a model by its primary key... $flight
= \App\Models\Flight::find(1); // Retrieve the first model matching the query constraints... $flight = \App\Models\Flight::where('active', 1)->first(); // Alternative to retrieving the first model matching the query constraints... $flight = \App\Models\Flight::firstWhere('active', 1); -
use 文によって簡潔に書ける use App\Models\Flight; // Retrieve a model by its
primary key... $flight = Flight::find(1); // Retrieve the first model matching the query constraints... $flight = Flight::where('active', 1)->first(); // Alternative to retrieving the first model matching the query constraints... $flight = Flight::firstWhere('active', 1); 引用元:https://laravel.com/docs/10.x/eloquent#retrieving-single-models -
• use 文に与えているのはファイルパスではない • 名前空間 + クラス名 • 本来、名前空間とファイルパスは独立した概念 •
use 文によってエイリアスが作成できる use 文 まとめ -
1. はじめに 00:00~02:00 2. use 文とは 02:00~08:00 3. なぜ他ファイルを読み込めるのか 08:00~11:30
4. どうファイル名が解決されるのか 11:30~17:00 5. まとめ 17:00~19:00 6. 自己紹介 19:00~20:00 目次(※時間は目安) -
(再掲)そもそもこれはファイル名ではない use App\Models\Flight; // Retrieve a model by its primary
key... $flight = Flight::find(1); // Retrieve the first model matching the query constraints... $flight = Flight::where('active', 1)->first(); // Alternative to retrieving the first model matching the query constraints... $flight = Flight::firstWhere('active', 1); 引用元:https://laravel.com/docs/10.x/eloquent#retrieving-single-models -
• ファイルの自動読み込みは Composer が提供してくれ ている • vendor/autoload.php だけを読み込めばよい • フレームワークでは
index.php にて vendor/autoload.php が読み込まれている なぜ他ファイルを読み込めるのか まとめ -
1. はじめに 00:00~02:00 2. use 文とは 02:00~08:00 3. なぜ他ファイルを読み込めるのか 08:00~11:30
4. どうファイル名が解決されるのか 11:30~17:00 5. まとめ 17:00~19:00 6. 自己紹介 19:00~20:00 目次(※時間は目安) -
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; class Flight extends Model {
// } 例)Laravel の Model app/Models/Flight.php 引用元:https://laravel.com/docs/10.x/eloquent#eloquent-model-conventions -
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; class Flight extends Model {
// } 例)Laravel の Model app/Models/Flight.php 引用元:https://laravel.com/docs/10.x/eloquent#eloquent-model-conventions namespace prefix base directory -
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; class Flight extends Model {
// } 例)Laravel の Model app/Models/Flight.php 引用元:https://laravel.com/docs/10.x/eloquent#eloquent-model-conventions -
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; class Flight extends Model {
// } 例)Laravel の Model app/Models/Flight.php 引用元:https://laravel.com/docs/10.x/eloquent#eloquent-model-conventions -
• PHP-FIG という団体が PSR(PHP Standard Recommendation)を策定している • PSR-4 にて autoloading
の仕様が定義されている • namespace prefix と base directory • Composer が PSR-4 にもとづく autoloading をサポー ト(実装)している どうファイル名が解決されるのか まとめ -
1. はじめに 00:00~02:00 2. use 文とは 02:00~08:00 3. なぜ他ファイルを読み込めるのか 08:00~11:30
4. どうファイル名が解決されるのか 11:30~17:00 5. まとめ 17:00~19:00 6. 自己紹介 19:00~20:00 目次(※時間は目安) -
まとめ • autoloading 機能を提供 • autoloading の 1 つの方法として PSR-4
をサポート • PSR-4 の仕様を策定 (ファイル名と名前空間の対応) • 言語機能としての名前空間 • use 文による名前空間の エイリアス -
まとめ • autoloading 機能を提供 • autoloading の 1 つの方法として PSR-4
をサポート • PSR-4 の仕様を策定 (ファイル名と名前空間の対応) • 言語機能としての名前空間 • use 文による名前空間の エイリアス -
まとめ • autoloading 機能を提供 • autoloading の 1 つの方法として PSR-4
をサポート • PSR-4 の仕様を策定 (ファイル名と名前空間の対応) • 言語機能としての名前空間 • use 文による名前空間の エイリアス -
• use 文には他のファイルを読み込む働きはない • PSR-4 という autoloading についての仕様が 定められている •
Composer は PSR-4 をサポート(実装)している • 各種フレームワークはインストールした時点で そのあたりの設定がすでに書かれている 全体 まとめ -
1. はじめに 00:00~02:00 2. use 文とは 02:00~08:00 3. なぜ他ファイルを読み込めるのか 08:00~11:30
4. どうファイル名が解決されるのか 11:30~17:00 5. まとめ 17:00~19:00 6. 自己紹介 19:00~20:00 目次(※時間は目安) -
余談 use App\Models\Flight as Flight; // Retrieve a model by
its primary key... $flight = Flight::find(1); // Retrieve the first model matching the query constraints... $flight = Flight::where('active', 1)->first(); // Alternative to retrieving the first model matching the query constraints... $flight = Flight::firstWhere('active', 1); 引用元:https://laravel.com/docs/10.x/eloquent#retrieving-single-models -
こう書いても同じだし use App\Models\Flight as FlightModel; // Retrieve a model by
its primary key... $flight = FlightModel::find(1); // Retrieve the first model matching the query constraints... $flight = FlightModel::where('active', 1)->first(); // Alternative to retrieving the first model matching the query constraints... $flight = FlightModel::firstWhere('active', 1); 引用元:https://laravel.com/docs/10.x/eloquent#retrieving-single-models -
あるいは use App\Models\Flight as Flight; // Retrieve a model by
its primary key... $flight = Flight::find(1); // Retrieve the first model matching the query constraints... $flight = Flight::where('active', 1)->first(); // Alternative to retrieving the first model matching the query constraints... $flight = Flight::firstWhere('active', 1); 引用元:https://laravel.com/docs/10.x/eloquent#retrieving-single-models -
こういう書き方もできる use App\Models; // Retrieve a model by its primary
key... $flight = Models\Flight::find(1); // Retrieve the first model matching the query constraints... $flight = Models\Flight::where('active', 1)->first(); // Alternative to retrieving the first model matching the query constraints... $flight = Models\Flight::firstWhere('active', 1); 引用元:https://laravel.com/docs/10.x/eloquent#retrieving-single-models