Advertisement
Guest User

Untitled

a guest
Nov 24th, 2017
345
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.53 KB | None | 0 0
  1. If(get_class($object) == 'Name') {
  2. ... do this ...
  3. }
  4.  
  5. $reflect = new ReflectionClass($object);
  6. if ($reflect->getShortName() === 'Name') {
  7. // do this
  8. }
  9.  
  10. Reflection: 1.967512512207 s ClassA
  11. Basename: 2.6840535163879 s ClassA
  12. Explode: 2.6507515668869 s ClassA
  13.  
  14. namespace foobarbaz;
  15.  
  16. class ClassA{
  17. public function getClassExplode(){
  18. $c = array_pop(explode('\', get_class($this)));
  19. return $c;
  20. }
  21.  
  22. public function getClassReflection(){
  23. $c = (new ReflectionClass($this))->getShortName();
  24. return $c;
  25. }
  26.  
  27. public function getClassBasename(){
  28. $c = basename(str_replace('\', '/', get_class($this)));
  29. return $c;
  30. }
  31. }
  32.  
  33. $a = new ClassA();
  34. $num = 100000;
  35.  
  36. $rounds = 10;
  37. $res = array(
  38. "Reflection" => array(),
  39. "Basename" => array(),
  40. "Explode" => array(),
  41. );
  42.  
  43. for($r = 0; $r < $rounds; $r++){
  44.  
  45. $start = microtime(true);
  46. for($i = 0; $i < $num; $i++){
  47. $a->getClassReflection();
  48. }
  49. $end = microtime(true);
  50. $res["Reflection"][] = ($end-$start);
  51.  
  52. $start = microtime(true);
  53. for($i = 0; $i < $num; $i++){
  54. $a->getClassBasename();
  55. }
  56. $end = microtime(true);
  57. $res["Basename"][] = ($end-$start);
  58.  
  59. $start = microtime(true);
  60. for($i = 0; $i < $num; $i++){
  61. $a->getClassExplode();
  62. }
  63. $end = microtime(true);
  64. $res["Explode"][] = ($end-$start);
  65. }
  66.  
  67. echo "Reflection: ".array_sum($res["Reflection"])/count($res["Reflection"])." s ".$a->getClassReflection()."n";
  68. echo "Basename: ".array_sum($res["Basename"])/count($res["Basename"])." s ".$a->getClassBasename()."n";
  69. echo "Explode: ".array_sum($res["Explode"])/count($res["Explode"])." s ".$a->getClassExplode()."n";
  70.  
  71. $classNameWithNamespace=get_class($this);
  72. return substr($classNameWithNamespace, strrpos($classNameWithNamespace, '\')+1);
  73.  
  74. Reflection: 0.068084406852722 s ClassA
  75. Basename: 0.12301609516144 s ClassA
  76. Explode: 0.14073524475098 s ClassA
  77. Substring: 0.059865570068359 s ClassA
  78.  
  79. namespace foobarbaz;
  80. class ClassA{
  81. public function getClassExplode(){
  82. $c = array_pop(explode('\', get_class($this)));
  83. return $c;
  84. }
  85.  
  86. public function getClassReflection(){
  87. $c = (new ReflectionClass($this))->getShortName();
  88. return $c;
  89. }
  90.  
  91. public function getClassBasename(){
  92. $c = basename(str_replace('\', '/', get_class($this)));
  93. return $c;
  94. }
  95.  
  96. public function getClassSubstring(){
  97. $classNameWithNamespace = get_class($this);
  98. return substr($classNameWithNamespace, strrpos($classNameWithNamespace, '\')+1);
  99. }
  100. }
  101.  
  102. $a = new ClassA();
  103. $num = 100000;
  104.  
  105. $rounds = 10;
  106. $res = array(
  107. "Reflection" => array(),
  108. "Basename" => array(),
  109. "Explode" => array(),
  110. "Substring" => array()
  111. );
  112.  
  113. for($r = 0; $r < $rounds; $r++){
  114.  
  115. $start = microtime(true);
  116. for($i = 0; $i < $num; $i++){
  117. $a->getClassReflection();
  118. }
  119. $end = microtime(true);
  120. $res["Reflection"][] = ($end-$start);
  121.  
  122. $start = microtime(true);
  123. for($i = 0; $i < $num; $i++){
  124. $a->getClassBasename();
  125. }
  126. $end = microtime(true);
  127. $res["Basename"][] = ($end-$start);
  128.  
  129. $start = microtime(true);
  130. for($i = 0; $i < $num; $i++){
  131. $a->getClassExplode();
  132. }
  133. $end = microtime(true);
  134. $res["Explode"][] = ($end-$start);
  135.  
  136. $start = microtime(true);
  137. for($i = 0; $i < $num; $i++){
  138. $a->getClassSubstring();
  139. }
  140. $end = microtime(true);
  141. $res["Substring"][] = ($end-$start);
  142. }
  143.  
  144. echo "Reflection: ".array_sum($res["Reflection"])/count($res["Reflection"])." s ".$a->getClassReflection()."n";
  145. echo "Basename: ".array_sum($res["Basename"])/count($res["Basename"])." s ".$a->getClassBasename()."n";
  146. echo "Explode: ".array_sum($res["Explode"])/count($res["Explode"])." s ".$a->getClassExplode()."n";
  147. echo "Substring: ".array_sum($res["Substring"])/count($res["Substring"])." s ".$a->getClassSubstring()."n";
  148.  
  149. return substr(strrchr(get_class($this), '\'), 1);
  150.  
  151. Reflection: 0.073065280914307 s ClassA
  152. Basename: 0.12585079669952 s ClassA
  153. Explode: 0.14593172073364 s ClassA
  154. Substring: 0.060415267944336 s ClassA
  155. SubstringStrChr: 0.059880912303925 s ClassA
  156.  
  157. basename(str_replace('\', '/', get_class($object)));
  158.  
  159. echo (new ReflectionClass($obj))->getShortName();
  160.  
  161. namespace {
  162. trait Names {
  163. public static function getNamespace() {
  164. return implode('\', array_slice(explode('\', get_called_class()), 0, -1));
  165. }
  166.  
  167. public static function getBaseClassName() {
  168. return basename(str_replace('\', '/', get_called_class()));
  169. }
  170. }
  171. }
  172.  
  173. namespace xyz {
  174. class SomeClass {
  175. use Names;
  176. }
  177.  
  178. echo xyzSomeClass::getNamespace() . PHP_EOL; // xyz
  179. echo xyzSomeClass::getBaseClassName() . PHP_EOL; // SomeClass
  180. }
  181.  
  182. namespace def {
  183.  
  184. class DifferentClass extends xyzSomeClass {
  185.  
  186. }
  187.  
  188. echo defDifferentClass::getNamespace() . PHP_EOL; // def
  189. echo defDifferentClass::getBaseClassName() . PHP_EOL; // DifferentClass
  190. }
  191.  
  192. namespace {
  193.  
  194. class ClassWithoutNamespace {
  195. use Names;
  196. }
  197.  
  198. echo ClassWithoutNamespace::getNamespace() . PHP_EOL; // empty string
  199. echo ClassWithoutNamespace::getBaseClassName() . PHP_EOL; // ClassWithoutNamespace
  200. }
  201.  
  202. <?php
  203.  
  204. // usage anywhere
  205. // returns HelloWorld
  206. $name = class_basename('PathToYourClassHelloWorld');
  207.  
  208. // usage inside a class
  209. // returns HelloWorld
  210. $name = class_basename(__CLASS__);
  211.  
  212. $calledClass = get_called_class();
  213. $name = strpos($calledClass, '\') === false ?
  214. $calledClass : substr($calledClass, strrpos($calledClass, '\') + 1);
  215.  
  216. <?php
  217. namespace OneTwo {
  218. class foo
  219. {
  220. public function foo()
  221. {
  222. $calledClass = get_called_class();
  223. $name = strpos($calledClass, '\') === false ?
  224. $calledClass : substr($calledClass, strrpos($calledClass, '\') + 1);
  225.  
  226. var_dump($name);
  227. }
  228. }
  229. }
  230.  
  231. namespace Three {
  232. class bar extends OneTwofoo
  233. {
  234. public function bar()
  235. {
  236. $this->foo();
  237. }
  238. }
  239. }
  240.  
  241. namespace {
  242. (new OneTwofoo)->foo();
  243. (new Threebar)->bar();
  244. }
  245.  
  246. // test.php:11:string 'foo' (length=3)
  247. // test.php:11:string 'bar' (length=3)
  248.  
  249. yiihelpersStringHelper::basename(get_class($model));
  250.  
  251. This method is similar to the php function basename() except that it will
  252. treat both and / as directory separators, independent of the operating system.
  253. This method was mainly created to work on php namespaces. When working with real
  254. file paths, php's basename() should work fine for you. Note: this method is not
  255. aware of the actual filesystem, or path components such as "..".
  256.  
  257. trait ClassShortNameTrait
  258. {
  259. public static function getClassShortName()
  260. {
  261. if ($pos = strrchr(static::class, '\')) {
  262. return substr($pos, 1);
  263. } else {
  264. return static::class;
  265. }
  266. }
  267. }
  268.  
  269. namespace FooBarBaz;
  270.  
  271. class A
  272. {
  273. use ClassShortNameTrait;
  274. }
  275.  
  276. $bench = new xoriBenchmark(1000, 1000); # https://github.com/Xorifelse/php-benchmark-closure
  277. $shell = new myfancynamespaceclassname(); # Just an empty class named `classname` defined in the `myfancynamespace` namespace
  278.  
  279. $bench->register('strrpos', (function(){
  280. return substr(static::class, strrpos(static::class, '\') + 1);
  281. })->bindTo($shell));
  282.  
  283. $bench->register('safe strrpos', (function(){
  284. return substr(static::class, ($p = strrpos(static::class, '\')) !== false ? $p + 1 : 0);
  285. })->bindTo($shell));
  286.  
  287. $bench->register('strrchr', (function(){
  288. return substr(strrchr(static::class, '\'), 1);
  289. })->bindTo($shell));
  290.  
  291. $bench->register('reflection', (function(){
  292. return (new ReflectionClass($this))->getShortName();
  293. })->bindTo($shell));
  294.  
  295. $bench->register('reflection 2', (function($obj){
  296. return $obj->getShortName();
  297. })->bindTo($shell), new ReflectionClass($shell));
  298.  
  299. $bench->register('basename', (function(){
  300. return basename(str_replace('\', '/', static::class));
  301. })->bindTo($shell));
  302.  
  303. $bench->register('explode', (function(){
  304. $e = explode("\", static::class);
  305. return end($e);
  306. })->bindTo($shell));
  307.  
  308. $bench->register('slice', (function(){
  309. return join('',array_slice(explode('\', static::class), -1));
  310. })->bindTo($shell));
  311.  
  312. print_r($bench->start());
  313.  
  314. +-----------------+--------+
  315. | registered name | speed |
  316. +-----------------+--------+
  317. | reflection 2 | 70.75% |
  318. | strrpos | 60.38% |
  319. | safe strrpos | 57.69% |
  320. | strrchr | 54.88% |
  321. | explode | 46.60% |
  322. | slice | 37.02% |
  323. | reflection | 16.75% |
  324. | basename | 0.00% |
  325. +-----------------+--------+
  326.  
  327. function get_class_name($object = null)
  328. {
  329. if (!is_object($object) && !is_string($object)) {
  330. return false;
  331. }
  332.  
  333. $class = explode('\', (is_string($object) ? $object : get_class($object)));
  334. return $class[count($class) - 1];
  335. }
  336.  
  337. $baseClass = substr(strrchr(get_class($this), '\'), 1);
  338.  
  339. $baseClass = substr(strrchr('\'.get_class($this), '\'), 1);
  340.  
  341. <?php
  342.  
  343. if (basename(str_replace('\', '/', get_class($object))) == 'Name') {
  344. // ... do this ...
  345. }
  346.  
  347. <?php
  348.  
  349. namespace MyAwesomeNamespace;
  350.  
  351. class Foo {
  352.  
  353. private $shortName;
  354.  
  355. public function fastShortName() {
  356. if ($this->shortName === null) {
  357. $this->shortName = explode("\", static::class);
  358. $this->shortName = end($this->shortName);
  359. }
  360. return $this->shortName;
  361. }
  362.  
  363. public function shortName() {
  364. return basename(strtr(static::class, "\", "/"));
  365. }
  366.  
  367. }
  368.  
  369. echo (new Foo())->shortName(); // "Foo"
  370.  
  371. ?>
  372.  
  373. $shortClassName = join('',array_slice(explode('\', $longClassName), -1));
  374.  
  375. $base_class = preg_replace('/^([w\\]+\\)?([^\\]+)$/', '$2', get_class($myobject));
  376.  
  377. // both of the below calls will output: ShortClassName
  378.  
  379. echo preg_replace('/.*\\/', '', 'ShortClassName');
  380. echo preg_replace('/.*\\/', '', 'SomeNamespaceSomePathShortClassName');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement