Difference between functions and public functions in classes

15,743

Solution 1

Omitting the visibility is legacy code. PHP 4 did not support public, protected and private, all methods were public.

Short: "public function" == "function" // true

See also the PHP manual:

// This is public
function Foo()
{
    $this->MyPublic();
    $this->MyProtected();
    $this->MyPrivate();
}

Similarly var $attribute; is equivalent to public $attribute. The var version also is PHP 4 legacy code.

Solution 2

There's no difference in PHP >=5. Class methods may be defined as public, private, or protected. Methods declared without any explicit visibility keyword are defined as public.

Solution 3

When you don't set the visibility of a method in php, it's the same as setting it as public.

From PHP Manual:

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

<?php
/**
 * Define MyClass
 */
class MyClass
{
    // Declare a public constructor
    public function __construct() { }

    // Declare a public method
    public function MyPublic() { }

    // Declare a protected method
    protected function MyProtected() { }

    // Declare a private method
    private function MyPrivate() { }

    // This is public
    function Foo()
    {
        $this->MyPublic();
        $this->MyProtected();
        $this->MyPrivate();
    }
}

Solution 4

If you define with simply function means, default it takes public scope (default) from PHP 5.

function sample { }

and

public function sample { }

are no difference between them.

private => can access the property with in the class

protected => can access the property own class and sub classes

public => can access anywhere in application.

Solution 5

The default visibility is public. If a method is declared without an explicit visibility prefix, it will be public.

The following declarations are equivalent:

function name() {};

public function name() {};
Share:
15,743
James
Author by

James

Updated on June 09, 2022

Comments

  • James
    James almost 2 years

    In classes, most people use public function name() { } to define methods. However, I have seen several examples of them being defined without the public keyword, like function name() { }. I was confused by this because I thought you had to use public/private/protected when inside a class.

    I made the same sort of thing and function was doing the exact same job as public function.

    So my question is, what is the difference between using function and public function when inside a class?

  • TimWolla
    TimWolla about 10 years
    @Will I said omitting it is legacy code, therefore you should always explicitly write public for clarity and full future compatibility.
  • George Cummins
    George Cummins about 10 years
    I'm curious to know why you label this as "legacy" code. The documentation is specific: the visibility of a method may be define but it is not required, and a suitable default is provided. According to my reading, non-explicit declarations are still valid in new code.
  • TimWolla
    TimWolla about 10 years
    @GeorgeCummins It has it origins in PHP 4 and exists for backwards compatibility. Other OOP compability functionality already has been axed (namely Constructors named like the class for namespaced classes) and explicitly defining the visibility is the only safe way.