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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:12:37+00:00 2026-05-26T07:12:37+00:00

I am creating a class that implements the composite pattern; the class is supposed

  • 0

I am creating a class that implements the composite pattern; the class is supposed to be able to represent a tree (so let’s call it Tree).

In order to use as little memory as possible, when a Tree class instance is passed an array, it is kept as an array until the index is required; at which time a new child Tree instance is created and returned

The reasoning for this: My Tree instances pass through many filters and their content might be traversed and modified by many observers, but their content will be requested only once (on the final rendering). Moreover, a certain chunk might not be rendered at all, so it would be wasted memory to create instances of Tree that are not even going to be used.

In other words, if one uses

$class->set($array,$index); //(or $class[$index] = $array)

, the array is stored as normal. But when someone uses

$class->get($index) //(or $class[$index])

, a new instance of Tree will be returned (and cached for subsequent calls).

However, I am left with a dilemma: Do I

  1. create a new instance of Tree when someone sets data?
    • Pros: code is easy to write, easy to maintain, easy for someone to pick up and improve upon
    • Cons: memory consumption, even more if we consider a part of the data entered might not be used. Manipulation gets more messy as special methods have to be written for special cases (as opposed to dealing with native arrays).
  2. leave it as is, but have people do $class[$index] = array($index2=>$array)?
    • Pros: everything stays stored as an array, so normal array functions work, and memory consumption is
      minimal.
    • cons: the usage is less “elegant”, and possibly more complex.

I know this is not a forum, so I am not asking for opinions, but for the advice of someone having already developed a (potentially heavy) tree structure in PHP and what is the best way (or “accepted way”) to go about it.

  • 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-26T07:12:38+00:00Added an answer on May 26, 2026 at 7:12 am

    Demo creating the Tree on construct and Demo creating Tree on demand are simple tests you can run for yourself.

    The latter creates new node objects only when they’re accessed, thus the memory consumption is not that high.

    The beauty of object oriented programming, encapsulation and facades is that nobody cares what you do internally, as long as the API does what it’s supposed to. Whoever / Whatever is using your TreeNode doesn’t need to know how it functions internally. Just go with whatever feels right. Design your APIs right and you can change internals any time.

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

Sidebar

Related Questions

I'm creating a specialised proxy class that implements IList<T> and wraps an internal List<T>
I am creating a class that implements the SipListener interface to receive events from
When creating a class that has internal private methods, usually to reduce code duplication,
I am creating a class that determines which of number of registered WCF client
I'm creating a class that derives from List... public class MyList : List<MyListItem> {}
I'm in the process of creating a class that stores metadata about a particular
im creating a Factory class that will contruct and return an object. I normally
I'm creating a class named TetraQueue that inherits System.Collections.Generic.Queue class overriding the Dequeue method,
How would I go about creating a real world form creation class that I
Currently I have created a ABCFactory class that has a single method creating ABC

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.