PHPackages                             gryph/php-struct - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. gryph/php-struct

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

gryph/php-struct
================

Generic Struct class for PHP

038↓100%PHP

Since Mar 10Pushed 1y ago1 watchersCompare

[ Source](https://github.com/ccordine/php-struct)[ Packagist](https://packagist.org/packages/gryph/php-struct)[ RSS](/packages/gryph-php-struct/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (1)DependenciesVersions (1)Used By (0)

The `Gryph\PHPStruct` library offers a robust and flexible solution for managing structured data in PHP. By leveraging PHP's reflection capabilities, this library enables developers to define classes with strict attribute typing and validation, ensuring data integrity and consistency across applications.

**Key Features:**

- **Dynamic Attribute Assignment:** Upon instantiation, the `Struct` class assigns values from an input array to the corresponding class properties. This dynamic assignment facilitates the seamless integration of data into structured objects.
- **Type Enforcement:** The library enforces strict type checking for class properties. If a property is assigned a value that doesn't match its declared type, an exception is thrown, preventing potential data type mismatches.
- **Nullable Support:** Developers can designate properties as nullable using the `Nullable` attribute or by specifying a nullable type (e.g., `?string`). This feature provides flexibility in handling optional attributes without compromising on type safety.
- **Comprehensive Property Analysis:** The `keys` method utilizes reflection to analyze and retrieve all properties of a class, categorizing them into public, private, and protected arrays. This comprehensive analysis aids in better property management and introspection.
- **Data Serialization:** The library offers methods to serialize the structured data into various formats:

    - `json()`: Converts the object into a JSON string.
    - `data()`: Returns a `stdClass` object representation.
    - `array()`: Provides an associative array representation.
- **Factory Method:** The static `generate` method allows for the creation of new instances of a class, initializing them with the provided attributes. This factory pattern promotes cleaner and more maintainable code when generating objects.

**Usage Example:**

```
