Re: php 5 classes: public, protected and private



jopperdepopper wrote:
You should read "can be accessed everywhere".

Private members can be accessed by members of the class only.
Protected members can be accessed by members of the class or a derived
class.
Public members can be accessed by anyone, including other classes,
functions and any other code.



Thanks Jerry. I'm trying to make a bit of sense of the php 5 approach
to classes, and so far having a hard time. I fail to see the 'why'
behind the 'public, protected and private' and stuff like abstraction,
interfaces and whatnot. Feels like things are being over-complicated
somehow... or it's just my being inexperienced on this...

Any other reading material on this suggested, someone?


Look for some good books on OO theory and design.

Two of the concepts in OO are 'encapsulation' and 'methods'.

Encapsulation means the internals of an object are managed only by that object and are not available to anyone else. In PHP these are private members.

Methods are implemented as functions in PHP. They operate on the object.

The idea is to completely separate the implementation of the class from the use of it. It means you can change the class any way you want. As long as the public interface remains the same, it makes no difference.

A good example of this is floating point numbers. They are stored as a

x * 2^y

X is the mantissa, y is the base 2 exponent. For instance, 8 would be stored as x=1 and y=3, because 8 is 1 * 2^3. But all of this is hidden behind the scenes; you can do operations on a floating point number such as add, subtract, print, etc.

And if at some other time PHP changed the implementation to something else, none of your code would change.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@xxxxxxxxxxxxx
==================
.



Relevant Pages

  • Re: Accessing private member via subclass
    ... "Members are either declared in the type, ... A class inherits the members of its direct base class. ... all those private members are still present ...
    (comp.lang.java.programmer)
  • Re: Accessing private member via subclass
    ... "Members are either declared in the type, ... A class inherits the members of its direct base ... and static constructors of the base class. ... all those private members are still present ...
    (comp.lang.java.programmer)
  • Re: Class member addressing
    ... > with the prefix of an underbar in private members because a program ... Plus it eliminates conflicts between variable ... used to have the underscore and at least some of the private variables in ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: How to access private members of a base class
    ... private members, and so on, and so on... ... > I already use reflection, ... But I CANNOT obtain the PRIVATE members of the base ... >>> members of its base classes.(I already have the ReflectionPermission) ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: php 5 classes: public, protected and private
    ... Private members can be accessed by members of the class only. ... Protected members can be accessed by members of the class or a derived ... Public members can be accessed by anyone, including other classes, ...
    (comp.lang.php)