Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View hopeseekr's full-sized avatar

Theodore R. Smith hopeseekr

View GitHub Profile
@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 / 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 / deep_objects.php
Last active November 21, 2019 11:47
Avoid PHP Memory Leaks in Long-lived Objects
<?php
// Avoid PHP Memory Leaks in Long-lived Objects
// License: Public Domain
class LongLivedObject
{
public function __construct() {
echo "Object " . spl_object_hash($this) . " was just born!\n";
}
@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 / docker_dedicated_filesystem.md
Created February 3, 2018 04:01
Putting Docker on its own pseudo filesystem

Docker on BTRFS is very buggy and can result in a fully-unusable system, in that it will completely butcher the underlying BTRFS filesystem in such a way that it uses far more disk space than it needs and can get into a state where it cannot even delete any image, requiring one to take drastic actions up to and including reformatting the entire affected BTRFS root file system.

According to the official Docker documentation:

btrfs requires a dedicated block storage device such as a physical disk. This block device must be formatted for Btrfs and mounted into /var/lib/docker/.

In my experience, you will still run into issues even if you use a dedicated partition. No, it seems it requires a standalone

@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) +