Skip to content

Instantly share code, notes, and snippets.

View m8rge's full-sized avatar
👨‍💻
Code is for developers first

Andrey m8rge

👨‍💻
Code is for developers first
View GitHub Profile
@m8rge
m8rge / WithoutTrait.php
Last active June 8, 2017 04:40
Without yii2 activerecord trait
<?php
namespace common\ActiveRecordTrait;
use yii\db\ActiveQuery;
use yii\db\ActiveRecord;
trait WithoutTrait
{
@m8rge
m8rge / HashBuilder.php
Last active August 29, 2015 14:21 — forked from mncaudill/similar.php
Image similarity algorithm
<?php
# Algorithm found here: http://www.hackerfactor.com/blog/index.php?/archives/432-Looks-Like-It.html
class HashBuilder
{
private $buf;
function __construct()
{
$this->buf = imagecreatetruecolor(8, 8);
}
<?php
namespace console\components;
class XMLReader extends \XMLReader
{
/**
* @param string $nodeName
* @return $this
@m8rge
m8rge / DbTestCase.php
Created July 2, 2015 03:25
Yii2 db fixtures from array
<?php
namespace tests\codeception\common\unit;
use Codeception\Specify;
use yii\di\Instance;
use yii\test\InitDbFixture;
/**
* usage:
@m8rge
m8rge / CronException.php
Last active October 18, 2018 04:14
Yii2 console controller behavior. Prevents double run console command
<?php
namespace console\components;
use Throwable;
use yii\base\Action;
class CronException extends \RuntimeException
{
/**
@m8rge
m8rge / AsteriskMatch.php
Last active January 29, 2016 08:16
Test string against simple mask with asterisk *
<?php
class AsteriskMatch
{
/**
* @param string $mask string with asterisk
* @param string $string tested subject
* @return bool
* @throws \Exception
*/
@m8rge
m8rge / InMemoryCache.php
Last active February 3, 2016 07:36
Simple limited memory cache
<?php
class InMemoryCache
{
public $size = 10;
protected $cache = [];
/**
* @param string $key
@m8rge
m8rge / TruncateString.php
Last active November 3, 2016 06:51
Truncate string on word break
<?php
/**
* @param string $string Subject
* @param int $length Max string length
* @param bool $exactLength Truncate string with exact $length
* @param string $append Ellipsis string
* @return string
*/
class TruncateString
@m8rge
m8rge / ConsoleProgress.php
Last active November 3, 2016 06:50
Simple yii2 console progress helper
<?php
use yii\base\Object;
use yii\helpers\Console;
/**
* Usage:
* $consoleProgress = new ConsoleProgress(['max' => 365]);
* $consoleProgress->start();
* foreach($days in $day) {
@m8rge
m8rge / post-or-pre.md
Last active February 4, 2016 10:54
Что потребляет больше памяти в php: преинкремент или постинкремент?

Что потребляет больше памяти в php: преинкремент или постинкремент?

Существует мнение, что преинкремент потребляет меньше памяти, т.к. он инкрементирует саму переменную, а постинкремент помимо этого, копирует ее предыдущее значение во временную переменную.

Давайте рассмотрим следующий код:

1. <?php
2. $a = 1;
3. echo $a++;
4. $b = 1;