note 85995 added to language.operators.bitwise

From: Date: Sat, 27 Sep 2008 20:32:21 +0000
Subject: note 85995 added to language.operators.bitwise
Groups: php.notes 
Request: Send a blank email to php-notes+get-144842@lists.php.net to get a copy of this message
BITWISE OPERATORS and PHP CLASSES As a webmaster myself, I've found that breaking down large apps into CLASSES make tough jobs even easier. For example, on one of the websites I maintain, I have a large group of Artists who wish to sell their artwork on the internet. They wanted the website to act like a real Gallery where artwork can be displayed, but not for sale, etc., etc. My severly truncated Artwork class below shows how I use Bitwise Operators to control a single integer called flags. <?php class Artwork { protected static $HIDDEN = 1; // bitmask for 1st bit (0x1) - do not show this item protected static $NFS = 2; // bitmask for 2nd bit (0x2) - not for sale protected static $SOLD = 4; // bitmask for 3rd bit (0x4) - item is already sold protected $flags = 0; // the property we're interested in... public function isHidden(){ return ($this->flags & self::$HIDDEN > 0); } public function isNotForSale(){ return ($this->flags & self::$NFS > 0); } public function isSold(){ return ($this->flags & self::$SOLD > 0); } public function setHidden($hide){ $this->flags = ($hide ? ($this->flags | self::$HIDDEN) : ($this->flags & ~self::$HIDDEN)); } public function setNotForSale($doNotSell){ $this->flags = ($hide ? ($this->flags | self::$NFS) : ($this->flags & ~self::$NFS)); } public function setSold($sold){ $this->flags = ($sold ? ($this->flags | self::$SOLD) : ($this->flags & ~self::$SOLD)); } public function __toString(){ return "Artwork [flags=$this->flags]"; } } $art = new Artwork(); $art->setSold(true); echo $art; // outputs: Artwork [flags=4] ?> With the code example above, $art->flags = 4, indicating that this artwork is still showing on the website, but visitors cannot purchase it because it has been sold already (and the Add to Cart link dissappears). If the artwork is hidden, then no one would be able to see it at all, and if it is marked Not For Sale, then it cannot be purchased and an icon will be shown to indicate that. You can see that a simple flags property can pull more than its fair share of duties. ---- Server IP: 69.147.83.197 Probable Submitter: 67.150.121.145 ---- Manual Page -- http://www.php.net/manual/en/language.operators.bitwise.php Edit -- https://master.php.net/note/edit/85995 Del: integrated -- https://master.php.net/note/delete/85995/integrated Del: useless -- https://master.php.net/note/delete/85995/useless Del: bad code -- https://master.php.net/note/delete/85995/bad+code Del: spam -- https://master.php.net/note/delete/85995/spam Del: non-english -- https://master.php.net/note/delete/85995/non-english Del: in docs -- https://master.php.net/note/delete/85995/in+docs Del: other reasons-- https://master.php.net/note/delete/85995 Reject -- https://master.php.net/note/reject/85995 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#144842) next »