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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:56:20+00:00 2026-06-14T13:56:20+00:00

So lets say I have a class that is composed of other classes. class

  • 0

So lets say I have a class that is composed of other classes.

class HttpRequest
{
public $session = new Session();
// .. the rest of the HttpRequest code
}

Now, I want to have access to Session class through HttpRequest class so Im using composition.
But does this breaks laws of OOP Encapsulation or Data hidding that states that all properties should be protected, and accessed through setter and getter methods?

Is this wrong:

$request = new HttpRequest();
$request->session->set('id', 5);

or should I use this:

$request = new HttpRequest();
$session = $request->getSession();
$session->set('id', 5);

Encapsulation states that properties shoud be protected.
How to provide access to inner classes then? Is the first example wrong as far as proper OOP goes?

  • 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-14T13:56:21+00:00Added an answer on June 14, 2026 at 1:56 pm

    There are valid reasons to not allow direct access to the object:

    • Allows for manipulation of the object outside of the object itself. If you make the property public, any part of your code could overwrite $session on the HttpRequest class, and you’d have a tough time tracking it down. Encapsulation from a data protection standpoint is there to ensure that only the object’s methods can directly alter the object.
    • Allows you to gracefully handle the case in which that variable is not set. If, for some reason, $session does not get set on your class – you’ll immediately have a fatal when you try to call a method on it. If you wrap it in a getter, you could check for that condition and create a new instance of the class on the fly.
    • Follows true “OO” paradigms

    However, in some cases I would say it is okay to do this. Particularly if you know that the property will always be set (and the only way in which it would not be set is not a supported way to use the object).

    It also makes sense depending on how the property is going to be accessed. Symfony2 uses this in their Request classes. It feels natural in that case, as the “query” “post” and “request” vars are all “ParameterBag”s (glorified arrays). However, they do expose a getter for the Session object – likely because of it’s use case.

    In short: it really depends on how you’ll be using the variable. In this particular case, I’d say it doesn’t much matter.

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

Sidebar

Related Questions

Lets say that I have the following code: public class Shelter<A extends Animal, B
Lets say I have the following code in C#: public class AppleTree { public
Lets say I have a class that is doing something like: public class Foo
Lets say you have a class MyClass that has a constructor public Myclass(SomeObject o)
Lets say I have a class A that is fairly simple like this -
lets say i have a background worker in a class that perform db query
Lets say I have a LunchBox class with a property for FreezerPack that has
Let's say I have a class that requires configuration, dependency injection etc. public class
Lets say I have a class that is supposed to generate some ID (for
Lets say I have a class that contains a list of sorted numbers. The

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.