Download the PHP package edgaralexanderfr/php-types without Composer
On this page you can find all versions of the php package edgaralexanderfr/php-types. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download edgaralexanderfr/php-types
More information about edgaralexanderfr/php-types
Files in edgaralexanderfr/php-types
Package php-types
Short Description Library intended to expand PHP's data types and useful data structures as well as strong types consistency in the code of target projects.
License MIT
Informations about the package php-types
PHP Types 🐘
PHP Types is a small library/framework aimed to improve and encourage strong typing across your project's codebase, consisting of basic array-types, useful data structures and more, that are not natively available by the language itself.
Table of contents 📖
- Requirements
- Installation
- Usage
- 3.1 Primitive types
-
- 3.1.1 Extending PHP primitive types
-
- 3.1.2
ObjectType
- 3.1.2
- 3.2 Primitive arrays
-
- 3.2.2
BoolArray
- 3.2.2
-
- 3.2.3
IntArray
- 3.2.3
-
- 3.2.4
FloatArray
- 3.2.4
-
- 3.2.5
StringArray
- 3.2.5
-
- 3.2.6
ObjectArray
- 3.2.6
-
- 3.2.7 Extra primitives arrays
- 3.3 Standard types
-
- 3.3.1 Standard Ints
-
- 3.3.2
size_t
- 3.3.2
-
- 3.3.3 Standard Ints Arrays
- 3.4 Data structures
-
- 3.4.1
HashSet
- 3.4.1
-
- 3.4.2
IntHashSet
- 3.4.2
-
- 3.4.3
StringHashSet
- 3.4.3
- 3.5 Type errors
- 3.6
hasAny()
andisEmpty()
methods - 3.7 The
splice
method - 3.8 The
length
property - 3.9 Defining custom types
-
- 3.9.1 Defining custom arrays
-
- 3.9.2
typedef()
- 3.9.2
Requirements
- PHP 8.3.0 or major
- Composer (optional)
- Have an initted Composer project (optional)
Installation
Install PHP Types via Composer:
or:
You can always download the library as .zip file, decompress it, store it somewhere in your target project and include the autoload.php file from the library's project root:
or:
You can download the packed .phar version of the library:
Usage
Primitive types
Extending PHP primitive types
PHP Types adds some cool extra types that you can play around with:
ObjectType
Primitive arrays
BoolArray
IntArray
FloatArray
StringArray
ObjectArray
Extra primitives arrays
Standard types
Standard Ints
Similarly to C, you can make use of the standard types (as you would by including the
Note: keep in mind that these are only representative values and not actual low-level values where we're not saving space by storing specific bytes per integer.
size_t
We can also make use of the size_t
data type to store large numbers based on sizes, lengths, etc:
Standard Ints Arrays
Data structures
HashSet
IntHashSet
StringHashSet
Type errors
In case you attempt to assign a value with a type different than an array's type, a TypeError
is thrown:
hasAny()
and isEmpty()
methods
Arrays contain useful methods that help to determine whether if they're empty or contain at least 1 element in them:
The splice
method
You can make use of the splice
method from array
s the same way you'd use the array_splice
function
with normal array
s:
The splice
method returns a multiple
, consisting of the resultant array along with the extracted elements respectively.
The length
property
Similarly to JavaScript/JS, you can make use of the length
property to retrieve the count
of total elements from a specific array
:
Defining custom types
Defining custom arrays
To define custom arrays, you can create a new class
that extends PHPTypes\ArrayObject
and implements a typed constructor for such purpose, e.g:
typedef()
It is very common that we may need to define multiple arrays for multiple classes from our project/app, to do so, we can simplify the previous example with the usage of the typedef
function
, just like this:
Notice the usage of the @disregard
and @var
phpDoc tags to preserve the array
treatment per User
and recognize the acccess to all its properties and methods by our preferred text editor. We also conjunct the @var
type definition with the ArrayObject
type to recognize access to methods such as count
, hasAny
, isEmpty
, splice
, etc.
In case our User
class
resides inside a specific namespace
, we can specify the full path to the namespace
when calling typedef
to define the new array
inside the same namespace
:
It is also recommended and a good practice to define custom types in a separate and dedicated file that can be accessed across our app, such as types.php