News & UpdatesProgrammingWeb programmingPartnersStore
Book Cover
Buy Now
Projects
Links

PHP Tutorial – 12 – Access Levels

The access level of a class member can be defined by prefixing the declaration with either public, protected, or private. Public is the default access level for all class members.

class MyClass
{
  public    $myPublic;    // unrestricted access
  protected $myProtected; // defining or derived class
  private   $myPrivate;   // defining class only
}

Private access

All members regardless of their visibility are available in the class where they are defined. This is the only place where the private member can be accessed.

class MyClass
{
  function test()
  {
    $myPublic    = 0; // allowed
    $myPrivate   = 0; // allowed
    $myProtected = 0; // allowed
  }  
}

Protected access

A protected member can be accessed from inside of a derived class as well as from within the defining class.

class MyChild extends MyClass
{
  function test()
  {
    $myPublic    = 0; // allowed
    $myProtected = 0; // allowed
    $myPrivate   = 0; // inaccessible	
  }  
}

Public access

Public members have unrestricted access, which means that they can be reached even from outside of the class from instances of the class.

$m = new MyClass();
$m->myPublic    = 0; // allowed	
$m->myProtected = 0; // inaccessible	
$m->myPrivate   = 0; // inaccessible

If you like this tutorial please +1 it:


4 Responses

Dotun
June 14th, 2011 at 15:09  

@admin… Hope the DVD, would be more comprehensive than the text version.. Keep the flag flying…

Great Job….

August 14th, 2010 at 14:27  

That is in the works. Hopefully, there will be DVD versions of the tutorials available before the end of 2011. Cheers.

aggi
August 4th, 2010 at 13:23  

where can i get dvd of alll these tutorial ?.

aggi
August 4th, 2010 at 13:23  

good tutorial

Leave a Reply