Note that there are some explanatory texts on larger screens.

plurals
  1. POIs it possible to modify methods of an object instance using reflection
    text
    copied!<p>What I'm trying to do is create a domain model with a Plain Old PHP Object. The I'm creating a library which will do all the infrastructure stuff. So one of my models looks like this</p> <pre><code>class Project { public $id; public $name; public $typeId; private $type; public function getType() { return $this-&gt;type; } public function setType(Type $type) { $this-&gt;typeId = $type-&gt;id; $this-&gt;type = $type; } } </code></pre> <p>Now if create a new Project and call <code>setType</code> with a valid Type object and save the project instance using the ORM both the project and type is saved. But then I load the Project and use <code>getType</code> method I want the ORM to have modified this method transparently to load the object from persitence. So this method here:</p> <pre><code>public function getType() { return $this-&gt;type; } </code></pre> <p>to be transparently changed into:</p> <pre><code>public function getType() { if (is_null($this-&gt;type) { $this-&gt;type = $this-&gt;adapter-&gt;findById('table', $this-&gt;typeId); } return $this-&gt;type; // or return parent::getType(); } </code></pre> <p>Outlet PHP uses eval to create a Proxy class for Project called Project_Proxy but sometimes there will be subclasses of Project so I am searching for a solution by using Reflection API if there is a way.</p> <p>I have search google but haven't found anyway to change a method behaviour</p> <p><strong>Edit:</strong> or would it be a good idea to use Outlet PHP's eval method to create Proxy Classes for my models and all subclasses of them?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload