0
0

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 1 year has passed since last update.

PHP 複数階層にある特定のファイルのみをrequire_onceする

Last updated at Posted at 2022-06-30

名前空間構成

composer.json
{
  "require": {
    <省略>
  },
  "autoload": {
    "psr-4":{ "App\\": "root/" }
  }
}

ディレクトリ構造

root/
  ├─ functions/
  │       ├─ func1.php
  │       └─ nest/
  │            └─ func2.php
  └─ Main.php

関数ファイル

func1.php
namespace App\functions;

function func1() {
    echo "func1" . PHP_EOL;
}
func2.php
namespace App\functions\nest;

function func2() {
    echo "func2" . PHP_EOL;
}

読み込み

Main.php
namespace App;

use function App\functions\func1;
use function App\functions\nest\func2;

array_map(fn($v) => require_once $v, glob(__DIR__ . "/functions/{*.php,nest/*.php}", GLOB_BRACE));

func1();
func2();
実行結果
func1
func2

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?