1
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 3 years have passed since last update.

[PHP] 親クラスで定義した変数を呼ぶ

Posted at

子クラス共通の変数を扱いたい場合、下記のように親クラスで定義する必要があります。

class TestCase extends Illuminate\Foundation\Testing\TestCase
{
    protected static $userId = 1;
}

このように定義した後、下記のように子クラスで呼び出します。

class ControllerTest extends \TestCase
{

    public function setUp(): void
    {
        $this->user = User::find(self::$userId);
    }
}

このようにself::$userIdと書くことにより、extendsしているクラスなら扱うことができます。

参照

【PHP】継承とprotectedと参照パターン

会社の紹介

私は現在、株式会社ダイアログという物流×ITの会社に勤務しております。
様々な職種を募集しているので、Wantedlyのページをご覧ください。

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