Reply to Re: [PHP] Best practices for set/get methods

Your name:

Reply:


Posted by Rasmus Lerdorf on 10/10/41 11:13

Tim Boring wrote:
> Does anyone have suggestions/ideas about best practices for writing
> set/get methods in PHP5? There are two basic ways I've seen this done,
> which I've provided examples of below. Method #2 is obviously the easier
> way, but that doesn't mean it may be the best way.
>
> I'm curious to read people's responses.
>
> Thanks,
> Tim
>
> #1: set/get method for each member attribute
> example:
> private $foo;
> private $bar;
>
> public getFoo()
> {
> return $this->foo;
> }
> public setFoo($val)
> {
> $this->foo = $val;
> }
> public getBar()
> {
> return $this->bar;
> }
> public setBar($val)
> {
> $this->bar = $val;
> }
>
> #2: generalized set/get methods
> example:
> private $foo;
> private $bar;
>
> public getVar($var)
> {
> return $this->$var;
> }
> public setVar($var, $val)
> {
> set $this->$var = $val;
> }

Why not use __set()?

http://www.php.net/manual/en/language.oop5.overloading.php

-Rasmus

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация