Skip to content

Instantly share code, notes, and snippets.

View vanchelo's full-sized avatar
💭
Life is good

Brezhniev Ivan vanchelo

💭
Life is good
View GitHub Profile
@vanchelo
vanchelo / gist:3059790
Created July 6, 2012 12:07
Pagebreaker
<?php
if ($modx->Event->name == 'OnPageNotFound') {
$uri = $_SERVER['REQUEST_URI'];
// Обычный ресурс
if (preg_match("#(\.*?[a-z0-9_\-]+)_p(\d+)\.html#si", $uri, $m)) {
$alias = preg_replace("#(\.*?[a-z0-9_\-]+)_p(\d+)(\.html)#si", "$1$3", $uri);
$alias = substr($alias, 1, strlen($alias));
}
// Контейнер
@vanchelo
vanchelo / chmod_site.sh
Created September 8, 2012 08:44
Set right permissions for files of site
#!/bin/bash
dir=/var/www/site
user=userofsite
echo "Set permissions for $dir...";
echo "CHOWN files...";
chown -R $user:$user "$dir";
echo "CHMOD directories...";
find "$dir" -type d -exec chmod 0755 '{}' \;
<?php
$range=20; //Размер массива
$arrInt = range(0, $range); //Создаем массив array(0=>1, 1=>2 , 2=>3, ... (n-1)=>n); где n = $range
$arrNull=array_fill(0,$range,null); //Создаем массив с $range элементами значение которых null
$arr = array_merge($arrInt,$_SERVER,$arrNull); //Смешиваем все массивы
$find = (rand(0,1) ? 'несуществующий индекс' : array_rand($arr,1)); //Определяем какой индекс будем искать
echo "<h3>Поиск элемента с индексом <u>{$find}</u> в массиве \$arr</h3>";
<?php
$range = 200; //Размер массива
$run = 100; //Колличество поисков
$mode = isset($_GET['mode']) ? $_GET['mode'] : '';
$time = 0;
$arr = getArr($range); //Создаем массив
for($i=$run; $i>0; $i--){
$find = (mt_rand(0,1) ? 'несуществующий индекс' : array_rand($arr,1)); //Определяем какой индекс будем искать
@vanchelo
vanchelo / hint.js
Last active December 20, 2015 13:09 — forked from anonymous/hint.js
/*DISPLAY HINTS FOR INPUTS*/
$('input').on('focus', function() {
var hint = $(this).attr('hint');
if(hint !== undefined){
$(this).after('<div class="hint">'+hint+'</div>');
$('.hint').fadeIn('fast');
$(this).closest('.row').find('label.error').hide();
}
})
@vanchelo
vanchelo / 0_reuse_code.js
Created December 21, 2013 23:06
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console

My Validation Base Class

I was asked how I deal with validation / create and update validation rulesets. Well here is one method I have used. Don't be afraid to build on top of what the framework has already given you. In my projects I use a base class for almost anything. You never know when you want your classes to inherit some common functionality. My BaseValidator actually has some pretty useful methods and properties in it.

<?php

namespace FooProject\Internal\Validators;

use FooProject\Internal\Sanitizers\BaseSanitizer;
@foreach(array_chunk($posts, 3) as $postSet)
<div class="row"> <!-- this div will surround every three posts -->
@foreach($postSet as $post)
<h3>{{ $post['title'] }}</h3>
@endforeach
</div>
@endforeach

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:

<?php
<<<CONFIG
packages:
- "lavoiesl/php-benchmark: dev-master"
CONFIG;
/**
* run command "melody run https://gist.githubusercontent.com/vanchelo/4e6476b52fa6fc665b0b/raw/f965054e77d7c431fdfb25ad096e378a3fb1a52f/speedtest.php -vvv"
*/