Jump to content

[SOLVED] calling an objects method within another object


andy_b42

Recommended Posts

I am fairly new to using objects in php. I have made a fairly simple database object so i can just call one of a few of my own methods to automatically make changes to the database when i change the relevant object. Below is a very simplified version of what i am trying to accomplish:

 

Class Database{

  public function Query($query){

      //connect to database, run query and return result

  }

}

 

Class User{

  private $username;

  private $db;

 

  public function __construct($intID){

      $this->db = new Database();

      if($intID > 0){

        getUserData($intID);

      }

  }

 

  public function getUserData($intID){

      $sql = "select * from users where intID = $intID";

      return $this->db->Query($sql);

  }

}

 

The problem i get is when "$this->db->Query($sql);" is executed and the error i get is:

"Fatal error: Call to a member function Query() on a non-object"

 

According to the IDE i am using, the function is accessible, but it seems when running the code, the parser cannot see that $this->db is an object with functions.

 

My question is, is what i am attempting to do possible or am I missing something very basic to make this work?

 

Thanks

Link to comment
Share on other sites

My question is, is what i am attempting to do possible or am I missing something very basic to make this work?

 

Not only is it possible, but the example you have supplied should actually work. Although this....

 

public function __construct($intID){
      $this->db = new Database();
      if($intID > 0){
         getUserData($intID);
      }
   }

 

should be....

 

public function __construct($intID) {
  $this->db = new Database();
  if ($intID > 0) {
    $this->getUserData($intID);
  }
}

Link to comment
Share on other sites

Thanks for the quick replies

 

The objects i am actually using are a lot more complex than what i have posted here: below are the database and user objects

 

http://andyb42.servebeer.com:4567/phpfreaks/database.txt

http://andyb42.servebeer.com:4567/phpfreaks/user.txt

 

The user is instantiated like this "$user = new User($intID);", the constructor then does all of the work filling in the users data from the database.

Link to comment
Share on other sites

Look in your User class

in the function load you have this

$user = mysql_fetch_array($db->getResult());

change $db to $this->db

 

Thanks, that line was like that becuase the only way i can make this work was to create the $db object outside the calss and then inside the function, use global $db and the call it as though $db were a variable local to that method.

Link to comment
Share on other sites

i was under the impression that global variables are not very secure.

 

Also, is there a chance that if i use 2 or more objects on the same page, the $db variables (for simplicity sake i would like to call it the same thing in every object) would get mixed up?

 

It is quite important that each instance of the $db variable is unique to the object becasue the Database class holds the results of the query which are then used by the object if they are required.

Link to comment
Share on other sites

my sincere apologies for wasting everyones time... I fell like a complete idiot, in the User constructor i had called the load() before creating the $db object :(

 

Original:

public function User($intID = 0){
		if($intID > 0){
			$this->load($intID);
		}else{
			//echo "No " . $this->currentClass . "ID information supplied";
		}

		try{
			$this->db = new Database();
		}
		catch(Exception $exc){
			echo "Error creating database object";
		}
	}

 

Should be:

public function User($intID = 0){
		try{
			$this->db = new Database();
		}
		catch(Exception $exc){
			echo "Error creating database object";
		}

		if($intID > 0){
			$this->load($intID);
		}else{
			//echo "No " . $this->currentClass . "ID information supplied";
		}
	}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.