note 85995 modified in language.operators.bitwise by danbrown

From: Date: Mon, 29 Sep 2008 19:44:12 +0000
Subject: note 85995 modified in language.operators.bitwise by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-144886@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 = ($doNotSell ? ($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. --was-- 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. http://php.net/manual/en/language.operators.bitwise.php

« previous php.notes (#144886) next »