![]() |
Using class methods in an included file?
Hello,
I'm creating a script, and I'm coding the templating system at the moment. The problem I'm running into, is that I am using class methods and variables (using the __call() magic method), in a template file, which includes no classes nor defines them.. I thought that as long as the parent file initializes the classes, I could use them in an included file in the same parent file. Here is my code: Index.php: PHP Code:
PHP Code:
Code:
Fatal error: Call to a member function site_name() on a non-object in C:\xampp\htdocs\PHP\WallpaperScript\templates\default\header.php on line 6How do I get around this? |
What I'm doing (using the Kohana framework) is to render a View but also passing an array to the View with all the needed data. Like this:
PHP Code:
indexView.php you then can simple call/echo $szTitle and $szHeader.I hope this helps you a bit. |
Not bad. Can you pass them through individually as well? I remember in CakePHP it was like:
php Code:
A bit slow, but I'm sure if I went deeper into it they'd be an array way as well. |
This is going off topic, but Kohana takes the View as just another object to be manipulated. Member variables can be set dynamically which are then available in the templates files. For example:
PHP Code:
Back to the original topic, chances are that you're looking at an issue of scope. The header appears to be loaded inside of a class method (on the $tpl object) so the global variables ($user, $site ...) will not be available unless you're using 'global $site' to provide access, or $GLOBALS['site'], or passing those into the template class/methods. For a test, you could try and var_dump($site) (from inside the header() method) just to see if the object is available to use. |
Quote:
PHP Code:
|
The use of 'global' is covered well in the PHP manual: Variable scope.
|
I was just trying out a few placements and got it working eventually. Thanks a lot Salathe! :)
|
| All times are GMT. The time now is 12:56 PM. |
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0