One man’s voice Thoughts, rants and commentary from a husband, father of five and professional web geek

24Jul/084

Fun with PHP

Every now and again someone writes a line or two of code that really makes me smile. Such is the case with this outcome determinating and decision making class below. This code is being reproduced, with or without consent, from the PHP Developers Network's own scottayy.

<?php
/**
 * Coin flipper class helps determine outcome of situations in which an outcome
 * cannot be decided by sheer manpower alone.
 * 
 * @author Scott Martin <scottayy@devnetwork.net>
 * @license None, don't even try to use this or your hair will turn yellow
 */
class coin {
	/**
	 * The outcome determinators
	 * 
	 * Each invocation of this object will require a determinator upon which the
	 * object relies to build a determined outcome. These are those determinators.
	 * 
	 * @access private
	 * @var array
	 */
	private static $_sides = array('heads', 'tails');
 
	/**
	 * The determinating method
	 * 
	 * This method, when called, invokes a determination sequence and returns a
	 * determined value for use in decision making.
	 * 
	 * @access public
	 * @return string Randomly selected determinator
	 */
	public static function flip() {
		// Quick, randomize me some determinators
		shuffle(self::$_sides);
 
		// Quick, offer it back before it gets angry
		return self::$_sides[mt_rand(0, 1)];
	}
}
 
/**
 * We should always test our determinating decision establisher
 *
 * 2,4,6,8 You know you want to determinate
 */
echo coin::flip();
?>

For those that just have to have an object to instantiate (and you know who you are), there is this lightly modified version for your obsessive/compulsive selves:

<?php
/**
 * Coin flipper class helps determine outcome of situations in which an outcome
 * cannot be decided by shear manpower alone.
 * 
 * @author Scott Martin <scottayy@devnetwork.net>
 * @license None, don't even try to use this or your hair will turn yellow
 */
class coin {
	/**
	 * The outcome determinators
	 * 
	 * Each invocation of this object will require a determinator upon which the
	 * object relies to build a determined outcome. These are those determinators.
	 * 
	 * @access private
	 * @var array
	 */
	private $_sides = array('heads', 'tails');
 
	/**
	 * The determinating method
	 * 
	 * This method, when called, invokes a determination sequence and returns a
	 * determined value for use in decision making.
	 * 
	 * @access public
	 * @return string Randomly selected determinator
	 */
	public function flip() {
		// Quick, randomize me some determinators
		shuffle($this->_sides);
 
		// Quick, offer it back before it gets angry
		return $this->_sides[mt_rand(0, 1)];
	}
}
 
/**
 * We should always test our determinating decision establisher
 */
$coin = new coin;
 
/**
 * 2,4,6,8 You know you want to determinate
 */
echo $coin->flip();
?>

See, just looking at that code makes you want to smile doesn't it? Geeks are great.

Comments (4) Trackbacks (0)
  1. found your site on del.icio.us today and really liked it.. i bookmarked it and will be back to check it out some more later ..

  2. Why thank you. Thank you very much.

  3. LOL, just found this by googling me. Awesome! I forgot all about that. Love the comments.

  4. I love Google. :P

    I had forgotten about this one, too.


Leave a comment


No trackbacks yet.