Public variables and functions are accessible from anywhere in your script.
We can see in the below example that the public variable $num is set from user space and we call a public method that adds two the number and returns the value of $num+2. Having our properties (variables) visible or accessible from any part of our script works in our favors here, but it can also work against us. A could arise if we lost track of our values and changed the value of $num. To counter this problem we can create a method to set the value for us. Even with this in place it is still possible for somebody to simply access the $num variable. So we make the variable private. This ensures us that the property is only available within the class itself. It is private to the calling class.
PHP Code:
<?php
class mathematics
{
/*** a number ***/
public $num;
public function addTwo()
{
Return $this->num+2;
}
}
$math = new mathematics;
$math->num = 2;
echo $math->addTwo();
?>