Stack user
Stack user

Reputation: 519

class related issue in PHP

I have a doubt in class concept that when we extend a class in php and define an same attribute in that class(derived) which was defined in base class then is it ok or not and why?

Upvotes: 0

Views: 36

Answers (1)

Philipp
Philipp

Reputation: 15629

That depends on the access modifiers and what you want to achieve.

First of all, if you declare your var private, then, the var is only known inside the class, it is declared.

If you want it to be available in any child class, but not from the outside, it should be protected.

Any other var (public available from everywhere) have to be public. But using public isn't recommned, because of data encapsulation. Instead of using public vars, you should write getters/setters.

At least, it's not a good idea, to mix vars with the same name, but different access modifiers - this leads to unreadable/confusing code.

Upvotes: 3

Related Questions