Advertisement
Guest User

PHP 7.1 - Accessing properties

a guest
Jun 1st, 2017
379
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. PHP 7.1
  2.  
  3. Magic methods:
  4.  
  5. php -d implicit_flush=off -r 'class dog {private $name = ""; public function __get($property) { if (property_exists($this, $property)) { return $this->$property; } } public function __set($property, $value) { if (property_exists($this, $property)) { $this->$property = $value; } } } $rover = new dog(); for ($x=0; $x<10; $x++) { $t = microtime(true); for ($i=0; $i<1000000; $i++) { $rover->name = "rover"; $n = $rover->name;} echo microtime(true) - $t;echo "\n";}'
  6. 0.57930207252502
  7. 0.5767650604248
  8. 0.5775511264801
  9. 0.5777759552002
  10. 0.57945394515991
  11. 0.57729411125183
  12. 0.57919216156006
  13. 0.57855105400085
  14. 0.579097032547
  15. 0.5776948928833
  16.  
  17. Getter and Setter:
  18.  
  19. php -d implicit_flush=off -r 'class dog {private $name = "";public function setName($name) {$this->name = $name; }public function getName() {return $this->name; } }$rover = new dog();for ($x=0; $x<10; $x++) { $t = microtime(true);for ($i=0; $i<1000000; $i++) { $rover->setName("rover");$n = $rover->getName();}echo microtime(true) - $t;echo "\n";}'
  20. 0.13011598587036
  21. 0.13124799728394
  22. 0.13241600990295
  23. 0.1318941116333
  24. 0.13171601295471
  25. 0.13296389579773
  26. 0.13102602958679
  27. 0.13133406639099
  28. 0.13163495063782
  29. 0.13129711151123
  30.  
  31. Accessing property directly:
  32.  
  33. php -d implicit_flush=off -r 'class dog { public $name = "";} $rover = new dog(); for ($x=0; $x<10; $x++) { $t = microtime(true); for ($i=0; $i<1000000; $i++) { $rover->name = "rover"; $n = $rover->name;} echo microtime(true) - $t;echo "\n";}'
  34. 0.051743984222412
  35. 0.051209926605225
  36. 0.051720142364502
  37. 0.050024032592773
  38. 0.049885988235474
  39. 0.051983118057251
  40. 0.049699068069458
  41. 0.050537824630737
  42. 0.050158023834229
  43. 0.049751996994019
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement