Bind custom method parameters to class attributes
<p>Within a class, is there a way to bind a class attribute to a method parameter so that a two-way link is established on Ctrl click? </p>
<pre class="brush:php;toolbar:false;">class Attachments extends Repository
{
public Documents $documents;
public function fromDocuments(callable $scope)
{
$this->scopeOnRepoProperty($scope, 'documents');
}
}
</pre>
<p>What I mean is that in this case the second parameter documents in the scopeOnRepoProperty() method should refer to the property $documents. <br /><br />The broader context is: <br />The problem is that a large part of the code is very similar, i.e. most scopeXXX/fromXXX methods do something similar, i.e. properties repository is passed as a parameter to the method. For example, in the above example, $scope($this->documents) would be executed; furthermore, if $this->documents was not initialized, a new instance would be created. <br /><br />So the code looks like this:</p><p><br /></p>
<pre class="brush:php;toolbar:false;">public function fromDocuments(callable $scope)
{
if (!isset($this->documents)) {
$this->documents = new Documents();
}
$scope($this->documents);
}
</pre>
<p>I wanted to avoid writing an if statement, creating a new object, and calling a method every time, so I thought I could do it with a method that would accept a callable object, a property name, and a type reflection class name. </p><p>The code runs fine, but unfortunately due to this approach I lose the binding to these properties. In previous versions, it was $this->documents, so the link was present, but that is no longer the case. </p><p>I would like to know if there is any way to achieve this. Any ideas? </p><p><br /></p>