note 49413 added to language.oop5.magic

From: Date: Thu, 27 Jan 2005 07:09:17 +0000
Subject: note 49413 added to language.oop5.magic
Groups: php.notes 
Request: Send a blank email to php-notes+get-84055@lists.php.net to get a copy of this message
One of the principles of OOP is encapsulation--the idea that an object should handle its own data and no others'. Asking base classes to take care of subclasses' data, esp considering that a class can't possibly know how many dozens of ways it will be extended, is irresponsible and dangerous. Consider the following... <?php class SomeStupidStorageClass { public function getContents($pos, $len) { ...stuff... } } class CryptedStorageClass extends SomeStupidStorageClass { private $decrypted_block; public function getContents($pos, $len) { ...decrypt... } } ?> If SomeStupidStorageClass decided to serialize its subclasses' data as well as its own, a portion of what was once an encrypted thingie could be stored, in the clear, wherever the thingie was stored. Obviously, CryptedStorageClass would never have chosen this...but it had to either know how to serialize its parent class's data without calling parent::_sleep(), or let the base class do what it wanted to. Considering encapsulation again, no class should have to know how the parent handles its own private data. And it certainly shouldn't have to worry that users will find a way to break access controls in the name of convenience. If a class wants both to have private/protected data and to survive serialization, it should have its own __sleep() method which asks the parent to report its own fields and then adds to the list if applicable. Like so.... <?php class BetterClass { private $content; public function __sleep() { return array('basedata1', 'basedata2'); } public function getContents() { ...stuff... } } class BetterDerivedClass extends BetterClass { private $decrypted_block; public function __sleep() { return parent::__sleep(); } public function getContents() { ...decrypt... } } ?> The derived class has better control over its data, and we don't have to worry about something being stored that shouldn't be. ---- Manual Page -- http://www.php.net/manual/en/language.oop5.magic.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+49413 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+49413&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+49413&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+49413&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+49413&report=yes&reason=useless Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+49413&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+49413&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#84055) next »