Upgrade to Pro — share decks privately, control downloads, hide ads and more …

ファイル先頭の use の意味、説明できますか? 〜PHP の namespace と autoloading の関係を正しく理解しよう〜 / namespace and autoloading in php

ファイル先頭の 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
Tweet

More Decks by Shohei Okada

Other Decks in Programming

Transcript

  1. 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 目次(※時間は目安)
  2. 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 目次(※時間は目安)
  3. フレームワークを使っていると遭遇するコード 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
  4. 説明できますか? 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
  5. 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 目次(※時間は目安)
  6. そもそもこれはファイル名ではない 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
  7. <?php namespace App\Models; use Illuminate\Database\Eloquent\Model; class Flight extends Model {

    // } 名前空間 + クラス名 定義元
 引用元:https://laravel.com/docs/10.x/eloquent#eloquent-model-conventions
  8. <?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ファイル内で こういう書き方をしても 文法上問題ない
  9. <?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ファイル内で こういう書き方をしても 文法上問題ない
  10. <?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ファイル内で こういう書き方をしても 文法上問題ない
  11. 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
  12. 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);
  13. つまりこのコードは 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);
  14. こう書いているのと同じ // 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);
  15. 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
  16. 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 目次(※時間は目安)
  17. (再掲)そもそもこれはファイル名ではない 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
  18. • ファイルの自動読み込みは Composer が提供してくれ ている • vendor/autoload.php だけを読み込めばよい • フレームワークでは

    index.php にて vendor/autoload.php が読み込まれている なぜ他ファイルを読み込めるのか まとめ
  19. 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 目次(※時間は目安)
  20. <?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
  21. <?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
  22. <?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
  23. <?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
  24. • PHP-FIG という団体が PSR(PHP Standard Recommendation)を策定している • PSR-4 にて autoloading

    の仕様が定義されている • namespace prefix と base directory • Composer が PSR-4 にもとづく autoloading をサポー ト(実装)している どうファイル名が解決されるのか まとめ
  25. 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 目次(※時間は目安)
  26. まとめ • autoloading 機能を提供 • autoloading の 1 つの方法として PSR-4

    をサポート • PSR-4 の仕様を策定 (ファイル名と名前空間の対応) • 言語機能としての名前空間 • use 文による名前空間の エイリアス
  27. まとめ • autoloading 機能を提供 • autoloading の 1 つの方法として PSR-4

    をサポート • PSR-4 の仕様を策定 (ファイル名と名前空間の対応) • 言語機能としての名前空間 • use 文による名前空間の エイリアス
  28. まとめ • autoloading 機能を提供 • autoloading の 1 つの方法として PSR-4

    をサポート • PSR-4 の仕様を策定 (ファイル名と名前空間の対応) • 言語機能としての名前空間 • use 文による名前空間の エイリアス
  29. • use 文には他のファイルを読み込む働きはない • PSR-4 という autoloading についての仕様が 定められている •

    Composer は PSR-4 をサポート(実装)している • 各種フレームワークはインストールした時点で そのあたりの設定がすでに書かれている 全体 まとめ
  30. 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 目次(※時間は目安)
  31. 余談 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
  32. こう書いても同じだし 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
  33. あるいは 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
  34. こういう書き方もできる 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