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!
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.
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.
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...
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);
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:
/* 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.
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");