Skip to content

Instantly share code, notes, and snippets.

@Trainmaster
Trainmaster / add.php
Created January 11, 2018 10:12
Behaviour of weak type check (string/float)
<?php
function add(float $a, float $b): float {
return $a + $b;
}
@Trainmaster
Trainmaster / ImageMagickCommandLineProcessor.php
Last active January 2, 2016 21:09
ImageMagickCommandLineProcessor for processing uploaded image files
<?php
/**
* ImagickProcessor
*
* @author Frank Liepert <contact@frank-liepert.de>
*/
class ImageMagickCommandLineProcessor
{
/** @type string $error */
protected $error;
<?php
try {
$pdflib = new PDFlib;
$colorspace = new Colorspace($pdflib);
print_r ($colorspace->analyze('test.pdf'));
} catch (PDFlibException $e) {
print_r ($e);
}
class Colorspace
@Trainmaster
Trainmaster / signatures.php
Created September 16, 2013 19:43
signature
<?php
class Parents
{
public function doSomething($x)
{
print $x;
}
}
class ChildA extends Parents
<?php
class Foo
{
protected $bar;
public function __construct()
{
$this->bar = 'bar';
}
<?php
$container->addDefinition('AnotherService', new Definition('Application\Service\AnotherService'))
->setter('setFoo', 'Foo');
$container->addDefinition('ExampleService', new Definition('Application\Service\ExampleService'))
->constructor(array('@AnotherService'));
$container->addDefinition('Session', new Definition('lib\Session\Session'));
$container->addDefinition('SessionAwareInterface', new Definition('lib\Session\SessionAwareInterface'))
@Trainmaster
Trainmaster / htmlspecialchars-types.php
Last active December 17, 2015 07:48
Behaviour of htmlspecialchars() (or any other string function?) when passing arguments with different types
<?php
$string = 'foo';
var_dump(htmlspecialchars($string));
// string(3) "foo"
$integer = 1;
var_dump(htmlspecialchars($integer));
// string(1) "1"
$float = 1.2;
@Trainmaster
Trainmaster / gist:5330082
Last active December 15, 2015 21:59
Extract array values by html array notation.
<?php
function getInputValueByName($name, $data) {
if (strpos($name, '[]') !== false) {
$name = str_replace('[]', '', $name);
}
$parts = explode('[', $name);
$value = $data;
foreach ($parts as $part) {
$part = rtrim($part, ']');
<?php
$data = array('foo', '', 'bar');
array_walk_recursive($data, function (&$value, $key) {
if ($value === '') {
$value = null;
}
});
var_dump($data);