[TYPO3-50-general] Helping Functions
Xavier Perseguers
typo3 at perseguers.ch
Mon Feb 2 18:12:56 CET 2009
Hi Nino,
> So, no matter if you restrict the access but provide a public access or
> declare it public from the start. It's the same.
>
> No excuses here.
> Maybe you change the getter method in near future but that's NOT an
> excuse for providing it right NOW.
> Code like this is, beside the validation of essential OOP principles,
> clear YAGNI. And leads to orphaned methods later when you decide to
> provide a "real" and good interface.
> So first provide that clean interface to a class with some meaningful
> behaviours and you won't have to write those getters/setters anymore.
I may write my class
class person {
public $email;
}
and then later on change to virtual properties as you said to validate
the email :
class person {
private $_email;
// some magic stuff for virtual properties
public setEmail($email) {
if (preg_match('/blabla/', $email)) $this->_email = $email;
}
}
and my code would still work:
before:
$foo->email = 'somebody at domain.tld';
echo $foo->email;
after:
$foo->email = 'somebody at domain.tld';
echo $foo->email;
thanks to the virtual properties that now use the setter I defined (and
the getter if I managed to write a special getEmail() method)
Now if FLOW3 does not provide virtual properties, you seem to say that
your coding style is to first write
class person {
public $email;
}
and associated code
$foo->email = 'somebody at domain.tld';
echo $foo->email;
and later on when you write
class person {
private $_email;
public setEmail($email) {
if (preg_match('/blabla/', $email)) $this->_email = $email;
}
public getEmail() {
return $this->_email;
}
}
you will refactor your code and change this to
$foo->setEmail('somebody at domain.tld');
echo $foo->getEmail();
???
Personally, when I know that a bit later on I will validate email, I
would write from the beginning the getter and setter with no validation
code and latter on simply add the validation without having to refactor
anything.
I really enjoy refactoring but not for such stuff. Interfaces are there
in order to let you change the code without having to refactor anything.
My example is just a step before writing an interface but anyway, I
think it does not make sense doing refactoring for the love of refactoring.
--
Xavier Perseguers
http://xavier.perseguers.ch/en/tutorials/typo3.html
More information about the TYPO3-project-5_0-general
mailing list