Visibility
Inside a class all functions and variables have to be defined as either public, protected or private.
Public
Public means that the variable or function is visible inside and outside the class.
class PublicClass
{
//A public variable
public $name;
//A public function
public function SetName($name)
{
$this->name = $name;
}
}
Protected
Protected means that the variable or function is visible to the current class and classes that inherit this class.
class ProtectedClass
{
//A protected variable
protected $name;
//A protected function
protected function SetName($name)
{
$this->name = $name;
}
}
class SubClass : ProtectedClass
{
function __construct()
{
//I can change the value of the protected variable.
$this->name = 'New class';
}
}
Private
Private means that the variable or function is only visible within the current class.
class PrivateClass
{
//A private variable, this cannot be seen outside the class.
private $name;
//A private function
private function SetName($name)
{
$this->name = $name;
}
}
No comments have been provided.
Written by Dominic Skinner
Last Updated: 2011-10-25 16:00:38