![]() |
OOP-related keywords
Can anyone explain to me exactly how keywords like "public", "private" and so on works when I write classes?
|
They are visibilty identifiers
: Visibility is similar to variable scope, however, offers finer control. There are three types of visibility.
|
As taken from http://tips.justanotherportfolio.com/?page_id=6
Visibility Variables in classes can be set to three different types of visibility Visibility tells the program who can see what. The three types are: Public: Anyone can access it, outside, inside and child classes. Example: Any above code, anything can access it. Protected: Only functions within that class and child classes can access it example: <?As you can see, the function it extended to are able to access calc_function’s functions, but if we where to make an instance of calc_function and try to call $instance->add(100,100); you would get a fatal error. Private: Only functions within that class can access it. Example: <?As you can see here, those could be accessed by the functions within the class, if we where to create a child class to inherit it, neither of those functions could be accessed by it. The same applies to outside the class. Note: When using var to declare a variable in a class, it sets it to public, this was only continued from php4 (which didn’t have visibility settings) for code compatibility reasons. It is deprecated and probably shouldn’t be used, but it doesn’t make a difference unless you need to use private or protected. Functions left without a visibility definition are public, I generally don’t define public in my code, it doesn’t make a difference. |
Ok, thanks. I think I understand now. :)
|
Just to comment on Village Idiot's article, scope is something different (but obviously related) to class method/property visibility. Your article is talking about the latter. I only post this message in case people read it and get the wrong (well, an inaccurate) impression.
|
And there I go again with C++ terms.... I'll revise that.
|
| All times are GMT. The time now is 02:18 PM. |
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0