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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T03:50:02+00:00 2026-05-18T03:50:02+00:00

I’m working on a case logging system. Each case can have comments attached, so

  • 0

I’m working on a case logging system. Each case can have comments attached, so I’m going to create a Case class and a Comment class and embed a Comment object in the Case class.

I’m thinking about how to provide the interface to the embedded Comment object. I could make it a public member and have the programmer access it directly, ($case -> comment -> addComment()), or make it private and put methods in the Case class to provide access to the comments through the Comment object (public function addComment () { return ($this -> comment -> addComment ()); }).

Thinking about it, the latter style would mean that the Case object becomes dependant on the Comment object. However, in the former case, the programmer may be able to do things like make the comments property of the Case object reference comments that don’t belong to the case, or even to items that aren’t comment objects!

Of the two approaches, which is considered to be the best practice?

  • 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-18T03:50:03+00:00Added an answer on May 18, 2026 at 3:50 am

    I don´t exactly understand what you mean, but…

    I think you need something like this:

    class Case {
        private $comment;
    
        public function getComment() {
            // Add some checks if necessary
            ...
            return $this->comment;
        }
    
        public function addComment(Comment $comment) {
            // Add some checks if necessary
            ...           
            $this->comment = $comment;
        }
    }
    

    You can also choose to make your $comment propery protected. This is especially useful if you want to extend the object.

    Always make extensive use of encapsulation, it is one of the biggest advantages of OOP. It is always wise to make a class property protected or private. Without it you can’t force checks on it when you set the properties. Keep in mind if you make a property public, every class can access and modify it without any checks. Think about the possible consequences of that!

    $comment isn’t a primitive type, its a reference to an instance of a comment clas

    Good luck!

    @Gordon: Doesn’t matter. Even if it is a instance of a Comment and you make it public, you can still do so something like:

    // $Case is a Case object, doh ;)...
    $Case->comment = "Just a string value";
    

    And suddenly your comment is a primitive type while you expect an instance of a Comment class… This can give you several errors. If you make it a protected or private property the line of code is just not possible outside of the class. Then you’re forced to use a setter for it like:

    public function setComment(Comment $comment) {
        ...
    }
    

    Your function expects a Comment and will throw an error if something else is given. This will ensure your object is consistent.

    @Gordon: It is not a Comment class method you’re building in your Case class. The methods described above are only needed for your Case class. The only thing they do is getting the Comment object that belongs to the Case class and Setting the Comment object that belongs to the Case class. Once you succesfully retrieved the Comment object with the getter, you can fire all public methods defined in your Comment class. So it does nothing with the Comment object itself.

    Your Comment is still completely independent. The relationship between these objects is (most likely) saved in the table of the Case object. Your Case class is the only class that knows about the relationship. So you can still re-use your Comment class for other purposes.

    Your Case class needs to know your Comment class in some way and this is the way to go!

    BTW, consider a many-to-many relationship. I assume a Case can have more then one Comment.

    Good luck!

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

Sidebar

Related Questions

No related questions found

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.