Recommend this page to a friend! |
![]() |
Info | Documentation | ![]() |
![]() |
![]() |
Reputation | Support forum | Blog | Links |
Last Updated | Ratings | Unique User Downloads | Download Rankings | |||||
2024-05-16 (10 months ago) ![]() | Not yet rated by the users | Total: 19 | All time: 11,370 This week: 73![]() |
Version | License | PHP version | Categories | |||
typed-arrays 1.0 | MIT/X Consortium ... | 5 | Data types, PHP 8 |
Description | Author | |
This package can implement arrays of values of only one type. |
|
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
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.
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"
}
}
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) {
}
}
}
See: the autoloader.
![]() |
File | Role | Description | ||
---|---|---|---|---|
![]() |
||||
![]() |
||||
![]() |
||||
![]() |
||||
![]() ![]() |
Data | Auxiliary data | ||
![]() ![]() |
Data | Auxiliary data | ||
![]() ![]() |
Data | Auxiliary data | ||
![]() ![]() |
Doc. | Documentation |
The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page. |
![]() |
Version Control | Unique User Downloads | Download Rankings | |||||||||||||||
100% |
|
|
Applications that use this package |
If you know an application of this package, send a message to the author to add a link here.