Re: Fundamental question about ADTs

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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 been

big 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 {
    private $caption;

    [...]

    public function setCaption($newCaption) {
        $this->caption = $newCaption;
    }
}

It just seems so pointless......

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) {
	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


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux