Для получения полного доступа
зарегистрируйтесь.

Section:
Sub-section::
Languages:


protonalex
  • Reputation: 26
  • Snippets: 21
  • Revisions: 1

Function to sort an array according to its keys which are floats casted as strings.

function sort_string_float($a, $b) {

	$a = (float) $a;
	$b = (float) $b;

	if ($a == $b) {
		return 0;
	}

	return $a < $b ? -1 : 1;

}

// use
uksort($l1_data, 'self::sort_string_float');
protonalex
  • Reputation: 26
  • Snippets: 21
  • Revisions: 1

Check whether value an array is not empty.

$arr = ['', 'apple', ''];

if (!empty(array_filter($arr))) {
    // do something
}
samdark
  • Reputation: 349
  • Snippets: 57
  • Revisions: 5

After posting about fast hydration I've got response with even faster method that doesn't require extra class. Credits go to Vladimir Chub.

$postHydrator = function(array $data) {
    $this->id = $data['id'];
    $this->title = $data['title'];
    $this->text = $data['text'];
};

$postExtractor = function() {
    return [
        'id' => $this->id,
        'title' => $this->title,
        'text' => $this->text
    ];
}

// $data is from database

$post = new Post();
$hydrator = $postHydrator->bindTo($post, $post);
$hydrator($data);

samdark
  • Reputation: 349
  • Snippets: 57
  • Revisions: 5

It's important for object oriented programming and domain driven design not to violate incapsulation i.e. what should not be set by developer should be private or protected:

class Post
{
    protected $id;
    protected $title;
    protected $text;

    public function getId()
    {
        return $this->id;
    }

    public function getTitle()
    {
        return $this->title;
    }

    public function setTitle($title)
    {
        $this->title = $title;
    }

    public function getText()
    {
        return $this->text;
    }

    public function setText($text)
    {
        $this->text = $text;
    }
}

When it comes to pesisting data in database and getting it back there's a problem of filling properties we can't set. Usually it's solved by using reflection but it's quite slow when it comes to hundreds of objects. Marco Pivetta posted a better approach which despite being meant for Doctrine internals is quite simple and interesting.

class PostSelfHydratingProxy extends Post
{
    /** zero-argument constructor since this is just a helper class for hydration */
    public function __construct()
    {
    }
    
    public function hydrate($data, Post $object)
    {
		$object->id = $data['id'];
        $object->title = $data['title'];
        $object->text = $data['text'];
    }

    
    public function extract(Post $object)
    {
        return [
            'id' => $object->id,
            'title' => $object->title,
            'text' => $object->text
        ];
    }
}

// $data is from database

$post = new Post();
$proxy = new PostSelfHydratingProxy();
$proxy->hydrate($data, $post);

samdark
  • Reputation: 349
  • Snippets: 57
  • Revisions: 5

It's possible to shuffle array once a week without storing anything. I've found the solution at StackOverflow.

It's based on initializing random number generator which is used by shuffle() with a seed that's constant for each week. date('W') is a week number in a year which fits the purpose well. After shuffling is done RNG is put back to normal by calling srand() with no argument i.e. initilizing it with random seed.

srand(date('W'));
shuffle($array);
srand();
samdark
  • Reputation: 349
  • Snippets: 57
  • Revisions: 5

In many cases it's useful to know if environment uses Suhosin.

if (extension_loaded('suhosin')) {
   echo 'Installed as PHP extension';
} elseif (defined("SUHOSIN_PATCH")) {
   echo 'Patched into PHP';
} else {
   echo 'Not detected';
}
protonalex
  • Reputation: 26
  • Snippets: 21
  • Revisions: 1

Check whether haystack ends with needle.

function ends_with($haystack, $needle) {

	if ($needle === '') {
    	return true;
	}

	return strtolower(substr($haystack, -strlen($needle))) === $needle;

}
protonalex
  • Reputation: 26
  • Snippets: 21
  • Revisions: 1

Check whether haystack starts with needle.

function starts_with($haystack, $needle) {
	return $needle === '' || stripos($haystack, $needle) === 0;
}
samdark
  • Reputation: 349
  • Snippets: 57
  • Revisions: 5

Despite it's quite common requirement to assert that elements in two arrays are the same while order doesn't matter, it's not that obvious on how to do it in PHPUnit.

$array = ['b', 'a'];
$this->assertEquals(['a', 'b'], $array, '', 0.0, 10, true);
samdark
  • Reputation: 349
  • Snippets: 57
  • Revisions: 5

Since PHP 5.4 it's possible to get script execution time without prior recording of script initial timestamp.

echo microtime(true) - $_SERVER["REQUEST_TIME_FLOAT"];