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

  • SEARCH
  • Home
  • 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 7830841
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T11:25:38+00:00 2026-06-02T11:25:38+00:00

I want to implement a class, which caches some internal results. These results are

  • 0

I want to implement a class, which caches some internal results. These results are the same for all instances of the class, i.e. it might be wise to share this cache among all instances.

However, these results may be different for sub-classes, i.e. the cache shall not be shared with sub-classes. Since the cache is a good idea for all sub-classes as well, the mechanism shall be inherited nevertheless. But each sub-class has to use a different static array.

I can think of various hacks and complicated patterns to attain this goal, but none looks really sane. Does anybody know of an efficient pattern in PHP?

  • 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-06-02T11:25:39+00:00Added an answer on June 2, 2026 at 11:25 am

    A combination of a private static variable to hold the cached data for all subclasses and protected accessor functions sounds like it would work, and it’s not too complicated:

    class Base {
        private static $cache = array();
    
        protected getCacheItem($key) {
            $type = get_class($this);
            if (!isset(self::$cache[$type])) {
                self::$cache[$type] = array();
            }
    
            // add checks to taste
            return self::$cache[$type][$key];
        }
    
        protected function setCacheItem($key, $value) {
            // similar to the above
        }
    }
    

    Taking it from here, you can get fancy so that accessing the cache is very convenient at the price of becoming somewhat evil:

    class Base {
        private static $cache = array();
    
        // WARNING! QUESTIONABLE PRACTICE: __get returns by reference
        // Also, overriding __get ONLY might not lead to an intuitive experience
        // As always, documenting non-standard behavior is paramount!
        public function &__get($name) {
            if ($name != 'cache') {
                // error handling
            }
    
            $type = get_class($this);
            if (!isset(self::$cache[$type])) {
                self::$cache[$type] = array();
            }
    
            return self::$cache[$type];
        }
    }
    

    You would then be able to use this like

    $b = new Base;
    $b->cache['foo'] = 'bar';
    

    See it in action.

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

Sidebar

Related Questions

Using MSDN example I want to implement some listener for simple class which periodically
I want to implement a class which will handle all HTTP Requests of my
I want to implement a (class) method attr_accessor_with_client_reset , which does the same thing
I want to implement a class which can be used by two classes of
I want to implement a generic method on a generic class which would allow
I want to implement my own copy method for a class A which looks
I have a class that instantiates two classes which implement interfaces. I want one
I want to implement a class which its fields could change (add new fields)
I have a class which uses a HashSet and I want the class implement
I want to implement a string class which has an option to create the

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.