PHP クラスを参照するための組み込みの静的メソッドまたはプロパティがあり、文脈上文字列として表されますか? 例えば:
これの代わりに:
$obj->tempFn('MyClass') //MyClass being the name of the class
私はこれをしたい:
$obj->tempFn(MyClass) //Directly references the class name, instead of a string representation
いいえ。ただし、次のように、クラス名を含む定数をクラスで定義できます。
class foo{
const
NAME = 'foo';
}
のようにアクセスしますfoo::NAME
。
PHP 5.5では、以下を使用できます。
foo::class
echo get_class($this);
クラス内で動作する必要があります。
echo __CLASS__;
これは静的なプロパティだと思います
静的を本当に避けたい場合は、Reflection クラスが機能すると思います。
function getClassName(ReflectionParameter $param) {
preg_match('/\[\s\<\w+?>\s([\w]+)/s', $param->__toString(), $matches);
return isset($matches[1]) ? $matches[1] : null;
}
これはhttp://www.php.net/manual/en/reflectionparameter.getclass.phpのコメントからのものです