Jump to content

Instantiate a class within a class


robgolding63

Recommended Posts

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).

Link to comment
https://forums.phpfreaks.com/topic/54765-instantiate-a-class-within-a-class/
Share on other sites

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)

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

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();
    }
}
?>

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

All is well now, turns out I had missed out a fullstop when assigning variables to the template :D

 

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.