Visibility keywords
When working with
classes, we can even restrict access to its
properties and
methods using the
visibility keywords for greater control:
public
This is the default for all class members in PHP.
A public property or method can be accessed anywhere, from within the class and outside.
private
A private property or method is accessible only from within the class that defines it.
Even child or inherited classes cannot access private properties or methods.
protected
A protected property or method can only be accessed from within the class itself or in child or inherited classes i.e. classes that extends that class.
These
Visibility keywords are also called
Access Modifiers.
$this
is a pseudo-variable that is available when a method is called from within an object context.
is a reference to the calling object; usually the object to which the method belongs, but possibly another object, if the method is called statically from the context of a secondary object.
As of PHP 7.0.0 calling a non-static method statically from an incompatible context results in $this being undefined inside the method.
Calling a non-static method statically from an incompatible context has been deprecated as of PHP 5.6.0.
As of PHP 7.0.0 calling a non-static method statically has been generally deprecated (even if called from a compatible context).
Before PHP 5.6.0 such calls already triggered a strict notice.
EXERCISE
<?php
class Dwarfs {
/* Properties */
public $name;
public $behavior;
private $friend;
protected $age;
/* Methods */
public function set_name($name)
{
$this->name = $name;
}
public function set_behavior($behavior)
{
$this->behavior = $behavior;
}
private function set_friend($friend)
{
$this->friend = $friend;
}
protected function set_age($age)
{
$this->age = $age;
}
public function get_name()
{
return $this->name;
}
public function get_behavior()
{
return $this->behavior;
}
private function get_friend()
{
return $this->friend;
}
protected function get_age()
{
return $this->age;
}
}
$nm = 'Happy';
$bh = 'This dwarf is always cheerful and happy about everything.';
$fr = 'Snow White friend';
$ag = '1937 - Year of the first performance of "Snow White and the Seven Dwarfs"';
$dwarf1 = new Dwarfs();
$dwarf1->set_name($nm);
$dwarf2 = new Dwarfs();
$dwarf2->set_behavior($bh);
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if executed - will display ERROR
This always happens when visibility is
designated as private or protected
in both Properties and Methods
$dwarf3 = new Dwarfs();
$dwarf3->set_friend($fr);
$dwarf4 = new Dwarfs();
$dwarf4->set_age($ag);
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
echo $dwarf1->get_name();
echo '<br><br>';
echo $dwarf2->get_behavior();
echo '<br><br>';
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if executed - will display ERROR
This always happens when visibility is
designated as private or protected
in both Properties and Methods
echo $dwarf3->get_friend();
echo '<br><br>';
echo $dwarf4->get_age();
echo '<br><br>';
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
?>
RESULT
Happy
This dwarf is always cheerful and happy about everything.
EXERCISE
<?php
class nDwarfs {
/* Properties */
public $name;
public $behavior;
private $friend;
protected $age;
/* Methods */
function set_name($name)
{
$this->name = $name;
}
function set_behavior($behavior)
{
$this->behavior = $behavior;
}
function set_friend($friend)
{
$this->friend = $friend;
}
function get_name()
{
return $this->name;
}
function get_behavior()
{
return $this->behavior;
}
function get_friend()
{
return $this->friend;
}
}
$nm = 'Happy';
$bh = 'This dwarf is always cheerful and happy about everything.';
$fr = 'Snow White friend';
$ndwarf1 = new nDwarfs();
$ndwarf1->set_name($nm);
$ndwarf2 = new nDwarfs();
$ndwarf2->set_behavior($bh);
$ndwarf3 = new nDwarfs();
$ndwarf3->set_friend($fr);
echo $ndwarf1->get_name();
echo '<br><br>';
echo $ndwarf2->get_behavior();
echo '<br><br>';
echo $ndwarf3->get_friend();
echo '<br><br>';
?>
RESULT
Happy
This dwarf is always cheerful and happy about everything.
Snow White friend
EXERCISE
<?php
class nDwarfs {
/* Properties */
public $name;
public $behavior;
public $friend;
public $age;
/* Methods */
function set_name($name)
{
$this->name = $name;
}
function set_behavior($behavior)
{
$this->behavior = $behavior;
}
function set_friend($friend)
{
$this->friend = $friend;
}
function set_age($age)
{
$this->age = $age;
}
function get_name()
{
return $this->name;
}
function get_behavior()
{
return $this->behavior;
}
function get_friend()
{
return $this->friend;
}
function get_age()
{
return $this->age;
}
}
$nm = 'Happy';
$bh = 'This dwarf is always cheerful and happy about everything.';
$fr = 'Snow White friend';
$ag = '1937 - Year of the first performance of "Snow White and the Seven Dwarfs"';
$ndwarf1 = new nDwarfs();
$ndwarf1->set_name($nm);
$ndwarf2 = new nDwarfs();
$ndwarf2->set_behavior($bh);
$ndwarf3 = new nDwarfs();
$ndwarf3->set_friend($fr);
$ndwarf4 = new nDwarfs();
$ndwarf4->set_age($ag);
echo $ndwarf1->get_name();
echo '<br><br>';
echo $ndwarf2->get_behavior();
echo '<br><br>';
echo $ndwarf3->get_friend();
echo '<br><br>';
echo $ndwarf4->get_age();
echo '<br><br>';
?>
RESULT
If you have to concurrently execute another code with the same Class name, you will surely get an ERROR similar to the one seen below in response:
Fatal error: Cannot declare class nDwarfs, because the name is already in use in D:\WEB\public_html\code\class_def03.php on line 3
EXERCISE
<?php
class nDwarfs {
/* Properties */
public $name;
public $behavior;
public $friend;
public $age;
/* Methods */
function set_name($name)
{
$this->name = $name;
}
function set_behavior($behavior)
{
$this->behavior = $behavior;
}
function set_friend($friend)
{
$this->friend = $friend;
}
function set_age($age)
{
$this->age = $age;
}
function get_name()
{
return $this->name;
}
function get_behavior()
{
return $this->behavior;
}
function get_friend()
{
return $this->friend;
}
function get_age()
{
return $this->age;
}
}
$nm = 'Happy';
$bh = 'This dwarf is always cheerful and happy about everything.';
$fr = 'Snow White friend';
$ag = '1937 - Year of the first performance of "Snow White and the Seven Dwarfs"';
$ndwarf1 = new nDwarfs();
$ndwarf1->set_name($nm);
$ndwarf2 = new nDwarfs();
$ndwarf2->set_behavior($bh);
$ndwarf3 = new nDwarfs();
$ndwarf3->set_friend($fr);
$ndwarf4 = new nDwarfs();
$ndwarf4->set_age($ag);
echo $ndwarf1->get_name();
echo '<br><br>';
echo $ndwarf2->get_behavior();
echo '<br><br>';
echo $ndwarf3->get_friend();
echo '<br><br>';
echo $ndwarf4->get_age();
echo '<br><br>';
?>
RESULT
Happy
This dwarf is always cheerful and happy about everything.
Snow White friend
1937 - Year of the first performance of "Snow White and the Seven Dwarfs"