You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# Introducing PHP Datatypes: A Strict and Safe Way to Handle Primitive Data Types
1
+
# PHP Datatypes: Strict, Safe, and Flexible Data Handling for PHP
2
2
3
3
[](https://packagist.org/packages/nejcc/php-datatypes)
[](https://sonarcloud.io/summary/new_code?id=Nejcc_php-datatypes)
20
14
21
-
I'm excited to share my latest PHP package, PHP Datatypes. This library introduces a flexible yet strict way of handling primitive data types like integers, floats, and strings in PHP. It emphasizes type safety and precision, supporting operations for signed and unsigned integers (Int8, UInt8, etc.) and various floating-point formats (Float32, Float64, etc.).
22
-
23
-
With PHP Datatypes, you get fine-grained control over the data you handle, ensuring your operations stay within valid ranges. It's perfect for anyone looking to avoid common pitfalls like overflows, division by zero, and unexpected type juggling in PHP.
15
+
---
16
+
17
+
## Overview
18
+
19
+
**PHP Datatypes** is a robust library that brings strict, safe, and expressive data type handling to PHP. It provides a comprehensive set of scalar and composite types, enabling you to:
20
+
- Enforce type safety and value ranges
21
+
- Prevent overflows, underflows, and type juggling bugs
22
+
- Serialize and deserialize data with confidence
23
+
- Improve code readability and maintainability
24
+
- Build scalable and secure applications with ease
25
+
- Integrate seamlessly with modern PHP frameworks and tools
26
+
- Leverage advanced features like custom types, validation rules, and serialization
27
+
- Ensure data integrity and consistency across your application
28
+
29
+
Whether you are building business-critical applications, APIs, or data processing pipelines, PHP Datatypes helps you write safer and more predictable PHP code.
30
+
31
+
### Key Benefits
32
+
-**Type Safety:** Eliminate runtime errors caused by unexpected data types
33
+
-**Precision:** Ensure accurate calculations with strict floating-point and integer handling
34
+
-**Range Safeguards:** Prevent overflows and underflows with explicit type boundaries
35
+
-**Readability:** Make your code self-documenting and easier to maintain
36
+
-**Performance:** Optimized for minimal runtime overhead
37
+
-**Extensibility:** Easily define your own types and validation rules
38
+
39
+
### Impact on Modern PHP Development
40
+
PHP Datatypes is designed to address the challenges of modern PHP development, where data integrity and type safety are paramount. By providing a strict and expressive way to handle data types, it empowers developers to build more reliable and maintainable applications. Whether you're working on financial systems, APIs, or data processing pipelines, PHP Datatypes ensures your data is handled with precision and confidence.
-**Composite Types:** Structs, arrays, unions, lists, dictionaries, and more
45
+
-**Type-safe Operations:** Arithmetic, validation, and conversion with built-in safeguards
46
+
-**Serialization:** Easy conversion to/from array, JSON, and XML
47
+
-**Laravel Integration:** Ready for use in modern PHP frameworks
48
+
-**Extensible:** Easily define your own types and validation rules
24
49
25
50
## Installation
26
51
27
-
You can install the package via composer:
52
+
Install via Composer:
28
53
29
54
```bash
30
55
composer require nejcc/php-datatypes
31
56
```
32
57
33
-
## Usage
34
-
35
-
Below are examples of how to use the basic integer and float classes in your project.
36
-
37
-
38
-
This approach has a few key benefits:
39
-
40
-
- Type Safety: By explicitly defining the data types like UInt8, you're eliminating the risk of invalid values sneaking into your application. For example, enforcing unsigned integers ensures that the value remains within valid ranges, offering a safeguard against unexpected data inputs.
41
-
42
-
43
-
- Precision: Especially with floating-point numbers, handling precision can be tricky in PHP due to how it manages floats natively. By offering precise types such as Float32 or Float64, we're giving developers the control they need to maintain consistency in calculations.
44
-
45
-
46
-
- Range Safeguards: By specifying exact ranges, you can prevent issues like overflows or underflows that often go unchecked in dynamic typing languages like PHP.
47
-
48
-
49
-
- Readability and Maintenance: Explicit data types improve code readability. When a developer reads your code, they instantly know what type of value is expected and the constraints around that value. This enhances long-term maintainability.
58
+
## Why Use PHP Datatypes?
59
+
-**Type Safety:** Prevent invalid values and unexpected type coercion
60
+
-**Precision:** Control floating-point and integer precision for critical calculations
61
+
-**Range Safeguards:** Avoid overflows and underflows with explicit type boundaries
62
+
-**Readability:** Make your code self-documenting and easier to maintain
50
63
51
-
### Laravel example
64
+
## Why Developers Love PHP Datatypes
65
+
-**Zero Runtime Overhead:** Optimized for performance with minimal overhead
66
+
-**Battle-Tested:** Used in production environments for critical applications
67
+
-**Community-Driven:** Actively maintained and supported by a growing community
68
+
-**Future-Proof:** Designed with modern PHP practices and future compatibility in mind
69
+
-**Must-Have for Enterprise:** Trusted by developers building scalable, secure, and maintainable applications
52
70
53
-
here's how it can be used in practice across different types, focusing on strict handling for both integers and floats:
71
+
## Usage Examples
54
72
73
+
### Laravel Example
55
74
```php
56
75
namespace App\Http\Controllers;
57
76
58
-
use Illuminate\Http\Request;use Nejcc\PhpDatatypes\Scalar\FloatingPoints\Float32;use Nejcc\PhpDatatypes\Scalar\Integers\Unsigned\UInt8;
77
+
use Illuminate\Http\Request;
78
+
use Nejcc\PhpDatatypes\Scalar\FloatingPoints\Float32;
79
+
use Nejcc\PhpDatatypes\Scalar\Integers\Unsigned\UInt8;
59
80
60
81
class TestController
61
82
{
@@ -66,28 +87,22 @@ class TestController
66
87
{
67
88
// Validating and assigning UInt8 (ensures non-negative user ID)
Here, we're not only safeguarding user IDs but also handling potentially complex floating-point operations, where precision is critical. This could be especially beneficial for applications in fields like finance or analytics where data integrity is paramount.
83
-
84
-
85
-
PHP examples
86
-
87
-
### Integers
88
100
101
+
### Scalar Types
102
+
#### Integers
89
103
```php
90
-
use Nejcc\PhpDatatypes\Scalar\Integers\Signed\Int8;use Nejcc\PhpDatatypes\Scalar\Integers\Unsigned\UInt8;
104
+
use Nejcc\PhpDatatypes\Scalar\Integers\Signed\Int8;
105
+
use Nejcc\PhpDatatypes\Scalar\Integers\Unsigned\UInt8;
91
106
92
107
$int8 = new Int8(-128); // Minimum value for Int8
93
108
echo $int8->getValue(); // -128
@@ -96,10 +111,10 @@ $uint8 = new UInt8(255); // Maximum value for UInt8
96
111
echo $uint8->getValue(); // 255
97
112
```
98
113
99
-
### Floats
100
-
114
+
#### Floats
101
115
```php
102
-
use Nejcc\PhpDatatypes\Scalar\FloatingPoints\Float32;use Nejcc\PhpDatatypes\Scalar\FloatingPoints\Float64;
116
+
use Nejcc\PhpDatatypes\Scalar\FloatingPoints\Float32;
117
+
use Nejcc\PhpDatatypes\Scalar\FloatingPoints\Float64;
103
118
104
119
$float32 = new Float32(3.14);
105
120
echo $float32->getValue(); // 3.14
@@ -108,8 +123,7 @@ $float64 = new Float64(1.7976931348623157e308); // Maximum value for Float64
use Nejcc\PhpDatatypes\Scalar\Integers\Signed\Int8;
115
129
@@ -118,31 +132,30 @@ $int2 = new Int8(30);
118
132
119
133
$result = $int1->add($int2); // Performs addition
120
134
echo $result->getValue(); // 80
121
-
122
135
```
123
136
124
-
#ROAD MAP
137
+
## Roadmap
125
138
126
139
```md
127
140
Data Types
128
141
│
129
142
├── Scalar Types
130
143
│ ├── Integer Types
131
144
│ │ ├── Signed Integers
132
-
│ │ │ ├── ✓ Int8
133
-
│ │ │ ├── ✓ Int16
134
-
│ │ │ ├── ✓ Int32
145
+
│ │ │ ├── ✓ Int8
146
+
│ │ │ ├── ✓ Int16
147
+
│ │ │ ├── ✓ Int32
135
148
│ │ │ ├── Int64
136
149
│ │ │ └── Int128
137
150
│ │ └── Unsigned Integers
138
-
│ │ ├── ✓ UInt8
139
-
│ │ ├── ✓ UInt16
140
-
│ │ ├── ✓ UInt32
151
+
│ │ ├── ✓ UInt8
152
+
│ │ ├── ✓ UInt16
153
+
│ │ ├── ✓ UInt32
141
154
│ │ ├── UInt64
142
155
│ │ └── UInt128
143
156
│ ├── Floating Point Types
144
-
│ │ ├── ✓ Float32
145
-
│ │ ├── ✓ Float64
157
+
│ │ ├── ✓ Float32
158
+
│ │ ├── ✓ Float64
146
159
│ │ ├── Double
147
160
│ │ └── Double Floating Point
148
161
│ ├── Boolean
@@ -180,32 +193,129 @@ Data Types
180
193
└── Channel
181
194
```
182
195
196
+
## Testing
183
197
184
-
### Testing
185
-
198
+
Run the test suite with:
186
199
```bash
187
200
composer test
188
201
```
189
202
190
-
###Changelog
203
+
## Changelog
191
204
192
-
Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.
205
+
Please see [CHANGELOG](CHANGELOG.md) for details on recent changes.
193
206
194
207
## Contributing
195
208
196
-
Please see [CONTRIBUTING](CONTRIBUTING.md) for details.
209
+
Contributions are welcome! Please see [CONTRIBUTING](CONTRIBUTING.md) for guidelines.
197
210
198
-
###Security
211
+
## Security
199
212
200
-
If you discover any securityrelated issues, please email [email protected] instead of using the issue tracker.
213
+
If you discover any security-related issues, please email [email protected] instead of using the issue tracker.
201
214
202
215
## Credits
203
-
-[Nejc Cotic](https://github.com/nejcc)
216
+
-[Nejc Cotic](https://github.com/nejcc)
204
217
205
218
## License
206
219
207
220
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
208
221
209
-
## PHP Package Boilerplate
222
+
## Real-Life Examples
223
+
224
+
### Financial Application
225
+
In a financial application, precision and type safety are critical. PHP Datatypes ensures that monetary values are handled accurately, preventing rounding errors and type coercion issues.
226
+
227
+
```php
228
+
use Nejcc\PhpDatatypes\Scalar\FloatingPoints\Float64;
When building APIs, data validation and type safety are essential. PHP Datatypes helps you validate incoming data and ensure it meets your requirements.
238
+
239
+
```php
240
+
use Nejcc\PhpDatatypes\Scalar\Integers\Unsigned\UInt8;
241
+
242
+
$userId = new UInt8($request->input('user_id'));
243
+
if ($userId->getValue() > 0) {
244
+
// Process valid user ID
245
+
} else {
246
+
// Handle invalid input
247
+
}
248
+
```
249
+
250
+
### Data Processing Pipeline
251
+
In data processing pipelines, ensuring data integrity is crucial. PHP Datatypes helps you maintain data consistency and prevent errors.
210
252
211
-
This package was generated using the [PHP Package Boilerplate](https://laravelpackageboilerplate.com) by [Beyond Code](http://beyondco.de/).
253
+
```php
254
+
use Nejcc\PhpDatatypes\Scalar\Integers\Signed\Int32;
255
+
256
+
$data = [1, 2, 3, 4, 5];
257
+
$sum = new Int32(0);
258
+
foreach ($data as $value) {
259
+
$sum = $sum->add(new Int32($value));
260
+
}
261
+
echo $sum->getValue(); // 15
262
+
```
263
+
264
+
## Advanced Usage
265
+
266
+
### Custom Types
267
+
PHP Datatypes allows you to define your own custom types, enabling you to encapsulate complex data structures and validation logic.
0 commit comments