Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Jonas Raoni Soares Silva http://jsfromhell.com

« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS 

Exception class fix for php 4 //PHP Class

   1  
   2  if(!class_exists('Exception')){
   3  	class Exception{
   4  		var $_message = '';
   5  		var $_code = 0;
   6  		var $_line = 0;
   7  		var $_file = '';
   8  		var $_trace = null;
   9  
  10  		function Exception($message = 'Unknown exception', $code = 0){
  11  			$this->_message = $message;
  12  			$this->_code = $code;
  13  			$this->_trace = debug_backtrace();
  14  			$x = array_shift($this->_trace);
  15  			$this->_file = $x['file'];
  16  			$this->_line = $x['line'];
  17  		}
  18  
  19  		function __construct($message = 'Unknown exception', $code = 0){
  20  			$this->Exception($message, $code);
  21  		}
  22  
  23  		function getMessage(){
  24  			return $this->_message;
  25  		}
  26  		function getCode(){
  27  			return $this->_code;
  28  		}
  29  		function getFile(){
  30  			return $this->_file;
  31  		}
  32  		function getLine(){
  33  			return $this->_line;
  34  		}
  35  		function getTrace(){
  36  			return $this->_trace;
  37  		}
  38  		function getTraceAsString(){
  39  			$s = '';
  40  			foreach($this->_trace as $i=>$item){
  41  				foreach($item['args'] as $j=>$arg)
  42  					$item['args'][$j] = print_r($arg, true);
  43  				$s .= "#$i " . (isset($item['class']) ? $item['class'] . $item['type'] : '') . $item['function']
  44  				. '(' . implode(', ', $item['args']) . ") at [$item[file]:$item[line]]\n";
  45  			}
  46  			return $s;
  47  		}
  48  		function printStackTace(){
  49  			echo $this->getTraceAsString();
  50  		}
  51  		function toString(){
  52  			return $this->getMessage();
  53  		}
  54  		function __toString(){
  55  			return $this->toString();
  56  		}
  57  	}
  58  }

Array.indexOf fix //JavaScript Function

Just an idiot fix to increase the number of my posts =b

   1  
   2  //+ Jonas Raoni Soares Silva
   3  //@ http://jsfromhell.com
   4  [].indexOf || (Array.prototype.indexOf = function(v){
   5         for(var i = this.length; i-- && this[i] !== v;);
   6         return i;
   7  });


example
   1  
   2  
   3  var x = [0,1,2,3];
   4  
   5  alert(x.indexOf(2));
   6  alert(x.indexOf(4));
« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS