<?php
if (substr(PHP_VERSION, 0, 5) !== '7.1.5') die('Meh');
function test($label = '') {
global $t;
$t2 = microtime(1);
if ($label) echo str_pad($label . ': ', 32, '.', STR_PAD_RIGHT) . ' ' . 1000 * ($t2 - $t) . 'ms' . "\n";
$t = $t2;
}
class A {
protected $_val = 123;
function getVal() {
return $this->_val;
}
function setVal($val) {
$this->_val = $val;
}
}
class A2 {
protected $_val = 123;
function getVal() {
return $this->_val;
}
function setVal($val) {
$this->_val = $val;
}
function __get($name) {
return $this->{'__get_' . $name}($name);
}
function __set($name, $value) {
$this->{'__set_' . $name}($value);
}
}
class B {
protected $val = 123;
function __get($name) {
return $this->{'__get_' . $name}($name);
}
function __set($name, $value) {
$this->{'__set_' . $name}($value);
}
function __get_val() {
return $this->val;
}
function __set_val($val) {
$this->val = $val;
}
}
class C {
protected $val = 123;
function __get($name) {
return $this->val;
}
function __set($name, $value) {
$this->val = $value;
}
}
class D {
public $val;
function __construct() {
$this->val = new Prop(123);
}
}
class D2 {
public $val;
protected $_val = 123;
function __construct() {
$this->val = new Prop2(function () {
return $this->_val;
}, function ($value) {
$this->_val = $value;
});
}
}
class Prop {
protected $_val = 123;
function __construct($val) {
$this->_val = $val;
}
function get() {
return $this->_val;
}
function set($val) {
$this->_val = $val;
}
}
class Prop2 {
protected $get;
protected $set;
function __construct($get, $set) {
$this->get = $get;
$this->set = $set;
}
function get() {
return ($this->get)();
}
function set($val) {
($this->set)($val);
}
}
class E {
public $val = 123;
}
$c = 1000000;
test();
$x = new A();
for ($i = $c; $i--;) {
$a = $x->getVal();
$x->setVal(321);
}
test('methods');
$x = new A2();
for ($i = $c; $i--;) {
$a = $x->getVal();
$x->setVal(321);
}
test('methods + passive magic');
$x = new B();
for ($i = $c; $i--;) {
$a = $x->val;
$x->val = 321;
}
test('magic');
$x = new C();
for ($i = $c; $i--;) {
$a = $x->val;
$x->val = 321;
}
test('light magic');
$x = new D();
for ($i = $c; $i--;) {
$a = $x->val->get();
$x->val->set(321);
}
test('nested object');
$x = new D2();
for ($i = $c; $i--;) {
$a = $x->val->get();
$x->val->set(321);
}
test('nested proxy');
$x = new E();
for ($i = $c; $i--;) {
$a = $x->val;
$x->val = 321;
}
test('property');
preferences:
33.55 ms | 402 KiB | 5 Q