Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

The typed array for PHP

License

NotificationsYou must be signed in to change notification settings

ngmy/php-typed-array

Repository files navigation

Latest Stable VersionTotal DownloadsLatest Unstable VersionLicensecomposer.lockPHP CICoverage StatusPHPStanPsalm CoveragePsalm Level

PHP Typed Array is the typed array for PHP.

  • Can create the typed array with the specified key and value type
  • Can specify the bool, float, int, object, resource, or string type, or the specified class type, or the class type that implements the specified interface, or the class type that uses the specified trait for the key type
  • Can specify the array, bool, float, int, object, resource, or string type, or the specified class type, or the class type that implements the specified interface, or the class type that uses the specified trait for the value type
  • Implements theArrayAccess,Countable, andIteratorAggregate interfaces
  • Supports the static analysis like PHPStan and Psalm. Please seeexamples
// Returns a new instance of the typed array with the int type value$intArray =Ngmy\TypedArray\TypedArray::new()->withIntValue();// TypedArray<mixed, int>$intArray[] =1;// Good// $intArray[] = '2'; // No good. The InvalidArgumentException exception is thrown// Returns a new instance of the typed array with the class type value that implements the DateTimeInterface interface$dateTimeInterfaceArray =Ngmy\TypedArray\TypedArray::new()    ->withInterfaceValue(DateTimeInterface::class);// TypedArray<mixed, DateTimeInterface>$dateTimeInterfaceArray[] =newDateTime();// Good$dateTimeInterfaceArray[] =newDateTimeImmutable();// Good// $dateTimeInterfaceArray[] = new stdClass();       // No good. The InvalidArgumentException exception is thrownforeach ($dateTimeInterfaceArrayas$dateTime) {echo$dateTime->format('Y-m-d H:i:s') .PHP_EOL;}// Determines if the typed array is empty or notechovar_export($dateTimeInterfaceArray->isEmpty(),true) .PHP_EOL;// false// Gets the typed array of items as a plain arrayprint_r($dateTimeInterfaceArray->toArray());// Array// (//     [0] => DateTime Object//         ...//     [1] => DateTimeImmutable Object//         ...// )// You can also specify the type of the key$stringKeyArray =Ngmy\TypedArray\TypedArray::new()->withStringKey();// TypedArray<string, mixed>$stringKeyArray['foo'] =1;// Good// $stringKeyArray[] = 2;   // No good. The InvalidArgumentException exception is thrown// Of course, you can also specify the type of both the key and the value$intStringArray =Ngmy\TypedArray\TypedArray::new()->withIntKey()->withStringValue();// TypedArray<int, string>

Requirements

PHP Typed Array has the following requirements:

  • PHP >= 7.3

Installation

Execute the Composerrequire command:

composer require ngmy/typed-array

Documentation

Please see theAPI documentation.

License

PHP Typed Array is open-sourced software licensed under theMIT license.

About

The typed array for PHP

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

  •  

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp