I understand your first concern, and I agree - the models should not
be aware of their proxies.
In the case of Reflection, an extremely useful tool that simplify many
programming tasks considerably, it does break polymorphism by
principle - if you're going to use Reflection, there is no way around
that. But usually, when applied, that's a calculated trade-off. If
you're going to use Reflection, there's no way around that.
Since get_class() and Reflection are fairly commonly used in PHP,
especially in modern frameworks, my objective here is, if it's
possible (and practical), why don't we try to find a way around
breaking normal PHP behavior? :-)
So here's another code sample:
<?php
class Proxy
{
private $entity;
public function __construct($entity)
{
$this->entity = $entity;
}
public function getName()
{
return $this->entity->name;
}
}
class ProxyManager
{
private static $proxies=array();
public static function getProxy($entity)
{
$id = spl_object_hash($entity);
if (!isset(self::$proxies[$id]))
self::$proxies[$id] = new Proxy($entity);
return self::$proxies[$id];
}
}
class User
{
public $name;
}
$user1 = new User;
$user2 = new User;
$proxy1 = ProxyManager::getProxy($user1);
$proxy2 = ProxyManager::getProxy($user2);
$user1->name = 'Rasmus';
$user2->name = 'Alvaro';
header('Content-type: text/plain');
var_dump($proxy1->getName(), $proxy2->getName());
?>
This removes the proxy from the entity entirely - it now simply keeps
track of objects by referencing the instances at run-time.
I think this is actually cleaner than what you're doing currently?
What do you think?