Call to a member function on a non-object

Discussion in 'PHP' started by warriorsz, Jul 15, 2008.

  1. #1
    Hey guys. here is my setup
    The first snippet is the page that I am getting the error "Call to a member function on a non-object"

    I am inferring that I am doing something wrong with the $l->get_state_full();

    the get_state_full function is at the bottom of the second snippet

    Let me know,
    Thanks

    require_once($physical_path."/class/class.Location.php");
    global $l;
    $state = $l->get_state_full();
    $meta_description        = $state."'s largest internship network - find internships and jobs locally or state wide!"; 
    PHP:
    class Location {
    	var $domain;
    	var $id;
    	var $title;
    	var $description;
    	var $use_default;
    	var $google_code;
    	var $state;
    	var $state_full;
    	var $code_abbrev;
    	
    	function Location($id = -1) {
    		if($id  == -1){
    			$this->domain = $_SERVER["SERVER_NAME"];
    			if($this->domain == 'localhost')
    				$this->domain = 'www.****.com';	
    			if($this->domain == '**.**.***.**'){
    				$this->domain = 'www.****.com';
    			}
    			if(substr($this->domain,0,4) != "www."){
    				$this->domain = "www.".$this->domain;
    			}
    			$q = "SELECT * ".
    			     "FROM locations " . 
    				  "WHERE domain = '$this->domain'";
    			//echo $q;
    		} else {
    			$q = "SELECT * ".
    			     "FROM locations " . 
    				  "WHERE id = $id";
    		}
    			//echo $q;
    			$results = mysql_query($q);
    			
    		// If the above failed, try the domain redirection...
    		if($results == false || mysql_numrows($results) == 0){
    			$r = $this->check_redirect();
    			$q = "SELECT * ".
    			     "FROM locations " . 
    				  "WHERE id = $id";
    			$results = mysql_query($q);
    		}
    		
    		// If either of these worked, process the data
    		// If none of them worked, you're shit out of luck. 
    		if($row = mysql_fetch_array($results)){
    			
    			$row = filter_record($row);
    			$this->id = $row["id"];
    			$this->domain = $row["domain"];
    			$this->title = $row["title"];
    			$this->google_code = $row["google_id"];
    			$this->use_default = $row["use_default"];			
    			$this->state = $row["state"];
    			$this->code_abbrev = $row["code_abbrev"];
    			
    			$this->state_full = $row["state_full"];
    			
    		}else {
    			$this->title = " - Not available - ";
    			$this->description = " - Not available - ";
    		}
    	}
    
    	function get_state_full(){
    		return $this->state_full;
    	
    	}
    PHP:

     
    warriorsz, Jul 15, 2008 IP
  2. rodney88

    rodney88 Guest

    Messages:
    480
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    0
    #2
    The error message is saying $l is not an object. I can't be any more specific with the code you've given but for whatever reason, $l is not what you're expecting it to be.
     
    rodney88, Jul 15, 2008 IP
  3. php-lover

    php-lover Active Member

    Messages:
    261
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    58
    #3
    $l = new Location; //<------initialize your class object first before you use it

    global $l; //<--you don't need a global, only use global if you inside function.
    
    require_once($physical_path."/class/class.Location.php");
    
    $l = new Location;
    $state = $l->get_state_full();
    $meta_description        = $state."'s largest internship network - find internships and jobs locally or state wide!";
    
    
    PHP:
     
    php-lover, Jul 15, 2008 IP