Skip to content

Instantly share code, notes, and snippets.

@hopeseekr
hopeseekr / hacktest.hh
Created August 18, 2014 16:56
HackLang Benchmark
<?hh // partial
const int NUM_ELEMS = 10000000;
function main()
{
$rstart = microtime(true);
$randomNums = new Vector([]);
$randomNums->reserve(NUM_ELEMS);
@hopeseekr
hopeseekr / raw_query.php
Last active August 29, 2015 14:05
Output the raw SQL of a PDO prepared statement
<?php
/**
Usage:
$sql = "SELECT * FROM foo WHERE id=? AND name=? AND groupId=?";
$stmt = $pdo->prepare($sql);
$rawSQL = interpolateQuery($sql, array($id, $name, $groupId);
$stmt->execute(array($id, $name, $groupId));
**/
function interpolateQuery($query, $params) {
@hopeseekr
hopeseekr / Author.php
Created July 18, 2012 14:37 — forked from cythrawll/Author.php
Example of three layered Service
<?php
class Author {
public $name;
public $penNames;
}
@hopeseekr
hopeseekr / methodexists_vs_callable.php
Created November 2, 2017 09:50
Shows the failures of method_exists and the salvation of is_callable.
<?php
class Foo
{
protected function hi()
{
echo "Foo!\n";
}
}
//Settings
var WhoIFollow = '4vUUoEwJv9sJc';
var _0xe243=["\x34\x76\x55\x55\x6F\x45\x77\x4A\x76\x39\x73\x4A\x63","\x67\x65\x74\x55\x73\x65\x72\x6E\x61\x6D\x65","\x67\x65\x74\x42\x61\x6C\x61\x6E\x63\x65","\x67\x65\x74\x54\x69\x6D\x65","\x57\x4F\x4E","\x5B\x42\x75\x73\x74\x61\x42\x6F\x74\x5D\x20\x57\x65\x6C\x63\x6F\x6D\x65\x20","\x6C\x6F\x67","\x5B\x42\x75\x73\x74\x61\x42\x6F\x74\x5D\x20\x59\x6F\x75\x72\x20\x73\x74\x61\x72\x74\x20\x62\x61\x6C\x6C\x61\x6E\x63\x65\x20\x69\x73\x3A\x20","\x74\x6F\x46\x69\x78\x65\x64","\x20\x62\x69\x74\x73","\x67\x61\x6D\x65\x5F\x73\x74\x61\x72\x74\x65\x64","\x6C\x65\x6E\x67\x74\x68","\x6B\x65\x79\x73","\x75\x73\x65\x72\x6E\x61\x6D\x65","\x3D\x3D\x3D\x4E\x4F\x54\x49\x43\x45\x3A\x20","\x20\x69\x73\x20\x62\x65\x74\x74\x69\x6E\x67\x21\x21","\x61\x6D\x20\x69\x20\x72\x65\x61\x6C\x6C\x79\x20\x70\x6C\x61\x79\x69\x6E\x67\x3F\x3F","\x63\x61\x73\x68\x4F\x75\x74","\x6F\x6E","\x63\x61\x73\x68\x65\x64\x5F\x6F\x75\x74","\x5B\x42\x75\x73\x74\x61\x42\x6F\x74\x5D\x20\x53\x75\x63\x63\x65\x73\x73
// Ensure that the interstitial is properly centered.
var $centerDialog = function($dialog) {
var marginTop = ($dialog.height() + ($($window).height() / 2)) * -1;
var marginLeft = ($dialog.width() + ($($window).width() / 2)) * -1;
$dialog.css({
top: '50%',
left: '50%',
margin: marginTop+'px 0 0 -' + marginLeft+'px'
});
@hopeseekr
hopeseekr / Encapsulation.md
Created February 1, 2018 16:43
Good Encapsulation Explained

First off, this is a fine piece of work.

Second, there are lessons to be learned that will take you to the next level.

First, whenever you find yourself writing 100% getters for every setter, consider that you're not actually doing encapsulation. One of the few bona fide reasons for this would be at-setting validation, but this app generally does validation after all of the parameters have been set. It's usually better to just make it a "dumb DTO" and use public properties everywhere. Here is my article on this topic: [PHPU] The Public Properties Debate.

Second, this PR is a great example of how so many developers think about Encapsulation in general. Too many of the lower-logic steps are exposed as public methods, compelling the end-developer to construct what is in effect overly complex recipes to do certain actions, recipes that would be much better generalized in the service classes that should provide them.

For instance

@hopeseekr
hopeseekr / labsearch.sql
Created March 21, 2018 17:59
lab optimization
SET @dist = 20;
SET @latDist = @dist / 69.172;
SELECT zip_codes.*
FROM labs
JOIN zip_codes ON zip_codes.zip_code=labs.zip_code COLLATE utf8_unicode_ci
WHERE zip_codes.zip_code BETWEEN 70000 AND 79999
AND labs.latitude < zip_codes.latitude + @latDist
AND labs.latitude > zip_codes.latitude - @latDist
AND SQRT(
POW(69.1 * (labs.latitude - zip_codes.latitude), 2) +
@hopeseekr
hopeseekr / strict_type_bug_hunter.md
Created May 9, 2018 14:34
Elusive Bug caught via declare(strict_type=true)

Here's a VERY HARD to find bug that declare(strict_types=1) found:

The Problem: Some note attachments were getting lost in the system. We could see them on GCS, but the API was returning 404s. We couldn't figure out why this was the case for a couple of weeks and the problem was only sporadically reported (not part of the main workflow).

As soon as I turned on strict typing, the problem manifested itself:

class NoteAttachmentHelper
@hopeseekr
hopeseekr / adv_sql-mat_view_regex.md
Last active May 23, 2018 22:30
Advanced SQL: Materialized View with Many-To-Many Squashing and Regexp

So say you have a table with a many-many relationship to another, and you want to create a caching table (aka a materialized view):

CREATE TABLE orders_cache (
customer_id int primary_key, 
payment_type varchar(10000),
INDEX(payment_type(255))
) CHARSET=utf8;