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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:48:35+00:00 2026-06-14T10:48:35+00:00

When using nested objects (ObjTwo as a property of objOne): $objOne->property = new ObjTwo($objOne);

  • 0

When using nested objects (ObjTwo as a property of objOne):

$objOne->property = new ObjTwo($objOne);

What’s the best way to communicate? Here are a few methods I can think of:

Using specific get/set methods

class ObjTwo {
    __construct($objOne){
        $prop1 = $objOne->get_prop1(); 
        // do something with prop1
        $prop2 = $objOne->get_prop2();
        // do something with prop2

        // ... Having to write all these out is kind of a pain
        //     if you're going to have 20+ vars, and there's no
        //     easy way to loop through them.
    }
}

The problem: Writing these out line by line, and having to update it when I add new properties.

I know that having a get/set method for each property is recommended, however I’d really like to loop through the data…

How about get_object_vars()

class ObjTwo {
    __construct($objOne){
        extract(get_object_vars($objOne));
        // do something with the vars
    }
}

The problem: This method bypasses the ability to use getter/setter methods, and each property would have to be public to be accessible.

Dynamic getter/setter method calls

Another way I have considered is to create an array of fields, and have a strict policy of naming the getter/setter methods:

class ObjTwo {
    __construct($objOne){
        $prop_array = array('prop1', 'prop2', 'prop_three');
        $values = array();
        foreach ($prop_array as $prop){
            $values[$prop] = $objOne->get_{$prop}();
        }
    }
}

The problem: Every time I add a new property, I have to make sure to name the get_method() correctly, and update the $prop_array.

Anyone have any better solutions? Maybe just building an array of data?:

$objOne->property = new ObjTwo($objOne->get_data());

I like this solution

Having thought this through, here’s a little clarification: I’m not trying to just make identical copies from parent to child or vice-versa – I edited the above examples to show that a little better. It’s more just the idea of passing a subset of the object’s data from one place to another.

Instead of having to write:

$first_name = $this->member->get_first_name();
$last_name = $this->member->get_last_name();
$email = $this->member->get_email();
$display_name = $this->member->get_display_name();
// etc... and
$this->member->set_first_name($first_name);
$this->member->set_last_name($last_name);
$this->member->set_email($email);
$this->member->set_display_name($display_name);
// etc..

How about having a $this->member->get_fields('first_name', 'last_name', 'email', 'display_name'); method? I don’t like having to remember the field names exactly (fname, f_name, first_name, etc), so you could use class constants:

$data = $this->member->get_fields(array(
    Member::FIRST_NAME, Member::LAST_NAME, Member::EMAIL, Member::DISPLAY_NAME
));

This way, I can loop through the returned data.

foreach ($data as $key=>$value) // ...

And setting the fields…

$this->member->set_fields(array(
    Member::FIRST_NAME => $first_name,   //  THE BIG ADVANTAGE HERE:
    Member::LAST_NAME => $last_name,     //  These field keys auto-complete
    Member::EMAIL => $email,             //  so you don't have to remember them!
    Member::DISPLAY_NAME => $display_name,
    // etc...
));

Still thinking this through… any thoughts?

  • 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-06-14T10:48:36+00:00Added an answer on June 14, 2026 at 10:48 am

    I think you’re asking the wrong question. Furthermore I think it would only be possible to really help, if you provided a real example instead of those pseudo examples. Every real situation is different with regard to the proper solution.

    Generally all your proposals smell. It seems that what you need is not injection but inheritance. If your ‘child’ class really needs access to all or most of the properties of another class, it seems it should extend that class.

    The parts of your software should know as little as possible about each other. In your comment you mention that you have a Member class and a Form class. I don’t know why any of them should know anything about the other at all.

    Furthermore you seem to be under the impression that you need to map every property to a property in the new class. Why? If you pass an instance of a class into another class via custructor (= dependency injection) then you can map that instance to a property and then access all properties of the injected instance via that instance. No mapping needed.

    class Consumer
    {
        private $injectedClass;
    
        function __construct($injectedClass)
        {
            $this->injectedClass = $injectedClass;
        }
    
        public function someFunction()
        {
            //do something by using any property of the injected class
            $this->injectedClass->getProperty();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can PHP PDO extension bind nested objects automatically ? I mean using foreign key
Can you save nested objects in redis? I'm using the node.js driver. One of
JSF 1.1 , using a backing bean with a nested objects inside I can
I have following nested objects. I am using @Valid for validation in my controller.
The Good: We send a JSON array of nested objects using JQuery and .ajax().
Have some nested objects that I'd like serialize using JSON. The problem is that
I am using predefined objects nested inside static class to store objects information at
I'm using jQuery's .data() method to store some data using nested objects like this:
I am using a nested plist in order to create a structure of objects
I'm attempting to implement nested object forms for my site, using Ryan Daigle's blog

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.