简体   繁体   中英

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.php

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM