note 114568 added to function.get-class

From: Date: Thu, 06 Mar 2014 16:36:59 +0000
Subject: note 114568 added to function.get-class
Groups: php.notes 
Request: Send a blank email to php-notes+get-198820@lists.php.net to get a copy of this message
A lot of people in other comments wanting to get the classname without the namespace. Some weird suggestions of code to do that - not what I would've written! So wanted to add my own way. <?php function get_class_name($classname) { if ($pos = strrpos($classname, '\\')) return substr($classname, $pos + 1); return $pos; } ?> Also did some quick benchmarking, and strrpos() was the fastest too. Micro-optimisations = macro optimisations! 39.0954 ms - preg_match() 28.6305 ms - explode() + end() 20.3314 ms - strrpos() (For reference, here's the debug code used. c() is a benchmarking function that runs each closure run 10,000 times.) <?php c( function($class = 'a\b\C') { if (preg_match('/\\\\([\w]+)$/', $class, $matches)) return $matches[1]; return $class; }, function($class = 'a\b\C') { $bits = explode('\\', $class); return end($bits); }, function($class = 'a\b\C') { if ($pos = strrpos($class, '\\')) return substr($class, $pos + 1); return $pos; } ); ?> ---- Server IP: 87.117.229.81 Probable Submitter: 141.0.48.3 ---- Manual Page -- http://php.net/manual/en/function.get-class.php Edit -- https://master.php.net/note/edit/114568 Del: integrated -- https://master.php.net/note/delete/114568/integrated Del: useless -- https://master.php.net/note/delete/114568/useless Del: bad code -- https://master.php.net/note/delete/114568/bad+code Del: spam -- https://master.php.net/note/delete/114568/spam Del: non-english -- https://master.php.net/note/delete/114568/non-english Del: in docs -- https://master.php.net/note/delete/114568/in+docs Del: other reasons-- https://master.php.net/note/delete/114568 Reject -- https://master.php.net/note/reject/114568 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#198820) next »