finchamgroves wrote:3. So - if I call a variable or a function from within this new instance I expect this to be of the class Customer and therefore to be available even if protected or private.
private and protected methods and attributes/properties are only available from within the class: public methods and attributes/properties are available from within the class, and to external code.
finchamgroves wrote:4. From the outputs the variables (properties) are available in $c whether public, private or protected - at least when accessed by methods within the class.
Correct, they're available from within the class instance itself using $this, but their direct availability to external access is determined by their "visibility"
Accessing attributes from within the class
Code: Select all
public function displayAll() {
echo $this->name; // echoes the value of the name attribute
echo $this->phone; // echoes the value of the phone attribute
echo $this->salary; // echoes the value of the salary attribute
}
Accessing attributes from outside the class
Code: Select all
$c = new Customer();
echo $c->name; // echoes the value of the name attribute because it's public
echo $c->phone; // generates an error because it's protected
echo $c->salary; // generates an error because it's private
finchamgroves wrote:5. From the output the functions (methods) only work in $c if they are specified as public. i.e. If I change the get_Salary function to protected it no longer works even in $c which is an instance of the same class - so the assumptions in paragraph (3) are obviously up the spout - but why?
Not correct. The same rules of "visibility" that apply to attributes/properties also apply to methods. If a method is defined as public, it can be called from outside the class. If it's private or protected, then it can only be called from another method within the same class instance as jmaker'sexample demonstrates.
finchamgroves wrote:6. Put another way - If I can't access a protected function from within an instance of the same class - then how can I access it? OK I can create another public function within the class (as per the first reply)to access the protected function but what's the point of making the first function protected and then adding a public function from which you can access that? In short - what's the point of a protected function if you have to access it via a public function?
The value of private or protected functions isn't in simple getters or setters, it's in protecting the internals of the class from external manipulation.
Code: Select all
class Customer
{
// declare variables
public $name;
private $salary;
//Setters and getters
//name
private function checkPrivilege() {
if ($_SESSION['HR_USER']) {
return true;
}
return false;
}
public function setName($name) {
$this->name = $name;
}
public function getName() {
return $this->name;
}
//salary
public function setSalary($salary) {
if ($this->checkPrivilege()) {
$this->salary = $salary;
} else {
throw new Exception('Security Violation.');
}
}
public function getSalary() {
if ($this->checkPrivilege()) {
return $this->salary;
} else {
throw new Exception('Security Violation.');
}
}
}
The checkPrivilege() method is private, so $c->checkPrivilege() is not accessible because the calling application should not need to access it. It's only relevant when testing whether the user has privilege when they try to access the public methods that call it