PHP Passing variables to included file
-
03-07-2021 - |
Question
I have a very simple template class that allows me to set some values and then include a template file. The class stores the variables, and the template file can access using $this->variable.
So my question is how should I go about doing this? Or perhaps it'd be better to just assign some variables and then include the template file - that way they don't need to be passed around?
Solution
If you call `extract' like this:
extract( $this -> variables );
on top on your template code, you will be able to refer to $this -> variables[foo]
as $foo
.
OTHER TIPS
One of the advantages of OO programming is that variables can be bound to a class and used within the context of a class.
So $this->title
means: use the title variable bound to this class.
What you are basically asking is: how do I get back from OO programming to procedural programming. My answer would be: do not do that, try to stick with OO programming.