Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • PHP Access Specifiers (Public, Private, Protected)

    • 0
    • 1
    • 1
    • 1
    • 0
    • 0
    • 0
    • 0
    • 314
    Comment on it

    Hello Readers,

    The below are php accesss specifiers which are commonly used in php:

    Public:

    When you declare Public Method or function in PHP. It can be used as Publicly into the Class (so as to be seen by other people).

    Public scope also used to other classes and instances of the object.

    Public Method can be accessed by:

    1> The same class that declared it.
    2> The classes that inherit the above declared class.
    3> Any foreign elements outside this class can also access those things.

    For Example:

    // Public

    public $variable;
    public function doSomething(){
        ...code...
    }
    

    Private:

    When you declare Private Method or Function in PHP. It can be used as a privately.

    Private scope visible only in its own class.

    Those methods and properties can be accessed by:

    1> The same class that declared it.
    2> Outsider members cannot access those variables.

    For Example:

    // Private

    private $variable;
    private function doSomething(){
        ...code...
    }
    

    Protected:

    When you declare Protected Method or Function in PHP. It can be visible in all classes that extend the current class including the parent class like Inheritance (Means parent,child relationship).

    It can be accessed by:

    1>The same class that declared it.
    2> The classes that inherit the above declared class.
    3> Outsider members cannot access those variables.

    For Example:

    // Protected

    protected $variable;
    protected function doSomething(){
        ...code...
    }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: