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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T00:20:08+00:00 2026-05-20T00:20:08+00:00

Ok so at work we have discovered that this method to fetch related data

  • 0

Ok so at work we have discovered that this method to fetch related data / objects is something near awesome.

Right to example, i have this class with some related objects inside:

class Country {

private $language; //$language will be an object of class Language
private $regions; //$regions will be an ARRAY of Region objects

//in the constructor i don't load regions or language

//magic method
    public function __get($name) {

        $fn_name = 'get_' . $name;

        if (method_exists($this, $fn_name)) {
            return $this->$fn_name();
        } else {
            if (property_exists($this, $name))
                return $this->$name;
        }

        return $this->$name;
    }

    public function get_language () {

        if (is_object($this->language)) return $this->language;

        $this->language = new Language($params); //example

        return $this->language;
    }

    public function get_regions () {

        if (is_array($this->regions)) return $this->regions;

        $this->regions = array();

        $this->regions[] = new Region('fake');
        $this->regions[] = new Region('fake2');

        return $this->regions;
    }
}

so the idea is:

i want an instance of Country, but i dont need its language and regions now.

In another case i need them, so i claim them as properties, and the magic method retrieves them for me only the first time.

$country = new Country();

echo "language is". $country->language->name;

echo "this country has ". sizeof($country->regions)." regions";

This on-demand method (that avoids nested loop of related objects too) has a name?
Maybe lazy loading properties? On-demand properties?

  • 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-20T00:20:09+00:00Added an answer on May 20, 2026 at 12:20 am

    In another case i need them, […] retrieves them for me only the first time.

    Initialize would be the proper wording. This is called Lazy Initialization.

    http://en.wikipedia.org/wiki/Lazy_initialization

    so i claim them as properties, and the magic method retrieves them

    This is called Overloading of properties.

    http://php.net/__get

    EDIT:

    I don’t think there’s a term for a combination of the two. You can just combine both and get something like “Lazy initialization of properties through overloading“.

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

Sidebar

Related Questions

I have discovered that in SQL Reporting there might be a problem. I have
I have a web site in asp.net that uses a master page. In this
I have a wierd problem that i need to work out how to resolve,
I have found this example on StackOverflow: var people = new List<Person> { new
I have an Excel 2007 workbook that contains tables of data that I'm importing
I have to work with text that was previously copy/pasted from an excel document
At work we have an application to play 2K (2048*1556px) OpenEXR film sequences. It
At work we have two competing theories for salts. The products I work on
At work I have a standard desk (4 legs, flat surface, you get the
At work we have 4 people working together on a few different projects. For

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.