There are three properties for functions and class variables in the PHP class: Public protected private, when to use what attributes are good entanglements, deliberately found an instance, so that looks clearer.
Public represents the global, and the inner and outer subclasses of the class can be accessed;
Private means that only this class can be used internally;
Protected is protected and is accessible only in this class or subclass or in the parent class;
<?php
Parent class
Class father{
Public function A () {
echo "function a";
}
Private Function B () {
echo "function B";
}
protected function C () {
echo "Function C";
}
}
Sub-class
Class Child extends father{
Function d () {
Parent::a ();//Call the parent Class A method
}
function e () {
Parent::c (); Call the C method of the parent class
}
function f () {
Parent::b (); Call the B method of the parent class
}
}
$father =new father ();
$father->a ();
$father->b (); Show error external cannot call private method called to protected method Father::b ()
$father->c (); Display error external cannot invoke protected method call to Private method Father::c ()
$chlid =new Child ();
$chlid->d ();
$chlid->e ();
$chlid->f ();//Display error cannot invoke parent class private method call to Private method Father::b ()
?>
done!
PHP Public Protected Private attribute instance detailed