php - How to derive public object interface from $this? -


This is a strip-down version of my template class, which is focused on executing a file in a limited scope $ Env :

  class templates {** ** * defines new opportunities by using the $ evn variable for the execution of the template and * executes the template script in an output buffer * * @Translate from the template directory except the $ string file file name (except the file extension) Te * @ Ultimate Array $ env variable populated in template scope. * @ String string template output * / render public function ($ file, array $ env = []) {$ env ['template'] = $ this; Returns self: render ($ file, $ env); } / ** * Use of additional static render method * This and other template properties are used to prevent the display in template scope. * Two variables (file and) * have been captured by using func_get_arg * The same reason not to highlight the template scope. * * Return template output * / Render static private function () {Remove (throw_gate_argatt (1), \ EXTR_REFS); Ob_start (); Func_get_arg (0) is required; Return ob_get_clean (); } Private Function Testing () {/ * I do not want to access this $ template}  

In the template itself, I want to display the instance of the template to execute the script Class can be done, eg.

  $ template = new template (); $ Template- & gt; Render ('foo', ['template' => gt; $ template]);  

However, there is clearly a need for capturing and passing $ template frequency when creating that template object.

However, if I do this inside the render method:

  $ env ['template'] = $ this;  

Then $ templates have access to private template methods, e.g. test .

Is there a way that can be obtained from the $ this example of that code used only by public methods?

You can create a rendering context that is a separate class; Here it is that this template :: render () function:

  changes the public function render ($ file, array $ env = []) {return RenderContext :: run ($ file, $ this, $ env); }  

and this is the shell class:

  class RenderContext {static function run ($ file, $ template, $ env) {extract ($ env) ); Ob_start (); $ File is required; Return ob_get_clean (); }}  

Comments