TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Advanced PHP Programming (http://www.talkphp.com/advanced-php-programming/)
-   -   Object Aggregation (http://www.talkphp.com/advanced-php-programming/3218-object-aggregation.html)

Jenski 08-06-2008 04:01 PM

Object Aggregation
 
I'm using a Database Connection class in a web app I am writing as I find myself calling the same functions often.

I was looking into using Aggregation in order to achieve this when I came across this article:

Page 2 - Object Interaction in PHP: Introduction to Aggregation, part 1

But it says:

Quote:

If we talk about performance issues, the advantages of aggregation mainly come from its lower overload, since most of the time only one object is shared by other objects. However, this advantage might be discarded in the case of having a class for database connection shared by other multiple classes. You may run into difficulties if multiple database connections are established to the same server, causing a noticeable detriment to the system, particularly if your site is attracting many visitors.
I was wondering if I could get around this using PHP5s features by cloning the object then unsetting it each time i'm using it, or if there any other alternatives?

Thanks

Jen

delayedinsanity 08-06-2008 09:15 PM

I'm not sure if this is exactly what you're asking, but you're system should be programmed in such a way that every time a unique visitor accesses a page, there should only ever be one distinct connection to the database (not multiple connections for the same visitor). If you're opening a connection three times for a single page view, there's something wrong - a hundred visitors, a hundred connections, there's no avoiding that, and your server will have to be able to handle the load.
-m

sketchMedia 08-07-2008 09:53 AM

I'm not quite sure why aggregation per se would be the cause of this, but bad design would.
If your system is managing to open multiple database connections per execution there is defiantly something wrong.

A singleton DB connection class would combat this somewhat:
PHP Code:

<?php
class DBConn
{
    private static 
$pDB;
    const 
HOST 'localhost';
    const 
USER 'root';
    const 
PASS '';
    
/*
     * Creates a single mysql connection
     */
    
public static function connect()
    {
        if(!
is_resource(self::$pDB))
        {
            
self::$pDB mysql_connect(self::HOSTself::USERself::PASS
                or 
self::throwException('Cannot connect to the database');
        }
        return 
self::$pDB;
    }
    
/*
     * Added so that i can throw exceptions using 'or' on function calls
     * I.E. mysql_connect([params]) or self::throwException([message])
     */
    
public static function throwException($szMsg)
    {
        throw new 
Exception($szMsg);
    }
    
/*
     * cannot create new object from a static class
     */
    
private function __construct() {}
    private function 
__clone() {}
}

To show it in action (sort of):
PHP Code:

class test
{
    private 
$pDb;
    
    public function 
__construct()
    {
        
$this->pDb DBConn::connect();
    }
}
try{
    
$lol = new test();
}catch(
Exception $e){
    echo 
$e->getMessage();


also just a note, mysql_connect will reuse an existing connection if possible (if the same args are passed in, according to the manual) so this method may be on the surface unnecessary, for mysql at least.


All times are GMT. The time now is 06:56 PM.

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