Default visibility of class methods in PHP

35,109

Solution 1

Default is public.

Class methods may be defined as public, private, or protected. Methods declared without any explicit visibility keyword are defined as public.

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

Solution 2

Default is public. It's a good practice to always include it, however PHP4 supported classes without access modifiers, so it's common to see no usage of them in legacy code.

And no, PHP has no package visibility, mainly because until recently PHP had no packages.

Solution 3

The default is public. The reason probably is backwards compatibility as old code expects it to be public (it would stop working if it weren't public).

Solution 4

Default visibility is PUBLIC

Source

Solution 5

When no visibility keyword (public,private or protected) used, methods will be public. But, you cannot define properties in this way. For properties, you will need to append a visibility keyword on declaration.

For properties which is not declared in the class and you assign a value to it inside a method will have a public visibility.

<?php
class Example {
    public $name; 
    public function __construct() {
        $this -> age = 9; // age is now public
        $this -> privateFunction();
    }
    private function privateFunction() {
        $this -> country = "USA"; // this is also public
    }
}
Share:
35,109
Yada
Author by

Yada

Software Developer living and working in Toronto, Canada. https://fantasysupercontest.com

Updated on February 28, 2020

Comments

  • Yada
    Yada about 4 years

    I looked at the manual, but I can't seem to find the answer.

    What is the default visibility in PHP for methods without a visibility declaration? Does PHP have a package visibility like in Java?

    For example, in the following code, is go() public or private?

    class test {
      function go() {
      }
    }
    

    The reason I asked is that I've seen many constructors code written as function __construct() and some as public function __construct(). Are they equivalent?

  • User
    User over 11 years
    @Ian: I would say because "explicit is better than implicit" (as the Zen of python says). It causes other programmers to waste brain cycles wondering if the constructor is private or public or what. If people always used access modifiers the original poster might not even have asked this question.
  • Marc.2377
    Marc.2377 over 7 years
    Same goes for properties
  • Cristopher Cuevas Moreno
    Cristopher Cuevas Moreno over 7 years
    Also of note, on languages such as Java, were the default is package-private, one always wonders if it's package-private by design, or the developer just forgot to specify it (specially when dealing with not-so-senior developers). That's why PMD includes rules such as this: pmd.github.io/pmd-5.5.2/pmd-java/rules/java/…
  • Kolyunya
    Kolyunya over 6 years
    Same goes for constants.
  • Guney Ozsan
    Guney Ozsan over 5 years
    Kudos for the reasoning.
  • jave.web
    jave.web almost 3 years
    This was changed over time, it's now "must" not a "may". Also usually you don't need public properties and lot of times it's a code smell.
  • jave.web
    jave.web almost 3 years
    @User therefore Python made everything just public and do the whatever hack you want with it x)