Skip to content

Instantly share code, notes, and snippets.

@kobus1998
Created February 12, 2020 15:10
Show Gist options
  • Save kobus1998/6765f229e0d440444a6f086c0919c7ac to your computer and use it in GitHub Desktop.
Save kobus1998/6765f229e0d440444a6f086c0919c7ac to your computer and use it in GitHub Desktop.
reflectionclass cache
<?php
class ReflectionCache
{
protected $instance;
protected static $instances = [];
public function __construct($className)
{
if (!isset(self::$instances[$className])) {
self::$instances[$className] = new ReflectionClass($className);
}
$this->instance = self::$instances[$className];
}
public function get()
{
return $this->instance;
}
}
class AAA
{
public function doA($no)
{
echo "AAA\n";
}
}
$r1 = (new ReflectionCache("AAA"))->get();
$r2 = (new ReflectionCache("AAA"))->get();
echo $r1 === $r2 ? 'exactly the same' : 'not the same'; // exactly the same
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment