Access Modifiers


Access Modifiers improve the object model by controlling how an object is used or reused.

Access Modifier
Modifier Description
private In Private access modifier,properties and methods can be accessed only within the class.
protected This modifier check external access to a property or method, but give permits access internally and to parent and child classes.
public                  In this modifier the property or method can be accessed by any part of a script both inside and outside the class definition. All methods are regarded as public unless preceded by a different modifier.
Example :
<?php
class Modifier
{
	public  $name  = "techknow Heights";
	private $name1 = "Website";
	function display()
	{
		echo $this->name."</br>";
	}
	
} 
class Test extends Modifier
{
	function display()
	{
	  echo $this->name."</br>";
	  echo $this->name1;//private variable
	}
}
$obj=new Modifier();
$obj->display();
$obj1=new Test();
$obj1->display();
?>
Output :
techknow Heights 
techknow Heights 
Notice: Undefined property: Test::$name1