Calling Classes within Classes

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
gkent
Forum Newbie
Posts: 3
Joined: Fri Oct 21, 2011 2:31 pm

Calling Classes within Classes

Post by gkent »

Hi,

I am a new member of the forum and a beginner in PHP programming. I am having trouble with what seems to be a very basic convention of PHP. My goal is to have separate files for all of my common PHP classes (i.e. MySQL_class, Authentication_class, etc). I would like to call several of the functions from the MySQL_class within the Authentication_class and other classes; however, I am not having success calling the functions.

Code: Select all


class MySQL_class {
function QueryItem($string) {
  --Perform a query based on a SQL string
}
}

class Authentication_class {
function CheckPassword($password) {
  --Run QueryItem with a string with $password in the WHERE clause by a simple call of the QueryItem function
}
}

Any suggestions? I'm at a loss as to how to make this work.
gkent
Forum Newbie
Posts: 3
Joined: Fri Oct 21, 2011 2:31 pm

Re: Calling Classes within Classes

Post by gkent »

I got that part working now, but the query is returning an error rather than the query results. I've determined the original connection that I made to the database is not longer available for the queries. Here's the code I have...

(Within MySQL_Class)

Code: Select all


function Create() {
$this->id = @mysql_pconnect($this->host, $this->user, $this->pass) or
            MySQL_ErrorMsg("Unable to connect to MySQL server: $this->host : '$SERVER_NAME'");
        $this->selectdb($this->db);
}

function Query ($query)
    {
        $this->result = @mysql_query($query, $this->id) or
            MySQL_ErrorMsg ("Unable to perform query: $query");
        $this->rows = @mysql_num_rows($this->result);
        $this->a_rows = @mysql_affected_rows($this->id);
    }


Within the Authentication_class

Code: Select all

function UserExist($username) {
		parent::QueryItem("SELECT count(*) FROM tblUsers WHERE user_login = '$username' LIMIT 1");
		$recordCount = parent::$this->data[0];
			echo("$recordCount,<br />");
	}
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Calling Classes within Classes

Post by Christopher »

It is a better practice to use objects and composition:

Code: Select all

class MySQL_class {
  function QueryItem($string) {
    --Perform a query based on a SQL string
  }
}

class Authentication_class {
  protected $db;

  function __construct($db) {
    $this->db = $db;
  }

  function CheckPassword($password) {
    $this->db->QueryItem("SELECT * FROM users WHERE password='$password'");
  }
}

$db = new MySQL_class($config);
$auth = new Authentication_class($db);
$auth-> CheckPassword($value);
(#10850)
gkent
Forum Newbie
Posts: 3
Joined: Fri Oct 21, 2011 2:31 pm

Re: Calling Classes within Classes

Post by gkent »

I am not sure I understand the last post. What is the purpose of this function within the Authentication_class?

Code: Select all


protected $db;

  function __construct($db) {
    $this->db = $db;
  }

How do I cause the functions to talk to one another if they are located in two separate external files?
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: Calling Classes within Classes

Post by Eric! »

I'll try to explain it more. You're passing the instance of MySQL_class to the Authentication_class using the variable $db. The __construct is called automatically any time a new ClassName() call is made and in this case the following happens:

Code: Select all

/*  First setup an instance of MySQL_class and let $db refer to that instance
     so other classes "know where to find it" if you pass $db to them.
*/
$db = new MySQL_class($config); //construct this class and assign it to $db

/* Construct the next instance by passing the instance to MySQL_class to it as $db 
    This will automatically call the __construct function where the line
       $this->db =$db;  will tell PHP that this object should use the instance defined by $db
*/
$auth = new Authentication_class($db); // automatically calls the __construct function in Authentication_class

/* The reference $this->db was set with the __construct earlier so this class now knows
    to look in the instance passed to it which is MySQL_class for processing the $value with 
    QueryItem.  This is how it finds the proper function to run.
*/
$auth-> CheckPassword($value);
And to confuse you more...you could also extend your main class. Here's a brief example.

Code: Select all

class MySQL_class {
  function QueryItem($string) {
    echo "Perform a query based on a SQL string $string";
  }
}

class Authentication_class extends MySQL_class{
  function CheckPassword($password) {
    $this->QueryItem("SELECT * FROM users WHERE password='$password'");
  }
}

$auth = new Authentication_class();
$auth-> CheckPassword("password");
Edit: I found a good basic tutorial on this subject that might help.
Post Reply