LoginSignup
0
0

More than 5 years have passed since last update.

PHPのauto load について

Last updated at Posted at 2019-04-28

オートロードとは

ファイルを自動で読み込む仕組み。

PHPではプログラム内で外部ファイルを読み込む場合、通常「require」を使用する。

その結果、「Class」を記述したファイルを複数読み込みたい場合、何度も「require」を記述しなければならない。

require "Authenticate.php"
require "Session.php"
require "User.php"

これを解決するのが、オートロードという機能。

クラスからインスタンスが生成される前のタイミングで、そのクラス名と同じファイルがあればrequire を実行しそれを読み込む。

spl_autoload_register

<?php
function spl_autoload_register( $class ) {
    require_once $class . '.php';
});

$user = new Session();
$article = new Article();

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