Using __get() to take away the necessity of PRIVATE variable

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
next08
Forum Newbie
Posts: 2
Joined: Thu Aug 31, 2006 6:31 pm

Using __get() to take away the necessity of PRIVATE variable

Post by next08 »

I just have a little question : now that PHP5 allows the new overloaded functions __get() and __set(), does it take away the meaning of PRIVATE and PUBLIC non-static variables ?

For instance :

Code: Select all

class Object
{
      public $var;

      public function __get($name)
      {
           return null;
      }

      public function __set($name, $value)
      {
      }
}

$obj = new Object;
$obj->var = 'hey';
echo $obj->var;
Without triggering fatal errors, access to $var is limited by the __get and __set functions. Can anybody tell me if I'm right ?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

They don't take away the meaning. Technically, they are error handling functions. PHP calls them when the context cannot access the property requested. This means that it could be private, could protected (and you aren't a derived class), or that the property doesn't exist.

You could have the functions throw exceptions, filter an existing internal property, or a multitude of other things. It is up to you on how you want to handle the error event.
next08
Forum Newbie
Posts: 2
Joined: Thu Aug 31, 2006 6:31 pm

Post by next08 »

I know that, but I meant it in the context of the program illustrated above. There is a role in private/public for class derivation, and that error handling is taken to a new level.

But I'm saying, in the context of this very simple program, there is no point (unless I make derived classes) in making $var private, because it is naturally unreachable from outside the class.

Right ?

thanks in advance and thanks for answering
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

That is a different question than what I read. :)

The answer, I believe, lies in what school of programming you come from. For instance in C++ the default access level of a property is private. In many circles private and public are nearly the only considered access levels of properties. Many schools of teaching in OOP teach in this manor. There are others that think public and protected should be where the sweet spot is.

PHP 5's choice was to make the default public which I suspect has more to do with legacy code from PHP 4 than in growing the language, but that's neither here nor there. This functions much more like C's structs when function tables are added.

It is my personal preference to use private more than protected and public. You are free to choose whichever preference you like. I certainly won't try to stop you.



Now, someone will likely ask why I do that. The reason I do it is partly due to how I was taught object oriented programming but also in my feeling that most classes should be considered black boxes, even from derivative code. This stems from using and building large application frameworks with hundreds and hundreds of classes which could be developed by any number of individuals over a long period of time. For me, it's a way to keep things consistent throughout the system.
Post Reply