I was just reading a new article in Developers Shed pn PHP 5 destructors and was struck by the technique used for coding the examples. I'm thinking primarily of the explicit use of private and public terms for declaring variables and functions. Is there any real benefit from this? Is this something exclusive to PHP 5? I am currently producing code that runs in both PHP 4 and 5 environments.
Although I still don't see the benefit I have found an explanation of public, protected and private visibility properties. I have only recently started developing in PHP5 so I really haven't paid much attention to it. PHP5 does not accept the var variable definition but does treat those so defined as public and throws an E_STRICT warning. Variables defined as public are accessible everywhere; protected is available in inherited and parent classes and in the class where defined; and, private only within the class wherein it is defined.
Variable and functions not explicitly defined with one of these attributes are treated as public.
Just thought I'd mention this.
Although I still don't see the benefit I have found an explanation of public, protected and private visibility properties.
Properly using private and public properties helps you make better, abstracted OOP code. In simple projects that don't have a heavy dose of OOP, this isn't going to make much difference.
However, if you're coming from another OOP environment (like C++), or you're developing a large project that makes use of classes, then you'll appreciate the abstraction and integration you get when you make as many variables as possible private.
The general idea is that most of a class's essential properties should be private. Instead of accessing the properties directly, you should go through methods that either fetch or modify that property for you.
Then, if you integrate that class into a project and later change the structure of the class, you don't have to alter the whole project. You can simply change the way the methods work with the class or keep older methods around for backwards compatibility.
It's similar to the way that it's generally better to abstract a large script into individual functions - passing variables and values back and forth - rather than using a whole bunch of global variables.
To some extent it's just a matter of form and "proper" coding, but it does have advantages in certain projects.