Skip to content

Instantly share code, notes, and snippets.

@jhartikainen
jhartikainen / gist:1050932
Created June 28, 2011 11:21
PHP error to exception converter with special invalid argument handling
<?php
//This converts errors into exceptions, but if the error is caused
//by an argument being invalid (for example failing a typehint), it gets converted into an InvalidArgumentException
function handle($code, $message, $file, $line) {
//This test might be naive but it worked in my very very simple test code :)
if(strpos($message, 'Argument ') === 0) {
throw new InvalidArgumentException($message);
}
else {
throw new ErrorException($message, 0, $code, $file, $line);
@jhartikainen
jhartikainen / gist:1046179
Created June 25, 2011 05:15
Format a numeric string every three numbers
//Is this abuse of array functions? :D
'20000001'.split('').reverse().join('').match(/\d{3}|\d+$/g).map(function(s){ return s.split('').reverse().join(''); }).reverse().join(' ');
//produces "20 000 001"
<?php
$obj = new stdClass();
$obj->foo = function() { };
//BOOM!
$obj->foo();
//Fatal error: Call to undefined method stdClass::foo()
// :(
@jhartikainen
jhartikainen / gist:964400
Created May 10, 2011 12:44
Beautiful jQuery checkebox toggler pattern! (or maybe not...)
$('#emailAlert').change(function() {
$(this.form.emailAddress)[
this.checked ? 'show' : 'hide'
]();
});
var myWidget = function() {
//create widget and all stuff here
//return an object with methods, which then muck with the widget & co. created inside the closure here
return {
doStuff: function() ...
};
};
@jhartikainen
jhartikainen / gist:829408
Created February 16, 2011 13:59
Using $this in closure example
public function x() {
$self = $this;
$closure = function() use ($self) {
//You can call public members of $self here but not protected/private
}
}
######################################################################
# jdong's zshrc file v0.2.1 , based on:
# mako's zshrc file, v0.1
#
#
######################################################################
# next lets set some enviromental/shell pref stuff up
# setopt NOHUP
#setopt NOTIFY
Name : php Relocations: (not relocatable)
Version : 5.2.9 Vendor: (none)
Release : 1 Build Date: Mon 02 Mar 2009 01:18:43 PM EET
Install Date: Wed 21 Oct 2009 10:29:14 AM EEST Build Host: phpbuild
Group : Development/Languages Source RPM: php-5.2.9-1.src.rpm
Size : 3122367 License: PHP
Signature : (none)
URL : http://www.php.net/
Summary : The PHP HTML-embedded scripting language
Description :
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
var docu = document.body.getElementsByTagName('iframe')[4].contentDocument;
x = docu.evaluate('/html/body/div/div[2]/div/div[2]/div/div[3]/div/div/div/div[2]/div/div/div/div/div[2]/div/div[2]/div/div/div/div/div[3]/div/div/div/div/div/div/div/div[2]/b', docu, null, XPathResult.ANY_TYPE, null);
var el = x.iterateNext();
el.innerHTML = 'blah blah blah';