tags:

views:

268

answers:

2

I have a problem while setting a private attribute on a PHP class, my __set() method is being called however when I perform this on an attribute which is an array it performs first my __get() method which renders the ser unusable :/

$this->person['name'] = 'perro';

simply, doesn't work,any idea on this subject ?

A: 

Solve it, the problem was I was trying to set a private variable within my class and I don't understand why if Im exteing PHP doesn't let me touch this attributes

to correct this problem I just changed my attributes from private to protected

PERR0_HUNTER
That's the definition of a private property. Only the class defining the private attribute may alter/access its contents. A protected attribute allows child classes to alter/access it. Just saw that Pascal MARTIN explained this difference nicely.
Mike B
+3  A: 

If your attribute is private, not being able to see it (either for reading nor writing) from a sub-class if perfectly normal : private means you attribute is private to the class it is defined in.

That's the difference between private and protected :

  • private = accessible only for the one class it's declared in
  • protected = accessible from any class that "is a" class of the type you'r declaring (super or sub-class)
  • public = accessible from anyone

For more informations about this in PHP, see Visibilty in the manual.

You will probably find more information on the net about that, if necessary : it's one of the basics of Object-Oriented programming, and is true in other languages (like C++, for instance)
Search for keywords like "Visibility", "Encapsulation", or "Information Hiding", for instance -- in relation with OOP / Object-Oriented Programmation

Pascal MARTIN