Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 553233
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T11:35:00+00:00 2026-05-13T11:35:00+00:00

In PHP if I create a singleton method for like 5 different caching classes

  • 0

In PHP if I create a singleton method for like 5 different caching classes (memcache, apc, session, file, cookie)

Then I have a main cache class which basicly routes my set() and get() methods to the appropriate caching class. Now lets say I need to use the session, cookie, memcache, and file cache all on the same page. My main cache class would then need to create a new instance 1 time for each of these cache types using a singleton.

SO I would then need to call my singleton methods many times on a page, if I were to set/get 30 different calls to the cache on 1 page, it would call the singleton method that many times.

I am wondering if it is bad practice or not very good to keep calling my singleton over and over on a page?


UPDATE

Below is some code I have started, in it you can get a better example of what I am trying to do… If I were to add something to memcache 40 times on a page, it would call the singleton method for ym memcache class 40 times

/**
* Set a key/value to cache system.
*
* @param   string        type of cache to store with
* @param   string|array  keys, or array of values
* @param   mixed         value (if keys is not an array)
* @return  void
*/  
public function set($type, $keys, $value = FALSE, $options_arr)
{
    if (empty($keys))
        return FALSE;

    if ( ! is_array($keys))
    {
        $keys = array($keys => $val);
    }

    // Pick our Cache system to use
    switch ($type) {
        case "memcache":
            // Cache item to memcache
            $this->memcache = Memcache::getInstance();
            $this->memcache->get($keys, $value, $options);
            break;

        case "apc":
            // Cache item to APC
            $this->apc = APC::getInstance();
            $this->apc->get($keys, $value, $options);
            break;

        case "session":
            // Cache item to Sessions
            foreach ($keys as $key => $val)
            {
                // Set the key
                $_SESSION[$key] = $val;
            }
            break;

        case "cookie":
            // Cache item to Cookie
            break;

        case "file":
            // Cache item to File
            break;
    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T11:35:01+00:00Added an answer on May 13, 2026 at 11:35 am

    Generally speaking, using singletons is more and more considered as a bad practice (one reason is they make unit-testing harder, if not impossible).

    To illustrate that : both symfony and Zend Framework, for their next major version (2.0) are trying to remove as many singletons as they can from both frameworks.

    That being said, singletons should be used when it makes sense to have one and only one instance of a given class ; does it make sense in your case ? Up to you to decide 😉

    If you don’t want to call your singleton over and over again, you might want to store it in a local variable — I suppose it might be seen as some kind of (maybe “premature” / “useless”) optimization.

    For instance, instead if having this kind of code :

    Singleton::getInstance()->method();
    Singleton::getInstance()->method();
    Singleton::getInstance()->method();
    

    You’d have :

    $s = Singleton::getInstance();
    $s->method();
    $s->method();
    $s->method();
    

    Not sure how much you’ll gain from that, though…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a script that parse the database and create php classes to work
Possible Duplicate: PHP : Create array for JSON I have an array like this
Possible Duplicate: PHP create a file without fopen I want to create a file
I am using PDO with PHP to create a new database and then a
Hi i'm parsing an XML file using PHP to create another XML file in
I have always used a Singleton class for a registry object in PHP. As
i has singleton class with final static method getInstance(): <?php abstract class Singleton {
<?php // create a new CURL resource $file_path = '/mail'; define(COOKIE_FILE, c:\cookie.txt); $ch =
starting at the top of the view: <?php ### CREATE SESSION ** $this->load->library('session'); $this->load->library('encrypt');
is it possible to create a singleton class in PHP 4? Right now I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.