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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T02:43:36+00:00 2026-06-16T02:43:36+00:00

Consider this hypothetical scenario: I have a Stomach class, which has a contents property,

  • 0

Consider this hypothetical scenario:

I have a Stomach class, which has a contents property, to which objects of class Food should be assigned. One way to do that would be to use a kind of a setter, e.g. setContents($food).

But lets suppose that we assign food directly, as in $stomach->contents = $orange.

Suppose also that whenever a Food object is assigned to contents we need to change the object’s eaten property to true. Is there a way to do that without using a method of an object that it’s being assigned to (in this case, the $stomach‘s setter)?

Basically, my question is: can we call a method whenever an object is assigned as a property to another object? Also, even if it is possible, is it bad design? Sorry if this is a stupid question, all of this is pretty new to me.

  • 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-16T02:43:37+00:00Added an answer on June 16, 2026 at 2:43 am

    The best OOP solution here would be to create a method that indicates an action, like eat().

    To ensure that the right Object is eaten, you could define an Interface (Say Food). This interface may define a method setEaten(). The Stomach (though I would prefer Mammal or something similar that actually can eat) can then call setEaten().

    Since it created some controversy in the comments, I want to point out that an object definition should as closely as possible reflect what it actually does. To reduce object coupling it is best to avoid directly accessing object properties from an other class, however there is nothing wrong with using setters instead of actions when it makes sense (note that I used one in the definition of Food), and it may often depend on the developer view.

    However, here it makes sense. Consider the case “A monkey eats a banana” (for simplicity please allow me to just materialize that banana out of thin air).

    $m = new Monkey();
    $m->eat(new Banana());
    

    perfect. Now lets try a setter.

    $m->setContents(new Banana());
    

    Now we have a problem, the monkey already contains a lot of things, like bones, blood, muscles a brain, etc. So setContents makes no sense here. You could try $m->getStomach()->setContents(new Banana()) but that would just increase object coupling.

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

Sidebar

Related Questions

Consider this very simple example, where I have template wrapper class for which are
Consider this example - I have a class called Report that has a field
Consider this problem: I have a program which should fetch (let's say) 100 records
consider this scenario: I have loaded a Parent entity through hibernate Parent contains a
Consider this hypothetical implementation of vector : template<class T> // ignore the allocator struct
This question requires some hypothetical background. Let's consider an employee table that has columns
Consider this simple example - public class Person { private String name; private Date
This is kind of hypothetical as I'm thinking about how to design this. Consider
(This is a hypothetical question for discussion, I have no actual problem). Say that
Consider this DialogFragment: public class RollTriggerDialog extends DialogFragment{ private ProgressDialog _dialog; int _progress; public

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.