Skip to content

Instantly share code, notes, and snippets.

@RRStoyanov
Forked from snpy/Test1.php
Created March 11, 2012 14:51
Show Gist options
  • Select an option

  • Save RRStoyanov/2016712 to your computer and use it in GitHub Desktop.

Select an option

Save RRStoyanov/2016712 to your computer and use it in GitHub Desktop.
Plain old class with multiple getters vs class with single magic method replacing multiple getters.
<?php
define( 'MAX', 100000 );
header( 'Content-Type: text/plain' );
$test = @$_GET['test'] ?: @$argv[1];
$test = $test . '.php';
if ( false === file_exists( $test ) )
{
$test = 'Test1.php';
}
$mem = memory_get_peak_usage();
require $test;
$mt = microtime( 1 );
for ( $i = MAX; false === empty( $i ); --$i ) {
$new = new Test();
$new->getTitle0();
unset( $new );
}
echo 'time [s]: ', ( microtime( 1 ) - $mt ), PHP_EOL, 'mem [B]: ', ( memory_get_peak_usage() - $mem ), PHP_EOL;
<?php
class Test {
public $title0 = 'string value';
public $title1 = 'string value';
public $title2 = 'string value';
public $title3 = 'string value';
public $title4 = 'string value';
public $title5 = 'string value';
public $title6 = 'string value';
public $title7 = 'string value';
public $title8 = 'string value';
public $title9 = 'string value';
public function getTitle0()
{
return $this->title0;
}
public function getTitle1()
{
return $this->title1;
}
public function getTitle2()
{
return $this->title2;
}
public function getTitle3()
{
return $this->title3;
}
public function getTitle4()
{
return $this->title4;
}
public function getTitle5()
{
return $this->title5;
}
public function getTitle6()
{
return $this->title6;
}
public function getTitle7()
{
return $this->title7;
}
public function getTitle8()
{
return $this->title8;
}
public function getTitle9()
{
return $this->title9;
}
}
<?php
class Test {
public $title0 = 'string value';
public $title1 = 'string value';
public $title2 = 'string value';
public $title3 = 'string value';
public $title4 = 'string value';
public $title5 = 'string value';
public $title6 = 'string value';
public $title7 = 'string value';
public $title8 = 'string value';
public $title9 = 'string value';
public function __call( $name, $args )
{
$field = 'title' . substr( $name, -1 );
return $this->$field;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment