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

Ask A Question

Stats

  • Questions 541k
  • Answers 541k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If you look at the implementation, you'll see case class… May 17, 2026 at 2:49 am
  • Editorial Team
    Editorial Team added an answer It's really easy. A non-strict (e.g. lazy) evaluation means that… May 17, 2026 at 2:49 am
  • Editorial Team
    Editorial Team added an answer One of the randomly generated characters was an ampersand. It… May 17, 2026 at 2:49 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I normally call perl scripts from PHP as below and pass in variables this
PHP creates files with apache:apache ownership which seems to be causing issues with other
All the popular PHP frameworks today use their own view layer implementation that is
I have a php page which allows a user to sort pieces of information
I am forking some php scripts and I need to update a mysql row
I got in every php project (around 25!), some sh scripts that help me
I've worked with XAMPP, WAMPP, MAMPP, etc and am starting to look at Django.
Where can I find an editor/IDE with the ability to display a JavaScript code
and thanks for taking a look at the question. The background I have several
When working with open source project (like wordpress, drupal, joomla) I always find in

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.