private function getType($var) { if (is_object($var)) { $refl = new \ReflectionObject($var); return $refl->getShortname(); } else { return gettype($var); } }
class MyClass { public function getShortName() { $reflector = new ReflectionObject($this); return $reflector->getShortName(); } } $obj = new MyClass(); echo $obj->getShortName(); // output: "MyClass"
class MyOtherClass { public function displayShortName() { $class = new ReflectionObject($this); echo $class->getShortName(); } } $obj = new MyOtherClass(); $obj->displayShortName(); // output: "MyOtherClass"In this example, we have a class named "MyOtherClass" which contains a method "displayShortName". Inside this method, we create a new ReflectionObject instance and then call the getShortName method on it to retrieve the short name of the MyOtherClass. Then, we simply echo the result. Package Library: The ReflectionObject class is part of the PHP core, so it does not belong to any package library.
private function getType($var) { if (is_object($var)) { $refl = new \ReflectionObject($var); return $refl->getShortname(); } else { return gettype($var); } }