LoginSignup
0
0

More than 1 year has passed since last update.

オブジェクトを連想配列にキャストする

Posted at

オブジェクトを配列にキャストすると、オブジェクトのプロパティに対して連想配列のようにアクセスすることができるようになる。

class MyClass {
    public    string $pub = 'public';
    protected string $pro = 'protected';
    private   string $pri = 'private';
}

$myClass = new MyClass();

// オブジェクトを配列にキャスト
$arrayMyClass = (array)$myClass;
echo $arrayMyClass['pub'];

出力結果

public

※クラス外部からアクセスできるのはpublicプロパティに限る、というルールは変わらないため、protectedおよびprivateのプロパティにアクセスすることはできません。

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