The magic functions are certainly slower than anything else in PHP and should be used carefully. In fact, it will be a good blog subject (automatic creation of attributes using magic functions to speed up work ... anyway). As El Yobo stated , I modified your PHP script so that the tests are more accurate:
<?php
class A {
public function __get($a) {
return 5;
}
}
class B {
public $a = 5;
}
class C {
private $a = 5;
public function __get($a) {
return $this->a;
}
}
$classes = array('A','B','C');
header('Content-type: text/plain; charset=utf-8');
foreach ($classes as $className) {
$a = new $className;
$start = microtime(true);
for ($i=0; $i < 1000000; $i++) {
$b = $a->a;
}
$end = microtime(true);
echo 'Class ' . get_class($a) . ' = ' . (($end - $start) * 1000) ." msec\n";
}
Result
Class A = 378.85212898254 msec
Class B = 109.26413536072 msec
Class C = 423.51794242859 msec
, . , , , 4 , .
** EDIT **
, , , ( ). C :
class C {
public function __get($a) {
$this->a = 5;
return 5;
}
}
Class A = 392.09413528442 msec
Class B = 110.16988754272 msec
Class C = 96.771955490112 msec
, : ", !" , 1000000 10 ():
Class A = 0.033140182495117 msec
Class B = 0.0078678131103516 msec
Class C = 0.01215934753418 msec
C , B, . , PHP , . , , , , PHP .. , , , , .
** 2 **
D, :
class D {
public function __construct() {
$this->a = 5;
}
}
1000 :
Class A = 1.3999938964844 msec
Class B = 0.42200088500977 msec
Class C = 0.3960132598877 msec
Class D = 0.37002563476562 msec <-- faster
1 000 000:
Class A = 380.80310821533 msec
Class B = 109.7559928894 msec
Class C = 91.224908828735 msec <-- faster ???
Class D = 96.340894699097 msec
, : ,
public function __get($a) {
$this->a = 5;
return 5;
}
,
public function __construct() {
$this->a = 5;
}
?