LoginSignup
4
4

More than 5 years have passed since last update.

new演算子が作用できるもの

Posted at

クラスとselfとstringが指定できることは知っていたけどオブジェクトが指定できるのは知らなかった。

  • クラス
  • self, parent, static(クラスのコンテキスト内のみ)
  • クラス名(string)
  • オブジェクト (PHP5.3以降)
<?php
class A {
}

$classname = 'A';

$a = new A; //Aオブジェクト1
$b = new $classname; //Aオブジェクト2
$c = new $a; //Aオブジェクト3

オブジェクトをnewすると、単に「オブジェクトのクラス名」をnewしたのと同じ挙動になります。
なお、オブジェクトに__toString()が実装されていても、単に無視されます。

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