Re: Fundamental question about ADTs
| From: | Jochem Maas | Date: | Fri, 29 Jul 2005 11:54:06 +0000 |
| Subject: | Re: Fundamental question about ADTs | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-219591@lists.php.net to get a copy of this message | ||
Jasper Bryant-Greene wrote:
Hi all Recently I've been reading /Code Complete/ by Steve McConnell, and in it he recommends the use of ADTs, or Abstract Data Types, which I have beenbig words :-) guess it means he can charge more per hour ;-)
thinking about implementing in a major project I am currently embarking on. Basically the question that I have for the list is this: If I have a relatively simple class, for example Photo, then why does McConnell recommend that I do this:<sidenote> doesn't McConnell give a reason? (maybe he is a scientologist and just demands blind faith) </sidenote>
<?php $caption = $instanceOfPhotoClass->getCaption(); //do something with $caption $instanceOfPhotoClass->setCaption($caption); ?> Rather than this: <?php $caption = $instanceOfPhotoClass->caption; // do something with $caption $instanceOfPhotoClass->caption = $caption; ?> I'm just looking for some alternative explanations to what McConnell offers in the book. I can understand that the first example allows me to "know" when an attribute has been modified... but it annoys me that I have code like this: class Photo {the point is encapsulation. basically you want to do something (set the caption) how that is implemented, should, according to OO, theory be hidden from the user of the object. it also means that if you want to log something or do any kind of action as a reaction to the caption changing you can implement it (at any stage in the development) in one place and all instances of code where the caption is potentially being changed will comply - whereas if you were doing $photo->caption = $caption all over the place you would be stuck... also consider this: class PhotoAdv {private $caption;[...]public function setCaption($newCaption) { $this->caption = $newCaption; }} It just seems so pointless......
private $caption;
public function setCaption($newCaption) {
if (is_string($newCaption)) {
$this->caption = $newCaption;}
}} $p = new PhotoAdv(); $p->setCaption( array("this is not very smart") ); as apposed to: class Photo {
public $caption;} $p = new Photo(); $p->caption = array("this is not very smart"); now I know which class/object I would rather be working with, for instance, when it comes to generating some SQL to update the relevant record in a DB. how about you? HTH ps - check out the __set(), __get() & __call() magic methods that are available for classes in php5. you might be able to figure out a cool way to use them in order to minimize the code you have to write (regarding repetitive setter/getter functions)
Thanks in advance for any comments. Jasper