robgolding63 Posted June 8, 2007 Share Posted June 8, 2007 Hi, I have written a template system, which is called like so: <?php $template = new template("index_body.html"); $template->assign_var("TITLE", "Site Title"); $template->output(); ?> What I want to ask is, is it supported to instatiate the template class (or any other class for that matter) from inside another class. I am rewriting the article system on my site, and for the viewarticle function, I obviously need to instatiate the template class, pass it some variables, then output the compiled template. I am doing this as follows: <?php class article { var $template; $this->template = new template("article_body.html"); $this->template->output(); } ?> Is this correct? The reason I am asking is because the assign_block_vars() function won't do what it's supposed to - (similar names and functions to phpBB, but code is much simpler). Quote Link to comment https://forums.phpfreaks.com/topic/54765-instantiate-a-class-within-a-class/ Share on other sites More sharing options...
per1os Posted June 8, 2007 Share Posted June 8, 2007 If you are using PHP4 this is how you should do it: <?php class article { var $template; function article() { $this->template = new template("article_body.html"); $this->template->output(); } } ?> Instantiate it in the constructor, as I do not think what you have is valid. (Note I assumed PHP4 due the var keyword) Quote Link to comment https://forums.phpfreaks.com/topic/54765-instantiate-a-class-within-a-class/#findComment-270840 Share on other sites More sharing options...
robgolding63 Posted June 8, 2007 Author Share Posted June 8, 2007 Actually I'm running PHP5, is it very different? I thought you had to use "var" . Everything is working with the templates, it replaces variables and outputs correctly, but just wont loop over block variables, with the assign_block_vars() function. Thanks for the help, Rob Quote Link to comment https://forums.phpfreaks.com/topic/54765-instantiate-a-class-within-a-class/#findComment-270845 Share on other sites More sharing options...
per1os Posted June 8, 2007 Share Posted June 8, 2007 PHP5 allows for private, public and protected instead of var, var has actually be depreciated. The following should work for PHP5 (should as I am not sure I run php4) <?php class article { public $template; function __constructor() { /// either 2 or 3 underscores not sure. $this->template = new template("article_body.html"); $this->template->output(); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/54765-instantiate-a-class-within-a-class/#findComment-270963 Share on other sites More sharing options...
robgolding63 Posted June 8, 2007 Author Share Posted June 8, 2007 This is exactly how I am doing it, but with "article" as the name of the constructor, not __constructor - as a function with the same name as the class is also considered a constructor, is that correct? The strange thing is, everything is working, apart from the looping part of the template engine - it loops over the code the correct number of times, but doesn't replace the variables with anything, so I end up with {related.NAME} instead of the name of a related article . The fact that only one function doesn't work correctly suggests that it's my fault, but it works fine when called from a normal variable within a class, ie. $template = new template("article_body.html"); instead of $this->article = new template("article.html"); If you want I can post the code for the template engine, and the article system as well, at the moment I am working on http://maxms.net/test.php Thanks for the help, I'll look over the code to see if I can find any errors, but I don't even know where to start! Rob Quote Link to comment https://forums.phpfreaks.com/topic/54765-instantiate-a-class-within-a-class/#findComment-271165 Share on other sites More sharing options...
emehrkay Posted June 10, 2007 Share Posted June 10, 2007 frost's code is almost correct change __constructor to __construct like this public function __construct(){} Quote Link to comment https://forums.phpfreaks.com/topic/54765-instantiate-a-class-within-a-class/#findComment-271652 Share on other sites More sharing options...
robgolding63 Posted June 10, 2007 Author Share Posted June 10, 2007 All is well now, turns out I had missed out a fullstop when assigning variables to the template Thanks for all the help, I've almost finished moving the whole site over to the template system, and it's much neater now - but it's a little bit slower than before - takes about 0.04 seconds to parse a page - is that average? Rob Quote Link to comment https://forums.phpfreaks.com/topic/54765-instantiate-a-class-within-a-class/#findComment-271693 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.