Why are member variables usually private?

2019-05-11 23:59发布

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!

标签: php oop
7条回答
冷血范
2楼-- · 2019-05-12 00:09

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();
查看更多
趁早两清
3楼-- · 2019-05-12 00:12

Making variables private keeps calling code from depending on the implementation details of your class so you can change the implementation afterward without breaking the calling code.

You can declare more than one variable on the same line and only use private or public once:

private $number_of_floors = 5, $color;

See also PHP docs' "Classes and Objects".

查看更多
你好瞎i
4楼-- · 2019-05-12 00:12

I really dont know how to answer such question, but i describe u the best as per php manual

The visibility of a property or method can be defined by prefixing the declaration with the keywords public, protected or private. Class members declared public can be accessed everywhere. Members declared protected can be accessed only within the class itself and by inherited and parent classes. Members declared as private may only be accessed by the class that defines the member.

Property Visibility

Class properties must be defined as public, private, or protected. If declared using var, the property will be defined as public.

Methods declared without any explicit visibility keyword are defined as public.

for more information see Visibility

查看更多
Ridiculous、
5楼-- · 2019-05-12 00:16

Senad's answer is correct in that it is good programming practice to make variables you do not want exterior methods to access private.

However, the reasoning for it in memory managed/garbage collected languages is that when you maintain a reference to an object, it is not able to be garbage collected, and can cause memory leaks.

Hope this helps!

查看更多
做个烂人
6楼-- · 2019-05-12 00:30

Rule of thumb is to try to hide information as much as possible, sharing it only when absolutely necessary.

  • Russian coders sometimes say Public Morozov at unnecessarily wide access modifiers, alluding to a story about improper information disclosure and about further punishment caused by that - Pavlik Morozov:

    a 13-year old boy who denounced his father to the authorities and was in turn killed by his family...

查看更多
ら.Afraid
7楼-- · 2019-05-12 00:31

It's to make the coding easier for you, and to make you less likely to make mistakes. The idea is that only the class can access its private variables, so no other classes elsewhere in your code can interfere and mess something up by changing the private variables in unexpected ways. Writing code like this, with a bunch of autonomous classes interacting through a small number of strictly controlled public methods, seems to be an easier way to code. Big projects are much easier to understand because they are broken up into bite sized chunks.

查看更多
登录 后发表回答