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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T01:13:00+00:00 2026-05-24T01:13:00+00:00

I just started to learn object oriented programming today and just by observation noticed

  • 0

I just started to learn object oriented programming today and just by observation noticed that in all examples, member variables are private. Why is that usually the case?

// Class
class Building {
    // Object variables/properties
    private $number_of_floors = 5; // These buildings have 5 floors
    private $color;

    // Class constructor
    public function __construct($paint) {
        $this->color = $paint;
    }

    public function describe() {
        printf('This building has %d floors. It is %s in color.', 
            $this->number_of_floors, 
            $this->color
        );
    }
}

Also, if you declare the member variable to be public, what is the syntax for accessing it outside of the class it was declared in?

And finally, do you have to prepend “public” or “private” to every variable and function inside a class?

EDIT: Thanks all for your answers, can anyone please confirm if you have to prepend “public” or “private” to every variable and function inside a class?

Thanks!

  • 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-24T01:13:01+00:00Added an answer on May 24, 2026 at 1:13 am

    Private variables can’t be accessed from outside, that gives you control.

    But if you put them Public then you can access it lke this

    $your_object_instance->Your_variable
    

    For example

    $building = new Building();
    echo $building->number_of_floors;
    

    but you have to put your number_of_floors variable to public, if you want to access private member then you need to implement new method in Building class

    public function getNumberOfFloors()
    {
      return $this->number_of_floors;
    }
    

    so your code should look like this

    $building = new Building();
    echo $building->getNumberofFloors();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just recently started with object oriented programming, using java. Before I was programming
I just started programming with objects recently and am trying to learn good habits
I just started using resharper and I am trying to learn all the hotkeys
I just started to learn Ruby and as a .Net developer, I'm wondering if
I just started to learn how to use action listeners. To my understanding it
I've just started to learn about tie . I have a class named Link
I've just started to learn C (using Thinking In C ) and I'm wondering
I have just started to learn Ruby and got a good take on the
I just have started to learn Haskell and combine reading books and tutorials with
Just started getting a bunch of errors on our C# .Net app that seemed

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.