Array repack operator in PHP concept

Presenting concept of array operator which repacks array by keys to newly created array. I've called it Array Repacking because such functionality will produce new array based on original array just with some few improvements.

Syntax

Proposed syntax doesn't break any current syntax upon PHP 7.1.

$strings = ['one' => 1, 'two' => 2, 'three' => 3];
$onlyThreeAndTwo = $strings['three', 'two'];

var_dump($onlyThreeAndTwo);
//array(2) {
//  ["three"]=>
//  int(3)
//  ["two"]=>
//  int(2)
//}
Nested/Inner class UML presentation

Inner Classes in PHP concept

Presenting concept of inner classes for PHP based on other languages and regarding PHP limitations. Inner class is a class that is declared inside the scope of another class. A nested class has access to the variables and other symbols of the classes it is nested inside.

Compelling reasons for using nested classes include the following:

  • It is a way of logically grouping classes that are only used in one place
  • It increases encapsulation
  • It can lead to more readable and maintainable code

The main advantage is organization and encapsulation.

Singleton pattern UML

Why Singleton Pattern in PHP sucks!

Recently at workshop with Mariusz Gil I've been trying to implement secure singleton pattern in PHP. It may sound ridiculous but there actually is no way to implement this pattern the Right Way. Speaking of the Right Way in this case I mean implementing a class which can produce one single instance of self and protecting against another objects intance creation. I'll explain why it's impossible using PHP.

Why Object Types are usefull in Hydrators and Extractors

There are many implementations of Hydrator and Extractor pattern. What is Hydration?

Hydration is the act of populating an object from a set of data.

Hydrators have wide usage in ORM and Data Mappers their responsibility is to provide entity objects from relational database. Hydrators allow objects to be filled with data from an array. Extractors do the opposite, they take an object and return an array of data.

We will improve Zend/Std/Hydrator interfaces with Object Types increasing readability and runtime type checks.

interface HydrationInterface {
  /**
   * Hydrate an object by populating data
   * @param array $data Data to populate
   * @param object $object Object to populate data
   * @return object
   */
  public function hydrate(array $data, object $object) : object;
}
interface ExtractionInterface {
  /**
   * Extract values from an object
   * @param object $object Object to extract data from
   * @return array
   */
  public function extract(object $object) : array;
}

Creating simple Dependency Injection Container with Object-Types

Simple DIC implementation based on proposed RFC:Object-typehint implementation to providing a proof-of-concept and real life example of usage.

Mentioned RFC proposes object type-hint and return-type should be added to the engine, working the same way like in case of scalar does

interface ServiceContainer {
  /**
   * Set service definition
   * @param string $id Service identifier
   * @param object $service Service object or closure
   */
  public function set(string $id, object $service);
  /**
   * Retrieve service object from container
   * @param string $id Service identifier
   * @return
   */
  public function get(string $id) : object;
}