0

I use from joomla.
I have a custom library in joomla.
We for avoid excessive memory,use static classes like :

class foo
{
    publc static function hello()
    {
        return "HI";
    }
}

And call it with:

foo::hello()

It's best way?
We want to use the functions in the class without any instance.any technical reason for that?
How singleton?

4
  • 2
    "We want to use the functions in the class without any instance" --- any technical reason for that? Commented Apr 8, 2014 at 10:46
  • 2
    Not the best way. You have a lot of learning to do. Don't use static classes just because you think it's using less memory. Commented Apr 8, 2014 at 10:47
  • 1
    It might seem overkill, but do read up on dependency injuection container. Or do read up on PHP programming in general phptherightway.com Commented Apr 8, 2014 at 10:49
  • @N.B. what else should be considered before using static functions? Commented Apr 8, 2014 at 10:49

3 Answers 3

1

An instance doesn't use memory by itself. What uses memory are the file and the instance info. If you use static classes, you will use the same amount of memory. This is an extreme way of optimizing your code. Not really good, but i will give you points for trying.

Sign up to request clarification or add additional context in comments.

4 Comments

which way??using static class instead of singleton class?
Either. When you load a class, you load it's entire file. If your static method makes references to other methods and stores data in the memory, well.. it doesn't really mather if you store it staticly or with instances. Conclusion: static vs instance doesn't necesarly implies memory optimization.
An instance does use memory. It has to store at least the properties of the instance and a vpointer.
The instance itself does not use memory. The properties do. Static methods can also make use of class properties, btw. That also uses memory. When deciding between instances and static calls memory optimization is not an issue.
1

Just don't put the functions in a class at all. This isn't Java.

function hello() {
    return "HI";
}

Comments

1

This is one of those design patterns which is good to learn, has good intentions but has inherent weaknesses which make experienced programmers initiate gag-reflexes when it is mentioned. In short, over-use of the singleton pattern will inhibit growth and the re usability of your code as the project grows. The basic intent of the singleton pattern to prevent multiple instantiations of the same object as various sections of a web framework access your singleton throughout a single page rendering.

If you're at the point of wanting to use patterns to simplify your code, that is a good thing; but be sure to apply the right patterns to the correct problems. Examples being Registry, Factory, Abstract Factory and Dependency Injection being a few I would explore when starting out.

Finally, here is an example of a singleton class for PDO connections from one of my many php books, PHP Master: write cutting edge code.

class Database extends PDO
{
    private static $_instance = null;

    private function __construct() {
        parent::__construct(APP_DB_DSN, APP_DB_USER, APP_DB_PASSWORD);
    }

    public static function getInstance() {
        if(!(self::$_instance instanceof Database)) {
            self::$_instance = new Database();
        }

        return self::$_instance;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.