James
James

Reputation: 291

Difference between functions and public functions in classes

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?

Upvotes: 7

Views: 20302

Answers (6)

Mubo
Mubo

Reputation: 1070

In OOP PHP function are called Methods.

  class MyClass {

    public function aMethod() {
    // (do stuff here)
    }
     function myMethod(){} //here public property is assumed

    } 

When you optionally leave out the public, private, or protected keyword in a class. If you do this, publicis assumed.

  • public -> Available in the whole scope, meaning it can be accessed everywhere
  • protected ->Available within the class or its subclasses.
  • private ->Available only within the class that defines the function

    These feature is available since php 5.X and they give you the possibility to control the visibility of your variable or function.

if you see a function with identifiers, like public , protected and private these are in class.

public function functionName(){} 

and 
function functionName(){}

are basically the same except that one has visibility level of being public and is object oriented way and the other php normal function with no visibility level.

Upvotes: 0

kpmDev
kpmDev

Reputation: 1370

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.

Upvotes: 3

TimWolla
TimWolla

Reputation: 32681

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.

Upvotes: 14

Guilherme Vaz
Guilherme Vaz

Reputation: 390

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();
    }
}

Upvotes: 4

George Cummins
George Cummins

Reputation: 28906

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() {};

Upvotes: 2

ziollek
ziollek

Reputation: 1993

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.

Upvotes: 4

Related Questions