Take the 2-minute tour ×

I keep seeing this format in recent codes and even here:

class Class {
    function this() {}

instead of

class Class {
    [public/private/protected] function this() {}
  1. Isn't it recommended to always specify the function scope?
  2. Isn't the first approach an old one?
  3. How are, in the first approach, defined private and protected functions?
share|improve this question
add comment

5 Answers

up vote 6 down vote accepted

when you declare a function without any keyword that is public by default.

Isn't it recommended to always specify the function scope?

You have to define function scope if you are going to use them as private or protected.

Isn't the first approach an old one?

What's the old and new if they are still accepted by PHP.


you can not with first approach you have to use keywords.

share|improve this answer
add comment
  1. Yes, probably.
  2. It's hard to quantify how new or old a technique is. It may have been less non-recommended in the past, when classes were relatively new in PHP.
  3. "Class methods may be defined as public, private, or protected. Methods declared without any explicit visibility keyword are defined as public."
share|improve this answer
add comment
  1. Yes it is. It's always recommended to specify the function/property visibility.
  2. Yes it is. The version without visibility modifiers has been around until PHP4. With PHP5 visibility modifiers have been introduced. Due to backwards compatibility for legacy code the version without visibility modifiers is still accepted and treated as if there was a public visibility modifier.
  3. PHP4 didn't know anything about visibility, so you cannot define private or protected members with this visibility-modifier-less syntax.
share|improve this answer
add comment

PHP was born as a (lazy, duck typed) scripting language, and people are still using it this way. Most PHP programmers don't have an idea of what OOP is, I know this problem very well because I started with PHP, and that did cost me a lot of work later. About 90% of PHP code you see around is messy and outdated as of object orientation, readability, encapsulation, etc... At least 50% is pure crap. :(

I can't tell you how much OOP programmers are suprised when they discover that "dependency injection" is actually considered an innovative design pattern by PHP developers, and it needs to be explained.

However, PHP4 had no scope operators such private or protected. At that time, you used to declare a method prepending one or more underscore(s) to the method name in order to indicate it is not meant to be called from external classes.

  1. Yes it is recommended
  2. Yes it is, in an OOP perspective
  3. Naming conventions that hopefully had to be understood by clients
share|improve this answer
add comment

Foremost PHP4 compatiblity and affinity.

Some developers (like me) omit the accessibility modifiers because they have little bearing on scripting languages. Real OOP languages like Python or Javascript don't have private or protected attributes, and don't need it. In PHP it's a bit different, but it doesn't make sense to always apply that syntactic sugar. I'll personally make it a point to reserve it for useful applications.

Many PHP coders are unaware of the original purpose of "encapsulation", as it doesn't apply to uncompiled code beyond logic visibility. It adds in fact a bit more fragility in PHP as it blows up errors at runtime, instead of compile-time (like in C++).
And I can't hold back on saying it again: Many coders also apply it solely as cargo cult programming idiom to simulate Java-like syntax (to make up for PHPs perceived/past lack of OOP constructs).

share|improve this answer
add comment

Your Answer


