Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Performance when Instantiating an object in php

I would like to ask about the performance and memory usage of instantiating an object in php.

On every php file of my application, i included a file which connect to the database.

$db->DB::getInstance();
$db->connect('all my credentials');

This is the getInstance() function in my db class.

//singleton database instance
public static function getInstance() {
    if (!self::$Instance) {
        self::$Instance = new DB();
    }

    return self::$Instance;
}

Currently everything turns out well. But i am concern of performance issues as in, can it be improved, possbile flaws etc.

I researched and found out that singleton instance can help to save memory. It will reuse the object if it's already been instantiate. Am i right?

My exact question is

E.g. if i have 10 users accessing the script, does it means that the object will be instantiate 10 times? When that happens, will it put 10 x load on my memory usage? -> this is pretty what i am interested in

Appreciate any expert advice.

like image 781
Slay Avatar asked Feb 19 '26 00:02

Slay


2 Answers

As with all performance tweaking, you should measure what you're doing instead of just blindly performing some voodoo rituals that you don't fully understand. When you save an object in $_SESSION, PHP will capture the objects state and generate a file from it (serialization). Upon the next request, PHP will then create a new object and re-populate it with this state. This process is much more expensive than just creating the object, since PHP will have to make disk I/O and then parse the serialized data. This has to happen both on read and write.

In general, PHP is designed as a shared-nothing architecture. This has its pros and its cons, but trying to somehow sidestep it, is usually not a very good idea.

like image 185
NullPoiиteя Avatar answered Feb 21 '26 12:02

NullPoiиteя


Do not use Singleton's, it makes testing a hell.

Objects are sent around the system in PHP as references by default. So you can simply share that Database connection object around other classes in the system without performance hit (it won't clone them until you specifically order it to be cloned).

But yes, if ten users use your system then that means you will have ten instances of that class. And it means that memory usage is ten times higher IF all the requests of users are active at the same time (well, usually, depending on load and server settings, some requests may have to wait until others are complete).

It is possible to tell PHP and database to use persistent connections, which means that other scripts that use the same database username and password and connect to the same database, share the 'connection'. But this is almost never recommended, since requests from one user may slow down the experience of another user who has to wait while the previous users database query has completed.

So, in short, do not use Singletons. In a smart object-oriented architecture, the class is passed to child objects or inherited from parents. This means that the class can easily be changed or a mockup can be easily created for testing purposes.

like image 34
kingmaple Avatar answered Feb 21 '26 13:02

kingmaple



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!