> 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];
}

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

Reply via email to