2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【オートローダー】別PHPファイル呼び出し機能の実装

Posted at

今北産業

  • 呼び出しの基本はrequire_once
  • require_once+spl_autoload_register()で自動で呼び出せるようにしよう
  • ___autoload()はもう非推奨なのでこれからはspl_autoload_register()を使おう

呼び出しの基本はrequire_once

結論から言うとrequire_onceを使うべきです。
理由は下記で説明します。

PHPで別PHPファイルを呼び出す時には以下のように4種類やり方があります。

呼び出しに使えるfunction
<?php
include();
include_once();
require();
require_once();

include系とrequire系の違い

include系とrequire系の違いは呼び出し失敗時の挙動にあります。
include系は呼び出し失敗してもWarningを出すだけでプログラムの実行は継続されます。
一方require系は呼び出しに失敗したらその時点でFatal errorで落ちます。

別PHPファイルの呼び出しが失敗する場合、基本的にはそれ以降のプログラムの実行が正常に行われないので、失敗時はその場で落ちるrequire系を利用するのが望ましいです。

requireとrequire_once(includeとinclude_once)の違い

結論から言うと既に同じファイルが呼び出されていた場合に、何回でも呼び出せるのがrequire(include)、
呼び出せないのがrequire_once(include_once)です。

同じファイルを複数回呼び出した場合、値が上書きされるなどの不具合がでる可能性があるので、特別理由がないのであれば、require_once(include_once)を使う方が望ましいです。

spl_autoload_register()で自動で呼び出せるようにしよう

require_onceを使うべきなのは分かったが、例えば10個の外部ファイルを呼び出す場合は以下のようになります。

<?php
require_once('hoge1.php');
require_once('hoge2.php');
require_once('hoge3.php');
require_once('hoge4.php');
require_once('hoge5.php');
require_once('hoge6.php');
require_once('hoge7.php');
require_once('hoge8.php');
require_once('hoge9.php');
require_once('hoge10.php');
//ここ以降に処理を書いていく

一つ一つのファイルに毎回以上のように書くのは可読性・保守性などあらゆる面でデメリットが多いです。
この問題を解決するのがオートローダー機能です。

オートローダーとは

その名の通り、"自動で"+"呼び出し"をする機能。
PHPにおいてはマジックメソッドの__autoload()が実行されることでこの機能が達成されます。
__autoload()は外部ファイル呼び出しに失敗した際に、自動で呼び出されるメソッドで、例えば以下のように実装します。

<?php

function __autoload($name)
{
  $file = $name . ".php";
  require($file);
}

$Hoge = new Hoge(); //ここで__autoload()が起動し、今回はHoge.phpが呼ばれることになる

ただし、__autoload()を直接呼び出すのはPHP7.2以降でdeprecated(非推奨)となるので、spl_autoload_register()でオートローダーを実装しましょう。

image.png

__autoload()を直接定義した場合、一つのファイル・ディレクトリ関係にしか対応できませんが、ライブラリを入れたりすると様々な関係がありえます。複数パターンに対応できるのがspl_autoload_register()となります。

例えば以下のように実装できます。

HogeAutoload.php(大文字命名を解決するautoload)
<?php

class HogeAutoload
{
  public function autoloadHoge($name)
  {
    $file = strtoupper($name) . ".php";
    require($file);
  }
}

FugaAutoload.php(パスカルケース命名を解決するautoload)
<?php

class FugaAutoload
{
  public function autoloadFuga($name)
  {
    $file = ucfirst(strtolower($name)) . ".php";
    require($file);
  }
}
index.php
<?php
//ここはrequire_onceする必要あり
require_once('HogeAutoload.php');
require_once('FugaAutoload.php');

//ここでパターンを登録 以降下記2パターンで解決できる場合はrequire_onceは不要となる
spl_autoload_register(array('HogeAutoload','autoloadHoge'));
spl_autoload_register(array('FugaAutoload','autoloadFuga'));
2
1
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
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?