LoginSignup
1
1

More than 5 years have passed since last update.

静的変数の動的呼出し

Last updated at Posted at 2018-05-21

TL;DR

下書きが埋まったので小ネタ解放。
動的にコード(可変変数)を呼び出したい。

setting.php
class Setting {
  public static $testNumber = array(
    "1" => "1",
    "3" => "2",
    "3" => "3",
    "4" => "4",
    "5" => "5",
  );
}

$className = "Setting";
$name = "test";
$myVarious = $name."Number";
$className::${$myVarious}[3];// 3

ことの詳細はあいまいさの回避。
$$myVarious[3]だと、$myVarious[3]が変数なのか$myVariousが変数で添え字が[3]なのかが判断できないのでエラーが起こる。
なのでそこを{}でくくり $myVariousが変数である事を教えてやる。

可変変数

1
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
1
1