Jump to content

what is the best way with OOP and object interaction?


Dânêl

Recommended Posts

Hi,

I'm writing some personal library also to do practice with OOP...

In particular I'm working on a user class which uses database and I want to do it simply reusable in different environments

 

For example I want to do it database type and database class independent. Is it possible?

I will try to explain this.

 

There could be already defined a Database class inside a project. How could I do to use it or mine database class without troubles?

 

I thought to use some class properties as functions (like C function pointer) and editing them with constructor allowing for example, the use of mysql_query instead of mysqli_query or "my_mysql_query" . Is this a good way or not?

 

An abstraction layer could be better but I have to know how are defined third part db classes, right?

 

 

PS: I know the existence of PDO but it can be disabled by the hosting provider (I know some hoster doing this...)

 

Thanks for any hints

Link to comment
Share on other sites

PS: I know the existence of PDO but it can be disabled by the hosting provider (I know some hoster doing this...)

 

 

There are also hosters that keep register_globals On, but that generally means that their service sucks.

Find one hosting partner that u are comfortable with (preferrably *not* a free one); they should have enough PHP expertise to realise what decent settings are instead of basing their settings on some blog post saying that PDO is not implemented well in PHP4 (while they ru PHP5) and that register_globals is a handy tool for developers...

 

Most hosting companies know a lot about Linux/apache but nothing about PHP, unfortunately.

 

greetz,

Marv

 

Link to comment
Share on other sites

Never really worked with OOP in PHP, but from what I remember from using them in C# back at uni shouldn't a single interface do the job?

 

Ie. you have an interface 'Database' that must include function query(), function num_rows(), etc, etc. You'd then have classes 'DB_MsSQL', 'DB_MySQL' etc that implents the 'Database' interface. Then within the 'User' class you would have a private member of type 'Database' called $db (or whatever you wish). The 'User' class would then have functions that would use $this->db to call database functions. If the person employing the class 'User' wishes to change to a different type of database they would merely have to change the declaration of $db to $db = new DB_MsSQL(); rather than $db = new DB_MysSQL();

 

Does it not work like this in PHP, or did I miss the point completely (quite possible)?

Link to comment
Share on other sites

Thanks to all for your replies.

 

 

There are also hosters that keep register_globals On, but that generally means that their service sucks.

Find one hosting partner that u are comfortable with (preferrably *not* a free one); they should have enough PHP expertise to realise what decent settings are instead of basing their settings on some blog post saying that PDO is not implemented well in PHP4 (while they ru PHP5) and that register_globals is a handy tool for developers...

 

Most hosting companies know a lot about Linux/apache but nothing about PHP, unfortunately.

 

greetz,

Marv

 

 

I agreed with you, but unfortunately usually I write little script for people who had already hosted their sites and for this I know hosting with PDO, SOAP disabled. I wrote also a gallery in PHP and then discovered that the hoster disabled GD. As a result I had to disable checks and Image resizing. It's a mystery for me the reason why well paid hosting service have these limitations...

 

As regards the answers to my questions. SO what I need is something like this?

<?php
interface db_wrapper
{
//Execute a query
public function query($query);

       // Other functions	
}


class Db implements db_wrapper
{
    private $dbclass;

    public function __construct()
    {

    	$this->dbclass = new TheClassYouLike();
    }
    
    public function query($query)
    {
       $this->dbclass->execute($query);	
    }
}
?>

 

 

Link to comment
Share on other sites

Really ?  :o thanks    8)

 

Is it a good idea to use a function for every different sql istruction (select,update,delete,insert) ?

 

I see them studying some DB classes and abstraction layers taken from OpenSource project and frameworks.

 

I think this way makes a bit hard the use of nested queries (like a Select inside a Select) or JOINs: it requires too much code even if it make possible to use non standard, db specific, sql query or change query only once for (maybe possible?) future  SQL commands update. Isn't it?

 

What is better? A general or a specific query function?

Link to comment
Share on other sites

Different database use different SQL dialects. What works on MySQL can throw a syntax error on Postgres and vice versa. In general, if you're using an abstraction layer, you should refrain from using raw SQL. This means some limitations (and also less performance, since more data handling has to be done in PHP).

Link to comment
Share on other sites

  • 1 month later...

I love it when there are no followed standards....

 

It is quite impossible to have a truly db abstraction layer.

 

How so? It is quite possible to generate queries that will work using different SQL flavours. The key is though to NOT use any straight SQL. One interface is used to do the CRUD operations, different driver adapters for different RDBMS flavours will generate different SQL. It is not rocket science, just a LOT of work.

 

PS: I know the existence of PDO but it can be disabled by the hosting provider (I know some hoster doing this...)

 

What I would recommend is creating a wrapper around PDO, creating a separate adapter for MySQLi. Indeed some stupid hosts have MySQLi enabled but not PDO. But the two are similar enough to create a wrapper around both to get a unified interface. I would use the PDO interface as the base for it. I recently had to create a wrapper around mysqli for Doctrine (the current version really only supports PDO, but they do use a wrapper and a separated interface, which made it possible to create a wrapper around MySQLi that basically implemented the PDO methods that Doctrine uses, but with MySQLi as the backend.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.