> On Aug 26, 2021, at 2:27 AM, Reindl Harald (privat) <ha...@rhsoft.net> wrote:
> Am 26.08.21 um 00:59 schrieb Mike Schinkel:
>>> On Aug 25, 2021, at 6:41 PM, Reindl Harald (privat) <ha...@rhsoft.net> 
>>> wrote:
>>> Am 26.08.21 um 00:37 schrieb Mike Schinkel:
>>>> That said, I'd be really interested in seeing use-cases where having 
>>>> dynamic properties is essential to an architecture and where it could not 
>>>> be easily refactored.  I cannot envision any, but I am sure that I am just 
>>>> limited by the extent of my vision and so would like to know what those 
>>>> use-cases would be.
>>> 
>>> public function __get(string $subclass)
>>> {
>>>  $include_file = "{$this->basedir}/modules/{$subclass}/api_{$subclass}.php";
>>>  $class_name = "cl_{$subclass}";
>>>  if(!include $include_file)
>>>  {
>>>   $this->misc->trigger_error("API-LOADER FAILED: '{$subclass}'");
>>>   }
>>>  $this->$subclass = new $class_name;
>>>  $this->$subclass->cl_api = $this;
>>>  return $this->$subclass;
>>> }
>> Easily refactored:
>> public function __get(string $subclass)
>> {
>>   if (isset($this->subclasses[$subclass])) {
>>     return $this->subclasses[$subclass];
>>   }
>>   $include_file = "{$this->basedir}/modules/{$subclass}/api_{$subclass}.php";
>>   $class_name = "cl_{$subclass}";
>>   if(!include $include_file)
>>   {
>>     $this->misc->trigger_error("API-LOADER FAILED: '{$subclass}'");
>>   }
>>   $this->subclasses[$subclass] = new $class_name;
>>   $this->subclasses[$subclass]->cl_api = $this;
>>   return $this->subclasses[$subclass];
>> }
> 
> and now get is called every single time a property or method in the 
> class-tree is called instead only at the first call
> 
> means in the real world probably thousands of times for each website call - 
> congratulations

https://3v4l.org/HAkhjW#v8.0.9

Total execution time for 5000 iterations for BadActor = 0.351906 milliseconds
Total execution time for 5000 iterations for GoodActor = 0.758886 milliseconds
Total time difference for 5000 iterations = 0.406981 milliseconds
Total time difference for 1 iteration = 1/12,286 millisecond

"Premature optimization is the root of all evil" 
— Sir Tony Hoare 

-Mike

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: https://www.php.net/unsub.php

Reply via email to