Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Using $this when not in object context

Options
  • 18-11-2011 9:42pm
    #1
    Registered Users Posts: 1,657 ✭✭✭


    Hey folks

    I have this error in PHP (wordpress)

    "Using $this when not in object context"

    My $this is inside a class so that's not the problem... Here is an extract of my code below - the offending line is in display_donors()
    class DonateForm {
    /* 
    .
    .
    Omitted a couple of functions 
    .
    .
    */
    	function display_donors () {
    
    		$donor_list = new Donor_List (); // A different class defined elsewhere
    		
    		if ($this->table_exists()) {
    		
    			$donor_list->prepare_items();
    			$donor_list->display();
    
    		}
    	}
    	
    	function table_exists() {
    		global $wpdb; 
    		
    		$table_exists = $wpdb->get_results('SHOW TABLES LIKE ' . $wpdb->prefix . 'donor_list');
    		
    		if (empty($table_exists)) {
    			$str_Create_Table = 'CREATE TABLE ' . $wpdb->prefix . 'donor_list (
    				id INT PRIMARY KEY,
    				name VARCHAR(100),
    				address VARCHAR(200),
    				email_address VARCHAR(50),
    				phone_number VARCHAR(20),
    				amount FLOAT(6,2),
    				frequency VARCHAR(20),
    				contact TINYINT
    			)';
    			
    			$wpdb->query($str_Create_Table);
    		}
    		
    		return true;
    	}
    }
    


Comments

  • Registered Users Posts: 6,464 ✭✭✭MOH


    Any chance you've an extra } somewhere in the omitted code, closing the class?


  • Registered Users Posts: 1,657 ✭✭✭komodosp


    No... I'm using Notepad++ which highlights the matching bracket when you move the mouse over and it's fine. Besides, if I take out the $this line, it runs fine with no error.


  • Registered Users Posts: 3,140 ✭✭✭ocallagh


    The fact it runs fine without $this would backup what MOH suggested.

    The only other way (I can see) that you might get that error is if you are calling that function in some sort of static scope, but it's not declared static from what I can see.

    Post up entire class code and also the line of code you use to initiate the class instance and should be able to sort it.


  • Registered Users Posts: 1,657 ✭✭✭komodosp


    The fact it runs fine without $this would backup what MOH suggested.

    Surely it would give an error if I had an extra } at the end, though.

    I've modified the code to avoid the $this error but I'll post it when I get the chance.


Advertisement