Reputation: 87
Two simple, classes, one checks length and returns true of false. Second checks if first class returned true or false and returns message depending on that. Now, my question is, what difference does it make whether I put my variables as public, protected or private(note: I know what they are).
Why would I do it, who, if I put my variables as public can access it?
I know what encapsulation is and how it tells to encapsulate all class members, but there seems to be no point really.. If it was IRL I can understand that yeah someone might take something off of my house, so I should keep them all locked.. but in programming doesn't seem to make any sense..
http://www.codeproject.com/Questions/161855/Encapsulation-and-why-we-must-use-it This guy tells anyone can access class members, but who're these anyone? I thought PHP runs in server and access to it is restricted by default. Nobody beside you can access/edit the file..
class Length_class{
public $bus;
public $returned;
function __construct($bus){
$this->bus = $bus;
if($this->bus < 10){
$this->returned = false;
}else{
$this->returned = true;
}
}
}
class Length_class_extended extends Length_class{
function display_length(){
if($this->returned){
return "Length is $this->bus";
}else{
return "Length is $this->bus";
}
}
}
$Length_class_extended = new Length_class_extended(10);
echo $Length_class_extended->display_length();
Upvotes: 2
Views: 62
Reputation: 841
Maybe it doesn't make sense in this little piece of code. But an actual application is way bigger than that with much more classes (and people) working on it.
The security that encapsulation provides is not about someone hacking your code. It is about someone that you know that is going to use your code and you want to say what he may or may not access.
You use encapsulation to specify what your class is and what it should do. It helps you with defining the responsibilities of your classes. And helps other developers to be aware of it without the need of knowing how you implemented that.
Well-encapsulated code provides painless (or at least a lesser pain) class modifications.
Upvotes: 2
Reputation: 8137
The primary reason for using access modifiers is when you provide the class for someone else to use. With encapsulation the other programmer can use your class within his code without ever needing to know what goes on inside your class.
He will have a list of methods to use and will only interact with the class through this interface. This forces him to use the method 'correctly' in a way that won't break either his program or the functioning of your class.
It may not seem relevant to you if you are the only person using the code and it is a relatively straightforward project, and it is perfectly possible to write functioning code with every member public - it is however good practice and worth doing anyway. It forces you to write in a 'good' style with real encapsulation and may save you a headache later on by stopping you from misusing your own class if you want to reuse the code in a wide variety of situations.
Upvotes: 1