note 101272 added to function.get-class

From: Date: Mon, 06 Dec 2010 16:14:04 +0000
Subject: note 101272 added to function.get-class
Groups: php.notes 
Request: Send a blank email to php-notes+get-174653@lists.php.net to get a copy of this message
Method for pulling the name of a class with namespaces pre-stripped. <?php /** * Returns the name of a class using get_class with the namespaces stripped. * This will not work inside a class scope as get_class() a workaround for * that is using get_class_name(get_class()); * * @param object|string $object Object or Class Name to retrieve name * @return string Name of class with namespaces stripped */ function get_class_name($object = null) { if (!is_object($object) && !is_string($object)) { return false; } $class = explode('\\', (is_string($object) ? $object : get_class($object))); return $class[count($class) - 1]; } ?> And for everyone for Unit Test goodiness! <?php namespace testme\here; class TestClass { public function test() { return get_class_name(get_class()); } } class GetClassNameTest extends \PHPUnit_Framework_TestCase { public function testGetClassName() { $class = new TestClass(); $std = new \stdClass(); $this->assertEquals('TestClass', get_class_name($class)); $this->assertEquals('stdClass', get_class_name($std)); $this->assertEquals('Test', get_class_name('Test')); $this->assertFalse(get_class_name(null)); $this->assertFalse(get_class_name(array())); $this->assertEquals('TestClass', $class->test()); } } ?> ---- Server IP: 69.147.83.197 Probable Submitter: 76.4.102.36 ---- Manual Page -- http://www.php.net/manual/en/function.get-class.php Edit -- https://master.php.net/note/edit/101272 Del: integrated -- https://master.php.net/note/delete/101272/integrated Del: useless -- https://master.php.net/note/delete/101272/useless Del: bad code -- https://master.php.net/note/delete/101272/bad+code Del: spam -- https://master.php.net/note/delete/101272/spam Del: non-english -- https://master.php.net/note/delete/101272/non-english Del: in docs -- https://master.php.net/note/delete/101272/in+docs Del: other reasons-- https://master.php.net/note/delete/101272 Reject -- https://master.php.net/note/reject/101272 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#174653) next »