SitePoint Sponsor |
|
User Tag List
Results 1 to 16 of 16
-
Apr 12, 2006, 16:12 #1
- Join Date
- Apr 2006
- Posts
- 89
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Loading templates from a class break the code
I just coded a class for loading template/pages; simplified:
PHP Code:class tpl {
function getTpl($file) {
include 'templates/'.$file;
}
}
$tpl = new tpl();
$mysql = new mysql();
$tpl->getTpl("mytemplate.php");
PHP Code:<?=$mysql->getData();?>
From what i understand the included file works from within the tpl class and therefore it can't access the mysql class.
What alternatives do i have, any suggestions on how to solve this?
The solutions i have soo far:
* Skip the tpl class and just use include 'template';
* Send some kind of reference of mysql class to tpl class, new tpl(new mysql), but since i have around 20 classes that may or may not be used inside the template i don't like this one.
Thanks
-
Apr 12, 2006, 16:39 #2
- Join Date
- Apr 2006
- Posts
- 89
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I have tried experimenting with ob_start/ob_get_clean and the Factory pattern/method but i don't see how i could get it to work.
-
Apr 12, 2006, 16:59 #3
- Join Date
- Jun 2004
- Location
- Copenhagen, Denmark
- Posts
- 6,157
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
$mysql is a variable, not a class. A class is a code-structure, like a function.
Your template-code can't access the variable, because it's in a different scope. See variables.scope and php variable scope for an explanation.
-
Apr 12, 2006, 17:01 #4
$mysql is out of scope when your template is called
One alternative is..
PHP Code:class tpl {
private $vars;
function set($name, $var) { $this->vars[$name] = $var; }
function getTpl($file) {
extract($this->vars);
include 'templates/'.$file;
}
}
$tpl = new tpl();
$tpl->set('mysql', new mysql());
$tpl->getTpl("mytemplate.php");
-
Apr 12, 2006, 17:06 #5
- Join Date
- Apr 2006
- Posts
- 89
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by kyberfabrikken
Originally Posted by kyberfabrikken
-
Apr 12, 2006, 17:07 #6
- Join Date
- Apr 2006
- Posts
- 89
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Thanks ren, thats worth looking into
-
Apr 12, 2006, 17:59 #7
- Join Date
- Jun 2004
- Location
- Copenhagen, Denmark
- Posts
- 6,157
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by webdevindex
Originally Posted by webdevindex
When calling a function, the variable-scope changes. Normally, only variables which are declared within the current scope are accessible. If we only focus on procedural programming (no objects/classes), the scope changes when you call a function - The function has it's own scope. You have two ways of passing a variable from the calling scope and into that functions scope. Either you pass it as an argument to the function, or you declare it global. The latter is bad practice and should be avoided.
The following should illustrate :
PHP Code:function test() {
echo $foo; // an undefined local variable
}
function test2() {
$foo = 53; // a local variable
echo $foo;
}
function test3($foo) { // the variable gets passed in as a parameter
echo $foo;
}
function test4() {
global $foo; // the variable gets passed in as a global variable
echo $foo;
}
function test5() {
global $foo;
$foo = 53;
echo $foo;
}
$foo = 42;
echo $foo; // will output 42
test(); // will not output anything
$test2(); // will output 53
echo $foo; // will still output 42
$test3($foo); // will output 42
$test4(); // will output 42
$test5(); // will output 53
echo $foo; // will now output 53
PHP Code:class MyClass
{
var $foo;
function test() {
echo $this->foo;
}
}
$myObject = new MyClass();
$myObject->foo = 42;
$myObject->test(); // will output 42
-
Apr 13, 2006, 03:38 #8
- Join Date
- Apr 2006
- Posts
- 89
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
No not really but i didn't know that when using a class to include a file the included file becomes part of that class-scoop. This would make it practually impossible to use a class for loading files since the code inside that file can't access anything else. :P
-
Apr 13, 2006, 04:17 #9
- Join Date
- Sep 2004
- Location
- UK
- Posts
- 19
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
...or, if you look at it another way. It's a brilliant way of restricting what is available to particular template, take the following for example.
PHP Code:class Template($file) {
private $file;
public function __construct($file) {
$this->file = 'templates/'.$file;
if(!file_exists($this->file)) {
throw new InvalidArgumentException("File not found");
}
}
public function render() {
include $this->file;
}
}
$somethingsecret = "Don't tell anyone about this, it's a secret";
$template = new Template('test.php');
$template->myvar = 'some data';
$template->render();
{{{test.php}}}
<?php
if(isset($somethingsecret)) {
echo("$somethingsecret\n");
} else {
echo("No secrets here, move along please\n");
}
if(isset($this->myvar)) {
echo("You're allowed to see {$this->myvar}\n");
} else {
echo("Nothing to see here, move along please\n");
}
Code:No secrets here, move along please You're allowed to see some data
-
Apr 13, 2006, 04:37 #10
- Join Date
- Apr 2004
- Location
- germany
- Posts
- 4,324
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by webdevindex
PHP Code:class tpl {
function getTpl($file) {
return 'templates/'.$file;
}
}
$tpl = new tpl();
$mysql = new mysql();
include $tpl->getTpl("mytemplate.php");
-
Apr 13, 2006, 05:39 #11
- Join Date
- Oct 2001
- Posts
- 656
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
...or, if you look at it another way. It's a brilliant way of restricting what is available to particular template, take the following for example.I once had a problem.
I thought: "Oh, I know: I'll just use XML!"
Now I had two problems.
-
Apr 13, 2006, 06:05 #12
- Join Date
- Sep 2004
- Location
- UK
- Posts
- 19
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Oh yea
Must remember to think before I speak
-
Apr 13, 2006, 06:32 #13
- Join Date
- Apr 2006
- Posts
- 89
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Thanks for all replys, can't decide if iŽll go with stereofrog or ren's suggestion.
Whats best? To pass the objects to the included file (ren) or to include the file in the "global scope".
Yes i understand that this probably depends on what you want to do, but is anyone of these considered bad practice?
Thanks again for your patience :I)
-
Apr 13, 2006, 07:34 #14
- Join Date
- Jan 2003
- Posts
- 5,748
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
> Whats best?
Rens example of use would proberly give you greater scope...
-
Apr 13, 2006, 23:05 #15
- Join Date
- May 2005
- Location
- Holland!
- Posts
- 852
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by webdevindex
anyway,
ob_start();
$myNewTemplateContent = ob_get_contents();
ob_flush_end();
return($myNewTemplateContent);
should do the trick, andaahh, <?=$mysql->getData();?> ?!?!?!? looks more like a static member to me in myAppp_mySqlDataManager::getData($templateID); or something like it....Business as usual is off the menu folks, ...
-
May 9, 2006, 16:11 #16
- Join Date
- Apr 2006
- Posts
- 89
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Ok now i have tried this for a while. Right now i load all my templates inside the tpl class and in the templates i use $this->function(); so i never use functions outside the tpl scoop in the templates. But the tpl class itself might call other classes / functions.
Example
PHP Code:class tpl {
function doSomething($str)
{
return do::Something($str);
}
function getTpl($file) {
include 'templates/'.$file;
}
}
Bookmarks