Giter Club home page Giter Club logo

struct's Introduction

Struct type for PHP

Latest Version Build Status Crutinizer Coverage Status Total Downloads Software License

About structs

A struct type is a value type that is typically used to encapsulate small groups of related variables, such as the coordinates of a rectangle or the characteristics of an item in an inventory.

The following example shows a simple struct declaration:

use Odan\ValueType\Struct;

class Book extends Struct
{
    public $price;
    public $title;
    public $author;
}

Read more:

Installation

composer require odan/struct

Why ?

What would struct offer over typed properties with accessors to most people? A struct is more a "fixed" type, while PHP class properties are not fixed. Example for a "wrong" struct.

class Book
{
    public $price;
    public $title;
    public $author;
}
$book = new Book();
$book->price = 39;
$book->title = 'My book title';
$book->author = 'Me';

// Set a undefined property from "outside".
// This is possible by default in PHP, but not allowed for a struct.
// A struct would raise an Exception here, and this would be better
// because this property is not defined in the Book class.
$book->isbn = '1234567890';

Usage

Inheritance

use Odan\ValueType\Struct;

class User extends Struct
{
    public $username;
    public $email;
}

As trait

use Odan\ValueType\StructTrait;

class User
{
    use StructTrait;
    
    public $username;
    public $email;
}

Basic example

$user = new User();
$user->username = 'John';
$user->email = '[email protected]';

// Get undefined property
$value = $user->nada;   // -> Exception: Cannot get undefined property

// Set undefined property
$user->nada = 'test';  // -> Exception: Undefined property (nada)

Using a struct as parameter

At one point or the other we have all seen a constructor like below:

public function __construct($size, $cheese = true, $pepperoni = true, $tomato = false, $lettuce = true) { //... }

As you can see; the number of constructor parameters can quickly get out of hand and it might become difficult to understand the arrangement of parameters. Plus this parameter list could keep on growing if you would want to add more options in future.

The sane alternative is to use a struct.

use Odan\ValueType\Struct;

class PizzaSetting extends Struct
{
    public $size;
    public $cheese;
    public $pepperoni;
    public $tomato;
    public $lettuce;
}

class Pizza 
{
    public function __construct(PizzaSetting $settings) {
        // ...
    }
}

And then it can be used like this:

$settings = new PizzaSetting();
$settings->size = 14;
$settings->tomato = true;
$settings->cheese = true;

$pizza = new Pizza($settings);

Using a struct for database queries

You can query more strongly typed results like this:

$pdo = new PDO('sqlite::memory:');
$rows = $pdo->query('SELECT username, email FROM user')->fetchAll(PDO::FETCH_CLASS, User::class);
var_dump($rows); // array of User struct objects

struct's People

Contributors

odan avatar

Stargazers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.