How to implement classes that should be loaded once?

I’m building a little framework, currently it is just for fun. I’m wondering how should I implement classes that should be instantiated by the framework and be passed to the controller to use?

For instance: I have an “input” class that houses functions which take POST and GET data and filter it. The class is also used by the framework for routing and similar things.

Should I make these methods static? Should I use the singleton pattern? Or maybe should I make something like Codeigniter has – a loader that stores instances of all instantiated classes?

2

Most of these answers are suggesting using the Singleton pattern or using static, but I disagree with this. Yes, this will get the job done, but IMHO, it’s a bad practice. It’s harder to test and isolate code that uses your Singleton/static code so it’s not ideal. I’d use the Inversion of Control pattern/Dependency Injection pattern. Basically it works like this (Note that I come from a Java background):

class INeedOneInstanceOfAClass {
    OneInstanceOfAClass oneInstanceOfAClass;

    public void setOneInstanceOfAClass(OneInstanceOfAClass oneInstanceOfAClass) {
        this.oneInstanceOfAClass = oneInstanceOfAClass;
    }
    // ...
}

Some other code is responsible for calling that setter. The code that calls the setter makes sure it only creates one instance of OneInstanceOfAClass and it passes that same instance into all the other classes you have with a setOneInstanceOfAClass method. As a result, there’s only one instance of OneInstanceOfAClass but, OneInstanceOfAClass is not a Singleton and it doesn’t need to use static anywhere. It’s just a regular class that you only instantiate once.

Now it’s really easy to isolate INeedOneInstanceOfAClass in a unit test. Your unit test creates INeedOneInstanceOfAClass and passes in a fake OneInstanceOfAClass that does whatever it needs to do to get out of the unit test’s way.

what you looking for “A SINGLE instance of a class” the way to go is with a Singleton pattern.

basic implementation of a singleton pattern.

Class Test
{
    $private $testInstance;

    private function __construct() { };

    public static function getInstance();
    {
        if ($this->testInstance === null) {
            $this->testInstance = new self();
        }

        return $this->testInstance;
    }
}

and from other place you get that class instanciated with

$test = Test::getInstance();

every time you’ll need the object you call the getInstance static method and it will return the object, or create it and return it.

hope it helps.

I suggest make them static if you can, that is if your class doesn’t have to save it’s state. If it does, I think you can better make some kind of framework class that has one member of type INPUT. When you want to use the framework you instantiate a framework object, which instantiates one INPUT object. You drag that framework object everywhere you need the methods from INPUT. It’s better debuggable and maintainable than a singleton. also, this: https://stackoverflow.com/a/138012/640888

When utilizing Singleton pattern, you also need to make __clone() private

class Single
{
    ... some code here ...
    private function __clone(){}
}

so that you can’t do something like this:

$instance = Single::getInstance();
$second = clone $instance;

when NOT done like this, you are getting two separate instances, and if __clone() is private this will produce error

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật