PHP Classes

How to Implement a PHP Typed Array in Pure PHP using the Package Typed Arrays: Implement arrays of values of only one type

Recommend this page to a friend!
  Info   View files Documentation   View files View files (19)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2024-05-16 (Yesterday) RSS 2.0 feedNot yet rated by the usersTotal: 14 This week: 14All time: 11,273 This week: 8Up
Version License PHP version Categories
typed-arrays 1.0MIT/X Consortium ...5Data types, PHP 8
Description 

Author

This package can implement arrays of values of only one type.

It provides a base class that implements the ArrayAccess interface to make its object work like arrays of values of a single type.

The package also provides several sub-classes to implement arrays of only one value.

Currently, it provides classes that implement arrays of values of the types: boolean, float, integer, object, and string.

Picture of Scott Arciszewski
  Performance   Level  
Name: Scott Arciszewski <contact>
Classes: 37 packages by
Country: United States United States
Innovation award
Innovation award
Nominee: 28x

Winner: 1x

Documentation

Strictly Typed Arrays in PHP 8

Build Status Latest Stable Version Latest Unstable Version License Downloads

Requires PHP 8.3. This is best described through example:

<?php
require_once 'vendor/autoload.php';

class Foo
{
    public function __construct(
        public readonly string?? $foo,
        public readonly int?? $bar
    ) {}
}

$x = new Foo(
    string??('apple', 'bee'),
    int??(4, 5, 120000),
);
var_dump($x->foo, $x->bar);
var_dump($x->foo[1]);

This should output the following:

object(string??)#5 (2) {
  [0]=>
  string(5) "apple"
  [1]=>
  string(3) "bee"
}
object(int??)#6 (3) {
  [0]=>
  int(4)
  [1]=>
  int(5)
  [2]=>
  int(120000)
}
string(3) "bee"

If you try to pass an incorrect type, you'll get a TypeError:

<?php
declare(strict_types=1);
require_once 'vendor/autoload.php';

class Foo
{
    public function __construct(
        public readonly string?? $foo
    ) {}
}

$x = new Foo(
    string??('apple', 'bee', 25)
);
var_dump($x->foo, $x->bar);

Should produce:

Fatal error: Uncaught TypeError: string??(): Argument #3 must be of type string, int given

What Is This Package Doing?

We are using Unicode characters (? and ?) to create a class that implements ArrayAccess. All arguments to these types are then strictly typed.

In effect, we have turned a class into a typed array that your IDE will not complain about.

Does It Support Multi-Level Types? e.g. string????

You betcha.

<?php
declare(strict_types=1);
require_once 'vendor/autoload.php';

class Bar
{
    public function __construct(
        public readonly string???? $double,
    ) {}
}

$test = new Bar(string????(
    string??('test'),
    string??('example'),
));
var_dump($test->double);

This will produce:

object(string????)#7 (2) {
  [0]=>
  object(string??)#5 (1) {
    [0]=>
    string(4) "test"
  }
  [1]=>
  object(string??)#6 (1) {
    [0]=>
    string(7) "example"
  }
}

Does This Support Arrays of Classes?

Of course!

<?php
declare(strict_types=1);
require_once 'vendor/autoload.php';

class Foo {}

class Bar
{
    public function __construct(
        public readonly Foo?? $example
    ) {}
}

$test = new Bar(new Foo??(new Foo));
var_dump($test);

Output:

object(Bar)#2 (1) {
  ["example"]=>
  object(Foo??)#5 (1) {
    [0]=>
    object(Foo)#6 (0) {
    }
  }
}

How Does This Create Types for My Classes?

See: the autoloader.


  Files folder image Files  
File Role Description
Files folder image.github (1 directory)
Files folder imageglobal (7 files)
Files folder imagesrc (6 files)
Files folder imagetests (1 file)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file phpunit.xml Data Auxiliary data
Accessible without login Plain text file psalm.xml Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  .github  
File Role Description
Files folder imageworkflows (1 file)

  Files folder image Files  /  .github  /  workflows  
File Role Description
  Accessible without login Plain text file ci.yml Data Auxiliary data

  Files folder image Files  /  global  
File Role Description
  Accessible without login Plain text file autoloader.php Class Class source
  Accessible without login Plain text file bool-array.php Class Class source
  Accessible without login Plain text file double-array.php Class Class source
  Accessible without login Plain text file float-array.php Class Class source
  Accessible without login Plain text file functions.php Aux. Auxiliary script
  Accessible without login Plain text file int-array.php Class Class source
  Accessible without login Plain text file string-array.php Class Class source

  Files folder image Files  /  src  
File Role Description
  Accessible without login Plain text file AbstractTypedArray.php Class Class source
  Accessible without login Plain text file BoolArray.php Class Class source
  Accessible without login Plain text file FloatArray.php Class Class source
  Accessible without login Plain text file IntArray.php Class Class source
  Accessible without login Plain text file ObjectTypedArray.php Class Class source
  Accessible without login Plain text file StringArray.php Class Class source

  Files folder image Files  /  tests  
File Role Description
  Accessible without login Plain text file BasicTest.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:14
This week:14
All time:11,273
This week:8Up