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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:26:39+00:00 2026-05-24T03:26:39+00:00

I don’t know what’s the best practice for this. Let’s say I have some

  • 0

I don’t know what’s the best practice for this.
Let’s say I have some hierarchical objects:

Neighborhood
Family

So each Family lives in a Neighborhood.
The objects are mapped like this:

class Neighborhood {
  public $id;
  public $name;

  public $nr_families;
}

class Family {
  public $id;
  public $name;
  public $neighborhood_id;
}

Each Object models it’s own database table. All objects of a type are kept in an array in the global scape, so there are 2 arrays.

How can I know, FROM INSIDE the Neighborhood object, at the CREATION DATE, how many Families are in that Neighborhood, without quering the database again or accessing the global array of Families? Let’s say the Family array was generated first, they are already there, and now when I generate the Neighborhood array, these objects need to have a property with the total number of families in each. This property doesn’t match a database field, it has to be calculated on the fly.

Is there a design pattern? Should I use some kind of third object?

  • 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-24T03:26:40+00:00Added an answer on May 24, 2026 at 3:26 am

    If you don’t want to use extra queries, you need to loop over the list of families to check the family’s neighborhood id. Because using global variables is discouraged, you can use some dependency injection to tell the neighborhood about the families.

    Say your Neighborhood object has a static variable that contains the list of families. Outside your object, you set that variable:

    Neighborhood::$families = $families;
    

    Neighborhood can now loop over that list, checking the family’s neighborhood id:

    class Neighborhood {
        function getNumberOfFamilies() {
            $myFamilies = array();
            foreach(self::$families as $family) {
                if($family->neighborhood_id === $this->id) {
                    $myFamilies[] = $family
                }
            }
            $this->nr_families = count($myFamilies);
            return $families;
        }
    }
    

    Would that work?

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

Sidebar

Related Questions

Don't know if this has been answered before. Have custom routes to users. If
I don't have much PHP experience and I want to know how to best
I don't know how jQuery works under the hood, but let's say that at
I don't know if anyone has seen this issue before but I'm just stumped.
I don't know if thats right but for some reason my stored procedure is
I don't know if this is possible. I display a table of results and
I don't know if i am doing it right, this is what I got:
Don't know if there is a better way to do this, so that is
Don't know what to do with this error. How to add data in SQL
Don't know if this is an eclipse specific problem but whenever I declare a

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.