TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 08-23-2009, 02:17 PM   #1 (permalink)
The Visitor
 
Join Date: Aug 2009
Location: The Netherlands
Posts: 3
Thanks: 0
Nora is on a distinguished road
Help 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.
Nora is offline  
Reply With Quote
Old 08-23-2009, 06:05 PM   #2 (permalink)
The Contributor
 
hello-world's Avatar
 
Join Date: Feb 2009
Posts: 73
Thanks: 30
hello-world is on a distinguished road
Default

Quote:
Originally Posted by Nora View Post
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.

hello-world is offline  
Reply With Quote
Old 08-23-2009, 07:27 PM   #3 (permalink)
The Addict
 
Enfernikus's Avatar
 
Join Date: Jun 2008
Posts: 335
Thanks: 2
Enfernikus is on a distinguished road
Default

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 )
__________________
My Blog
Enfernikus is offline  
Reply With Quote
Old 08-23-2009, 09:48 PM   #4 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

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();
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 08-24-2009, 12:23 AM   #5 (permalink)
The Contributor
 
hello-world's Avatar
 
Join Date: Feb 2009
Posts: 73
Thanks: 30
hello-world is on a distinguished road
Default

Quote:
Originally Posted by Enfernikus View Post
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.
hello-world is offline  
Reply With Quote
Old 08-24-2009, 09:43 AM   #6 (permalink)
The Acquainted
 
captainmerton's Avatar
 
Join Date: May 2009
Posts: 178
Thanks: 9
captainmerton is on a distinguished road
Default

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.
captainmerton is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
base classes..... allworknoplay Absolute Beginners 16 05-10-2009 08:09 PM
A Generic Singleton Base Class Theo Advanced PHP Programming 7 08-18-2008 02:25 AM
[Tutorial] Basic tutorial about class basics Tanax Absolute Beginners 14 07-24-2008 01:37 PM
PHP5 Classes A to Z Part 1 quantumkangaroo Advanced PHP Programming 11 04-01-2008 04:21 AM
Accessing Variables from Conf file to Upload Class Gareth Absolute Beginners 10 02-22-2008 01:27 AM


All times are GMT. The time now is 02:15 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design