Which class properties must be defined as private/protected/public in top of class?

Each and every class property must be defined as public/protected/private in top of class, or just properties that may cause E_NOTICE should be defined there?

you should always declare the property/method visibility. makes the code much better to understand. and use public properties only, if there is no constraint on the property (neither for its data type, nor its value).

I asked visibility of each and every properties of class should be declared or how to decide visibility of which properties must be declared?

I think of public protected private in terms of Java which may not equate to how PHP looks at them.

What is your take on how they differ?

I am not familiar with hava to compare. Let’s talk about php only.

Fine with me.

So my question remains,

What is your understanding of public vs. protected vs. private?

My understanding is that private is used for the all the properties and any internal methods and public is used for the methods the rest of the code will use to interact with the object. You modify private to protected for whatever a new object that inherits from this one will need access to in addition to the public methods (usually just some of the internal methods).

The usage shouldn’t change between OO languages as this is methodology related and not language related.

2 Likes

Thanks felagl, what about my initial question?

No answer for my original question?

Maybe try only using protected and see what does not work :slight_smile:

If there are errors change to private. Sill errors? Change to public :slight_smile:

Have you searched for a solution?

Sorry for confusion. My question is not “which property should be defined as what”, my question actually is “Each and every properties must be defined at top of class at all” ?

@nimasdj

PHP’s manual has quite a good explanation if there is anything explicit that is not clear, please quote the reference, your understanding and what is not clear.

http://php.net/manual/en/language.oop5.visibility.php

If I understood correctly, each and every property must be defined…

You understood wrong. Properties don’t need to be defined at all. Try it. And maybe take a peek at the manual.

By “define” I meant setting the visibility of them. If not all properties should have a visibility type, so which one of them should have a visibility type in top of class? Well, it seems I am just getting useless answers here!

From the manual:

http://php.net/manual/en/language.oop5.visibility.php


Visibility ¶

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 classes.
Members declared as private may only be accessed by the class that defines the member.

Simplified the above by adding line-feeds.

If a properties visible access scope is not defined it will default to public. But think of them as access gateways to your methods and properties of your class/object.

public - used by the class and can be used by other classes once an instance has been created.
protected - can be used by the class and only used by other classes that extend the class
private - can only be used internally by the class.

1 Like

Thanks.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.