Page 1 of 1

Restricting Class Instances (PHP 5)

Posted: Thu Jul 03, 2008 5:14 pm
by Bon Bon
I have a MySQL database wrapper that restricts the implementation of using a database wrapper for another database like Oracle for instance because the class is called Default_Database and the reason for this is it is used throughout the framework I am working on for different classes, therefore using MySQL_Database would not permit the swapping of databases for whatever reason someone may have.

If I change my Default_Database name to MySQLi_Database and then extend the class like so:

[Example 1]

Code: Select all

class Default_Database extends MySQLi_Database {
  private function _connect () {
    self::$_db->real_connect('127.0.0.1', 'root', '*****', '*****');
  }
}
This allows me to easily set a default database for whatever database I want to use but the only problem is if somebody chooses to use MySQLi_Database they can create another connection to the same database depending on what parameters have been supplied in the following:

[Example 2]

Code: Select all

self::$_db->real_connect('127.0.0.1', 'root', '*****', '*****');
for the MySQLi_Database class.

No matter what is used here there is going to be a problem, passing no parameters will attempt a connection based default values and passing wrong values will cause errors, either way we either connect to 2 different databases when not required, connect to the same database twice (if persistent connections are not enabled), or plain and simply get lots of errors.

As the MySQLi_Database class has lots of useful methods that return values, I want this functionality to be easily extended should somebody need to connect to 2 different databases like in [Example 1] but I want to stop people creating an instance of MySQLi_Database itself. Is this at all possible?

Cutting a long story short, I want to be able to extend a class that is restricted so that the only way it can be used is when it is extended.

Re: Restricting Class Instances (PHP 5)

Posted: Thu Jul 03, 2008 6:10 pm
by John Cartwright
Bon Bon wrote:Cutting a long story short, I want to be able to extend a class that is restricted so that the only way it can be used is when it is extended.
I didn't fully understand your post, but it sounds like you want to declare your MySQLi_Database class as abstract.

I.e.,

Code: Select all

abstract class MySQLi_Database {
 
}

Re: Restricting Class Instances (PHP 5)

Posted: Fri Jul 04, 2008 4:01 am
by Bon Bon
I'm not sure if I am doing something wrong here but when I tried using the abstract keyword I got the following error message:
Cannot instantiate abstract class MySQLi_Database

I have included the file I am working on in a zip as it's getting quite long, so you can download it at:
http://www.stocktonwebdesign.co.uk/mysqlim.zip

Re: Restricting Class Instances (PHP 5)

Posted: Fri Jul 04, 2008 4:26 am
by John Cartwright
Isn't that exactly what you wanted? -- to not be able to initialize the MySQLi_Database object directly.

Re: Restricting Class Instances (PHP 5)

Posted: Fri Jul 04, 2008 4:30 am
by Bon Bon
Yes but the thing is I thought by extending the class I wasn't, I am guessing then that I am doing something wrong but I can't work out what. I am not directly accessing the class but I still get the error?

Re: Restricting Class Instances (PHP 5)

Posted: Fri Jul 04, 2008 4:48 am
by John Cartwright
I took a look at your code, and it seems you are implementing your connection class as a singleton. This means that your abstract class is creating an instance of itself, and because it is abstract this is not allowed.

Your going to have to rethink your design.

Re: Restricting Class Instances (PHP 5)

Posted: Fri Jul 04, 2008 7:09 am
by Bon Bon
Thanks for the help, I will rethink how to get everything working how I want it to although I did play with it earlier and had no luck so I am going to sit down with a pen and paper to work out what is what.