TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   Accessing Mysqli From Different Class (http://www.talkphp.com/general/4891-accessing-mysqli-different-class.html)

Nora 08-23-2009 02:17 PM

Accessing Mysqli From Different Class
 
I'm just getting started with OOP and I'm having trouble connecting to the database.

I made a class that would allowed to connect to the database but I had to use 'global $mysqli' in every method to avoid the non-object errors.
Now, I'm experimenting with a singleton pattern but I have no idea how to access the database connection from another class.

Database class:
PHP Code:

class database{

    private 
$mysqli;
    private function 
__construct(){
        
$this->mysqli = new mysqli($this->host$this->user$this->pass$this->name);        
    }

    public static function 
getInstance(){    
        if(
is_null(database::$instance)){
            
database::$instance = new database();
        }
        return 
database::$instance;    
    }


New class where it all goes wrong:
PHP Code:

class {
    public function 
select($sql){
        
$query $mysqli->query($sql);
    }


It works fine outside the classes using:
$db = database::getInstance();

I'd really appreciate it if someone could point me in the right direction. :-)

hello-world 08-23-2009 06:05 PM

Quote:

Originally Posted by Nora (Post 28037)
I'm just getting started with OOP and I'm .....
I'd really appreciate it if someone could point me in the right direction. :-)

you can use extends;
PHP Code:

class extends database{
//codes go here.



Enfernikus 08-23-2009 07:27 PM

Only extend classes which will be children of that database object, for instance an SQL-Writer ( Part of an ActiveRecord, ORM Object ) would be a logical extension of a DAL object ( Though the argument could be made that the DAL object should just be fed into the SQL Writer but I digress ).

In this instance the issue seems to be that the object isn't being created in your getInstance() method. Also, I don't see the static instance property in your class.

Hello-world, extending the database class would bear no fruit in this case because the __construct method creates the database connection and it is private ( I'm under the assumption that he would like to keep the current structure )

Wildhoney 08-23-2009 09:48 PM

As you've already identified a solution, we might as well stick to it. That being the singleton pattern. This would work like so. With the following database class:

php Code:
class Database
{
    private static $m_pInstance;
   
    /**
     * @return Database
     */

    public static function getInstance()
    {
        if (!isset(self::$m_pInstance))
        {
            self::$m_pInstance = new self();
        }
       
        return self::$m_pInstance;
    }
   
    public function query($szSQL)
    {
        printf("Executing query: %s<br />", $szSQL);
    }
}

And then we can use it like so from any other class we wish:

php Code:
class MyClass
{
    public function __construct()
    {
        Database::getInstance()->query('SELECT * FROM myTable');
    }
}

new MyClass();

hello-world 08-24-2009 12:23 AM

Quote:

Originally Posted by Enfernikus (Post 28040)
Hello-world, extending the database class would bear no fruit in this case because the __construct method creates the database connection and it is private ( I'm under the assumption that he would like to keep the current structure )

Thanks alot for pointing out my mistake. Every time I am here. I learn something new.8-)

captainmerton 08-24-2009 09:43 AM

for info - i ended up creating a singleton select pattern for db access. Using a static method check if an object has already been instantiated and if not then instantiate one.

I also extended the mysqli PHP delivered class to add a lot of extra functionality like class methods to start transactions of work, force rollbacks etc and also added a debug property so if its set to True i throw exceptions spilling out a lot of debug info and if False i throw a generic "having technical difficulties" exception.

Reason I mention is I am using the same approach as yourself and found extending the mysqli class incredibly useful.


All times are GMT. The time now is 01:22 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0