www.webdeveloper.com
Results 1 to 4 of 4

Thread: using $this when not in object context

  1. #1
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    304

    using $this when not in object context

    Hello guys!

    Im coding a new project and ran into this very odd error, the output says:
    Fatal error: Using $this when not in object context in C:\wamp\www\ChessRPG\inc\template.inc.php on line 23


    I tried a work around, at that time it was possible to just assign the final value to the variable but more errors like this started to show up, so think I should get this cleared out before going on to redesign the whole project. I have commented above the two lines that I know show this same error:

    PHP Code:
    <?php
    /**
    *@author purefan
    */

    class Template
    {
        
    // Attributes
        // the path to the templates
        
    var $path "templates/";
        
    // the file name we're fetching
        
    var $_file "";
        
    // the array of changes to be applied to this fileName
        
    var $_changes = array();
        
        
                
    // Functions
        
    function Template()
        {}
        
        function 
    loadTemplate($section ""$fn)
        {
            
    // this next line is error number 1
            
    $filename $this->path;
            
    $filename.=$section.$fn;
            if(
    file_exists($filename))
            {
                    
    $fr fopen($filename,"r");
                    
    $this->_file fread($fhfilesize($myFile));
                    return 
    true;
            }
            return 
    false;
        }

        function 
    setChanges($arr)
        {
            
    $this->_changes $arr;
        }
        
        function 
    applyChanges()
        {
            if(
    $this-_file != "")
            {
                foreach(
    $this->_changes as $from => $to)
                {
                    
    $this->_file str_replace("{%".$from."%}"$to$this->_file);
                }
                
    //$this->_file = str_replace("{".$from."}", $to, $this->_file);
            
    }
        }
        
        function 
    prnTemplate($isReturn false)
        {
            if(
    $isReturn){
                return 
    $this->_file;
            }
            else
            {
                
    // this other line shows another error like before
                
    echo $this->_file;
            }
        }
    }
    ?>
    The project is running on a PHP5 server (its actually WAMP) in case this helps...

    Ive looked around but the obvious replies have been what the error itself suggests, that $this is being used in a non object context, but I just want to access the attribute like ive done always before, dont know why this time it isnt working...

    Thanks for your time

  2. #2
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,742
    Are you accessing the class statically?

  3. #3
    Join Date
    Jun 2006
    Location
    Down at the bottom of the garden
    Posts
    1,239
    PHP Code:
    function applyChanges()
        {
            if(
    $this-_file != ""
    Spot the missing >

  4. #4
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    304
    Bokeh!!!
    you are a marvel! I actually was and when I instantiated it we're back on track :smile: Thank you so much!

    Thanks to you too MrCoder!!, this was also a miss from me
    Quote Originally Posted by MrCoder
    PHP Code:
    function applyChanges()
        {
            if(
    $this-_file != ""
    Spot the missing >

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles