對如下使用魔術方法和不適用魔術方法運行時間進行比較php
使用魔術方法test1.php:性能
<?php /** * 測試類 */ class test { private $name = "jepeng"; public function __get($varname) { return $this->name; } } $i = 0; while ( $i<= 10000) { $i++; $test = new test(); $test->name; }
不使用魔術方法test2.php測試
<?php /** * 測試類 */ class test { public $name = "jepeng"; } $i = 0; while ( $i<= 100000) { $i++; $test = new test(); $test->name; }
檢測this
time php test1.php time php test2.php
結果spa
test1.php real 0m0.015s user 0m0.008s sys 0m0.006s test2.php real 0m0.017s user 0m0.014s sys 0m0.002s
明顯不使用魔術方法的運行時間是使用魔術方法的一半,若是項目很大,那麼性能影響就會很大。code