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 1064273
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T18:54:30+00:00 2026-05-16T18:54:30+00:00

When I find scripts for php or look at php frameworks I see a

  • 0

When I find scripts for php or look at php frameworks I see a “registry class” or a “container class” which often holds variables or other objects utilizing the __get magic method.

Here is a oversimplified example of what I mean:

example 1:

class container {
 private $objects;
 public function __get($class){
  if(isset($this->objects[$class])){
   return $this->objects[$class];
  }
  return $this->objects[$class] = new $class();
 }
}

the above example will have more functions to it when creating the class instead of just calling it but for my example it should be enough.
“example 1” is how I mostly see it in scripts downloaded from the internet, it maintains a single class instance, now what I’m wondering is that wouldn’t this example do the same thing and be more efficient:

example 2:

class simplecontainer {
 public function __get($class){
  return $this->$class = new $class();
 }
}

But I never see “example 2” in other peoples scripts which makes me think twice before even considering to use it.

I tested container vs simplecontainer using several classes that they would contain and re-use around 100000 times and “example 1” does it in 0.75seconds on my local machine, and “example 2” does it in 0.29seconds.

which should I use in my scripts? example 1 or example 2? and why?

  • 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-16T18:54:31+00:00Added an answer on May 16, 2026 at 6:54 pm

    Because in yours I can’t do this:

    class container {
        private $foo = "I'm only a var";
        private $objects;
        public function __get($class){
            if(isset($this->objects[$class])){
                return $this->objects[$class];
            }
            return $this->objects[$class] = new $class();
        }
    }
    
    class simplecontainer {
        private $foo = "I'm only a var";
        public function __get($class){
            return $this->$class = new $class();
        }
    }
    
    class foo {
        public $bar = "wibble";
    }
    
    $c1 = new container();
    $c2 = new simplecontainer();
    
    $foo = $c1->foo;
    $foo->bar = "Don't you forget about me!";
    unset($foo);
    $foo = $c1->foo;
    echo $foo->bar;
    // Don't you forget about me!
    
    $foo = $c2->foo;
    $foo->bar = "Don't you forget about me!";
    unset($foo);   
    $foo = $c2->foo;
    echo $foo->bar;
    // wibble
    

    Your version relies on no class name being the same as a private member of the container object. Ok, so it should be simple to avoid, but would be a pain tracking down a bug for. Safe coding means less stress.

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

Sidebar

Related Questions

Right now, I have a script which uses PHP's tokenizer to look for certain
My scripts (php, python, etc.) and the scripts of other users on my Linux
Is there any php script to find the name of the web server like
I can't find any PHP script to compute Krippendorff's Alpha . I need this
I'm trying to make a simple php script to find all src attributes from
say a ruby script is running ruby.rb SOMEUSERID using PHP, how can i find
I suck at php, and cant find the error here. The script gets 2
I cannot get my Spring web app to find my scripts. I have the
I've downloaded the WTL 8.0 package and come to find the scripts to install
I just started writing powershell scripts for Citrix XenServer. However, I can only find

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.