DownloadStruct type for PHP
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:
* https://msdn.microsoft.com/en-us/library/ah19swz4.aspx
* https://qafoo.com/blog/096_refactoring_extract_data_objects.html
* https://www.reddit.com/r/PHP/comments/5tui33/refactoring_extracting_data_objects/
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 = 'john@example.com';
// 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
|