0
0

Yii2フレームワークのエントリスクリプトを理解する。

Posted at

前回記事/var/www/html/humhub/index.phpの中身を理解する。

index.php
<?php

/**
 * @link https://www.humhub.org/
 * @copyright Copyright (c) 2017 HumHub GmbH & Co. KG
 * @license https://www.humhub.com/licences
 */

// comment out the following two lines when deployed to production
defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'dev');

require(__DIR__ . '/protected/vendor/autoload.php');
require(__DIR__ . '/protected/vendor/yiisoft/yii2/Yii.php');


$config = yii\helpers\ArrayHelper::merge(
    require(__DIR__ . '/protected/humhub/config/common.php'),
    require(__DIR__ . '/protected/humhub/config/web.php'),
    (is_readable(__DIR__ . '/protected/config/dynamic.php')) ? require(__DIR__ . '/protected/config/dynamic.php') : [],
    require(__DIR__ . '/protected/config/common.php'),
    require(__DIR__ . '/protected/config/web.php')
);

(new humhub\components\Application($config))->run();

これ↓は

index.php
defined('YII_DEBUG') or define('YII_DEBUG', true);

これ↓と同じ意味。

if (!defined('YII_DEBUG')) {
    define('YII_DEBUG', true);
}

index.php
require(__DIR__ . '/protected/vendor/autoload.php');
  • require()でファイルを読み込む。

index.php
$config = yii\helpers\ArrayHelper::merge(
  • ::はスコープ演算子。クラス::定数やメソッドでクラスの定数やメソッドにアクセスする。
  • merge()メソッドの戻り値を変数configに格納する。

参考:merge()の呼び出し先

/var/www/html/humhub/protected/vendor/yiisoft/yii2/helpers/BaseArrayHelper.php
static function BaseArrayHelper::merge(
    array $a,
    array $b
): array

functionの末尾の:は戻り値の型を定義している。

index.php
(new humhub\components\Application($config))->run();
  • Applicationクラスのコンストラクタに引数に変数configを渡して、インスタンスをnewする。
  • Applicationクラスのrun()メソッドを実行する。

参考:Applicationクラスの呼び出し先コンストラクタ

/var/www/html/humhub/protected/humhub/components/Application.php
    public function __construct($config = [])
    {
        // Remove obsolete config params:
        unset($config['components']['formatterApp']);

        parent::__construct($config);
    }
  • __が頭についているメソッドはマジックメソッドと呼ばれるPHPで予約済みのメソッド。
  • PHPでコンストラクタを定義するときは__construct()と定義する。
  • 関数( 引数 = 設定したいデフォルト値 )で引数のデフォルト値を定義する。
/var/www/html/humhub/protected/humhub/components/Application.php
unset($config['components']['formatterApp']);
  • unset()で配列の要素を削除する。
  • $array['key1']['key2']は、多次元連想配列の要素を1次元目と2次元目のキーを指定して削除する。
/var/www/html/humhub/protected/humhub/components/Application.php
parent::__construct($config);
  • 親クラスのコンストラクタを呼び出すお作法。
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