Defining the IReadOnlyDictionary interface in PHP

.NET PHP Interface
The generic IReadOnlyDictionary interface was first introduced to .NET in Framework version 4.5, way back in August 2012. At this time, I'd been working with C# for only a few months and I remember that .NET Framework 4.5 introduced a whole range of new features, classes and interfaces.

One of these new interfaces was the generic IReadOnlyDictionary interface.

There had been a generic IDictionary interface in .NET since version 2.0 (January 2006) but this included two additional methods Add() and Remove() which meant that the dictionary object that implemented this was designed to be modified.

A read-only dictionary is a set of key-value pairs which cannot be modified by the calling method. There is nothing to stop the object which implements the IReadOnlyDictionary interface from modifying its own data, but from an external "public" perspective, the dictionary has a fixed set of keys and values. As with all dictionary types, the key must be unique and the value does not need to be unique.

In .NET, the IReadOnlyDictionary interface is generic, meaning that you as a coder can specify the key and value types. PHP does not yet support generics. If it did, that would be amazing. (Hey, PHP guys, get a move on!)

So instead we need a string version of the IReadOnlyDictionary interface; hence IReadOnlyStringDictionary.

But why do we need this interface?



Good question. PHP already contains an array() structure, so why don't we just use that as a parameter? The reason is that array() is not type safe, and the keys can be a range of scalar types.

This interface helps us to pass parameters to methods where a dictionary is required, without needing to know the concrete implementation. In short, it enables you to simplify your code.

I've built a IReadOnlyStringDictionary interface to represent a set of key-values that are both strings, inspired by .NET.


<?php

namespace ACA\Collections
{
/**
* Represents a read-only collection of string key/value pairs.
* @author Antony Charles Allen
* @since 11th August 2020
* @link docs.microsoft.com
*/
interface IReadOnlyStringDictionary
{
/**
* An array that contains the keys in the read-only dictionary.
*/
function Keys() : array;

/**
* An enumerable collection that contains the values in the read-only dictionary.
*/
function Values() : array;

/**
* Determines whether the read-only dictionary contains an element that has the specified key.
* @param string $key The key to locate.
*/
function ContainsKey(string $key) : bool;

/**
* ArrayAccess interface equivalent of ContainsKey()
* @param string $offset An offset to check for.
*/
function offsetExists($offset) : bool;

/**
* Gets the value that is associated with the specified key.
* @param string $key The key to locate.
* @param string $value When this method returns, the value associated with the specified key, if the key is found; otherwise, the default value for the type of the value parameter.
*/
function TryGetValue(string $key, string & $value) : bool;

/**
* Gets the element that has the specified key in the read-only dictionary.
* @param string $offset The key to locate.
*/
function offsetGet($offset) : string;
}
}



As always, feel free to use this code, modify it, have fun and don't forget to support me 😄
Hey you! I need your help!

Thanks for reading! All the content on this site is free, but I need your help to spread the word. Please support me by:

  1. Sharing my page on Facebook
  2. Tweeting my page on Twitter
  3. Posting my page on LinkedIn
  4. Bookmarking this site and returning in the near future
Thank you!