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
Below are examples of how to use the basic integer and float classes in your project.
36
36
37
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.
50
+
38
51
### Laravel example
39
52
53
+
here's how it can be used in practice across different types, focusing on strict handling for both integers and floats:
40
54
```php
41
-
<?php
42
-
43
55
namespace App\Http\Controllers;
44
56
45
57
use Illuminate\Http\Request;
46
58
use Nejcc\PhpDatatypes\Integers\Unsigned\UInt8;
59
+
use Nejcc\PhpDatatypes\Floats\Float32;
47
60
48
61
class TestController
49
62
{
50
63
public UInt8 $user_id;
64
+
public Float32 $account_balance;
65
+
51
66
public function __invoke(Request $request)
52
67
{
68
+
// 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.
0 commit comments