Skip to content

Instantly share code, notes, and snippets.

View mtvbrianking's full-sized avatar

Brian Matovu mtvbrianking

View GitHub Profile
@mtvbrianking
mtvbrianking / delete-expired-files-recursive.php
Created December 22, 2019 17:52
Delete files older than a certain date.
<?php
/**
* Delete files older than a given age recursively.
*
* @link https://www.php.net/manual/en/class.recursivedirectoryiterator.php#111142 Source
*
* @param string $dir Path to directory having the files
* @param int $age In seconds
* @param string $pattern File name pattern
@mtvbrianking
mtvbrianking / directory tree.php
Last active December 22, 2019 17:21
Read files in directory into array.
<?php
/**
* Get files in directory.
*
* @link https://www.php.net/manual/en/class.recursivedirectoryiterator.php#111142 Source
*
* @param string $dir Directory path
*
* @throws UnexpectedValueException
@mtvbrianking
mtvbrianking / delete-expired-files.php
Last active December 22, 2019 08:14
Delete files older than a certain date.
<?php
/**
* Delete files older than a given age.
*
* @param string $dirPath Path to directory having the files
* @param int $maxAge In seconds
* @param string $pattern File name pattern
*
* @return array Deleted files
@mtvbrianking
mtvbrianking / objects_arrays.md
Created December 21, 2019 19:44 — 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:

@mtvbrianking
mtvbrianking / laravel eloquent query debug.md
Last active December 25, 2024 21:40
Laravel debug (dump) database queries

Laravel debug database queries

Raw SQL

SELECT
  users.name AS staff,
  facilities.name AS facility
FROM
  users
@mtvbrianking
mtvbrianking / windows xampp vhosts.md
Last active November 4, 2019 06:59
Windows Xampp vHosts

WINDOWS XAMPP VHOSTS

Enable vHosts

Uncommenting the following line in C:\xampp\apache\conf\httpd.conf

# Virtual hosts
Include conf/extra/httpd-vhosts.conf
@mtvbrianking
mtvbrianking / countries.php
Last active October 29, 2019 16:03
select2 multi select auto suggestion
<?php
header('Content-Type: application/json');
$countries = array(
[
'name' => 'Afghanistan',
'code' => 'AF'
],
[
@mtvbrianking
mtvbrianking / parse-google-earth-route.php
Created October 7, 2019 15:18
Parse Google Earth Route KML
<?php
class XMLElement extends \SimpleXMLElement
{
public function get($attr, $default = null)
{
if (empty($this->{$attr})) {
return $default;
}
return $this->{$attr};
@mtvbrianking
mtvbrianking / ObjectListener.php
Created October 1, 2019 16:58 — forked from soyuka/ObjectListener.php
Streaming big json files the good way with php with https://soyuka.me/streaming-big-json-files-the-good-way/
<?php
namespace Fry;
use JsonStreamingParser\Listener;
/**
* This implementation allows to process an object at a specific level
* when it has been fully parsed
*/
class ObjectListener implements Listener
{
@mtvbrianking
mtvbrianking / api-request-token.md
Last active September 21, 2019 14:48
Sample API request tokens requests
curl -X POST \
  http://localhost:8000/api/v1/oauth/token \
  -H 'Accept: application/json' \
  -d 'grant_type=authorization_code' \
  -d 'client_id=b8e7163a-83a0-4386-b0bc-697a96edef14' \
  -d 'client_secret=bNQbpQnMPXayTn8XFPRQDWvIyyclN5pKnccVVUNx' \
  -d 'redirect_uri=http://localhost:8000/test/callback' \
  -d 'code=def502006626c316151b4f074308...'