Classes Visibility
Visibility
•
Properties and methods can be declared as
public accessible everywhere
private accessible only within the same class
protected accessible only within the class itself and
by inheriting and parent classes
•
For properties, a visibility
declaration is required
•
For methods, a visibility
declaration is optional
; by default, methods
are public
•
Accessing a private or
protected property /
method outside its visibility
is a fatal error
class Vis {
public $publi c = 1;
privat e $priv ate = 2;
prot ected $pro tect ed = 3;
prot ected funct ion proFc () {}
privat e funct ion priFc () {}
}
$v = new Vis ();
echo $v -> public ; # pri nts 1
echo $v -> privat e ; # Fatal Error
echo $v -> prot ecte d ; # Fatal Error
echo $v -> priFc (); # Fatal Error
echo $v -> proFc (); # Fatal Error
COMP284 Scripting Languages Lecture 8 Slide L8 – 5