SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    ********* Addict
    Join Date
    Apr 2000
    Location
    Posts
    272
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello all.

    Here's the problem:

    I am making a PHP website, and I'm going to need lots of templates. So I researched on them, and I came to the conclusion that PHPLib's template system is the best option. I find this thing of having to set files before I can use them quite annoying, so I want to create a function to help. Here's my code so far for this:

    PHP Code:
    require ("inc/template.php");
    $t = new Template("templates");
    function 
    template ($template,$output="Main",$print=0) {
        
    $t->set_file($template,$template.".ihtml");
        if (
    $print==0) {
            
    $t->parse($output,$template);
        } else {
            
    $t->pparse($output,$template);
        }

    The output I get with this code is this:

    Fatal error: Call to a member function on a non-object in /nusphere/apache/htdocs/org/global.php on line 16

    I know I need to get the function to have access to the functions used in the template class, but I have tried adding a
    PHP Code:
    global $t
    line and I tried accessing the functions using
    PHP Code:
    $GLOBALS["$t"]->set_file(.........); 
    but none of them worked... Can anyone help?

    Pleease... I have already wasted hours just with this problem...

    Edit:

    I'd like to add that it works if I access these functions outside the function, but I don't want that.


    Thanks in advance.

    Regards,

    Pedro

    <off-topic>I am running all this on two computers, on my own private server, all on one computer. http://www.vmware.com - I am hooked with it. Anyway, on the "real" computer I am running Windows 2000, and on the virtual machine, I am running RedHat 7.0 with NuSphere and networking enabled. It is just sooo cool. The possibilites are endless, and experiments can be done by backing up your entire computer, so it's great for trying out new stuff on, although it is quite slow. </off-topic>
    Last edited by pedro_gb; Apr 21, 2001 at 11:14.

  2. #2
    SitePoint Columnist Skunk's Avatar
    Join Date
    Jan 2001
    Location
    Lawrence, Kansas
    Posts
    2,066
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The global thing should work just fine, provided you have declared that $t is an object somewhere else in the script before calling the function. Here's how it should look:
    PHP Code:
    require ("inc/template.php");
    $t = new Template("templates");
    function 
    template ($template,$output="Main",$print=0) {
        global 
    $t;
        
    $t->set_file($template,$template.".ihtml");
        if (
    $print==0) {
            
    $t->parse($output,$template);
        } else {
            
    $t->pparse($output,$template);
        }


  3. #3
    ********* Addict
    Join Date
    Apr 2000
    Location
    Posts
    272
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Noooo!!! Still doesn't work. I also thought it would work but it still gives me the error message I quoted above. This is so unfair. Perhaps it is because of the PHP version I have (I think 4.0.4). Who knows. Any help would be appreciated, though.

    Thanks for trying to help.

    Pedro

  4. #4
    SitePoint Enthusiast
    Join Date
    Feb 2001
    Location
    Monmouth Junction, NJ
    Posts
    88
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if you can send me the file i can take a look at it since i have no clue what line 16 really is

  5. #5
    ********* Addict
    Join Date
    Apr 2000
    Location
    Posts
    272
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Line 16 is the
    PHP Code:
    $t->set_file($template,$template.".ihtml"); 
    line. When I add the global thingy, the line with the error changes to 17. The only other thing the file does is it connects to the database.

    Thanks.

  6. #6
    SitePoint Enthusiast
    Join Date
    Feb 2001
    Location
    Monmouth Junction, NJ
    Posts
    88
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you got icq or aol or msn?

  7. #7
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I tell you what I think is a bit iffy about your code. You have a class Template, so naturally, the constructor name for this class is Template which you call when you instantiate the object $t*=*new*Template("templates");.

    Now the thing is you have defined a function:
    function template*($template,$output="Main",$print=0)

    Remember that in PHP function names are not case sensitive (which is crazy IMHO). Now you would expect that your function template should in no way conflict with the constructor for class Template as they are in completely different scope, global scope and class scope. However, I have always been a bit iffy about PHP's object orentedness, and suspect that this may be your problem. What happens if you change the name of your function from template to something else? Worth a try.

  8. #8
    ********* Addict
    Join Date
    Apr 2000
    Location
    Posts
    272
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    freakysid: Here's something I have to say to you:

    THANKS!


    To say the truth, I had thought of that, but I never tried it. It was probably too late or something and my brain has SpeedStep on, so I don't think as well. Anyway, I'm now off to fix another syntax error on the same file... I hope this is a simple one.

  9. #9
    ********* Addict
    Join Date
    Apr 2000
    Location
    Posts
    272
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually, I realised I had just created another syntax error when I renamed the function and all the references to it, but once I fixed it, the old error (the one that this thread is all about ) came back... Darn. Anyway, anyone have anymore ideas? Please...

  10. #10
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK - as well as renaming the function you included the line gobal $t; right?
    Can you post your code on the internet as text and tell us the url? This doesn't make sense as Skunk said! Perhaps if we can see the entire code.

    Also, one other thing would be to pass a reference to $t when you call the function so that the function signature would look something like:

    function*template*($template, &$t, $output="Main",$print=0)

    From a design perspective, what you appear to be doing is extending the functionalilty/behaviour of the class Template. So you could just add this function as a member function (with a different name) to class Template. For example if you call it function conditioinalParse():
    PHP Code:

    function*conditionalParse*
    ($template,$output="Main",$print=0)*{
    ****$this->set_file($template,$template.".ihtml");
    ****if*($print==0)*{
    ********$this->parse($output,$template);
    ****}*else*{
    * ******$this->pparse($output,$template);
    ****}

    And you would call it through the object like for example,
    PHP Code:
    $t->conditionalParse($template); 
    Now from the perspective of a pure design, adding this function to the object would be achieved by using inheritence, so you would create a subclass of Template, such as MyTemplate. This is something I haven't looked into with PHP.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •