Access instantiated class methods
I'm currently working on a class that manages the user "authorization", and since the users are stored on a database, i need to access it from within the class. I've found this Mysql class: http://github.com/JeffreyWay/PHP-MySQL-Database-Class/blob/master/MysqlDb.p开发者_Go百科hp
But when i have instantiated the Mysql class with the mysql login information. How can i then use the methods from the Mysql class inside the other class?
You will need to add a reference to the MySQL object in your class.
I recommend passing the MySQL object into your constructor and accessing it like this:
class MyClass
{
private $mysql;
function __construct($mysql)
{
$this->mysql = $mysql;
}
function foo()
{
$this->mysql->doSomething();
}
}
You can then create your instance of the object with:
$myobject = new MyClass($mysql);
In your other class you should requir Mysql class file. Create an ocject. And you can use public methods of Mysql
class.
require 'path/Mysql.php';
$mysql = new Mysql();
$mysql->connect();
But check if Mysql class methods ere public.
If there are static methods - use ::
instead of ->
.
You can do create __autoload
function and don't require Mysql class.
精彩评论