Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

SitePoint PHP Blog:
Drupal 8 Entity Validation and Typed Data Explained
Mar 30, 2016 @ 12:18:28

The SitePoint PHP blog has posted another in its series looking at Drupal 8 functionality today. In this latest post author Daniel Sipos looks at entity validation and typed data and what functionality the project provides over what was offered in previous versions (built on top of the Symfony validation component).

Data validation is a very important part of any application. Drupal 7 has a great Form API that can handle complex validation of submitted data, which can then be turned into entities. However, form level validation is problematic. [...] With the introduction of subsystems such as the REST API, Drupal 8 needed something better to handle this problem. [...] In this article, and its followup, we will explore the Drupal 8 Entity Validation API, see how it works, and how it can be extended.

He starts by looking at typed data and the consistency it provides in working with the metadata on objects. He includes a few examples of defining a string data type with a maximum length. He then applies this to content entities and enforcing the constraints provided by the types. In the next part of this series he'll look at the validation itself and how it works with these types/constraints.

tagged: drupal8 validation entity typed data tutorial introduction

Link: http://www.sitepoint.com/drupal-8-entity-validation-and-typed-data-explained/

ThePHP.cc:
Typed Arrays in PHP
Feb 16, 2016 @ 09:36:29

On thePHP.cc site there's an article posted from Tim Bezhashvyly, a software engineer in Berlin, looking at typed arrays in PHP and how they (sort of) exist.

Even though Rasmus Lerdorf denies the fact, typed arrays exist in PHP. At least to some degree. This wonderful feature sneaked in as a side-effect of variadic functions that were added to the language in PHP 5.6.

[...] An array comprising the full argument list can be obtained using the func_get_args() function. [...] Regardless how many arguments have been passed to the function above, they are all accessible as elements of the ?$args array. This is nothing special really, until you realize that variadics can be augmented with type declarations.

Using these type declarations, you can, in essence create "typed arrays" where you know the end result will only be a set of objects of the defined type. The same goes for scalar type handling in PHP 7, making it so you can use things like "string" or "int" as your type enforcement. He does point out two issues in using this method however: the limit PHP places on the number of variadics in the function definition and that using functions to enforce types could result in a fatal error.

tagged: typed array hinting enforcement tutorial variadics

Link: https://thephp.cc/news/2016/02/typed-arrays-in-php

Wes Shell's Blog:
Quicky 1: PHP is Loosely Typed – What does that Mean?
Oct 09, 2009 @ 12:30:11

Wes Shell has posted a "PHP quicky" to his blog today looking at something at the core of the PHP language - its loosely-typed nature.

A loosely typed language such as PHP is a language that does not require you to declare a variable type when declaring a variable. What does that mean? In PHP when you declare a variable, you can with no need to worry about what kind of data will be stored in that variable.

He compares it to some of the other strictly-typed languages where the variable must be set up and cast to the type and the flexibility that having looser types can have over it. He also looks at some of the drawbacks (ex. can lead to bad code) and advantages (ex. highly dynamic, flexible applications) of this method.

tagged: loosely typed language opinion

Link:

David Coallier's Blog:
PHP's Strong typing (SPL_Types) and a bit of ranting about winter...
Nov 27, 2007 @ 08:42:00

David Coallier has posted a nice overview of what SPL Types are in PHP and a brief example of hos they can be used.

I got karma to SPL_Types and added a new SplFloat() object, a few warning typos and more "flexible" strict modes for different types.

SPL_Types are a way to make PHP strongly typed (no adapting variables) to help make a PHP application "more correct" by forcing the variable's type to stay the same. His example shows the creation of a few variables with these new methods and how they can be used, both correctly and incorrectly.

tagged: spltypes stringly typed variable splfloat spltypes stringly typed variable splfloat

Link:

David Coallier's Blog:
PHP's Strong typing (SPL_Types) and a bit of ranting about winter...
Nov 27, 2007 @ 08:42:00

David Coallier has posted a nice overview of what SPL Types are in PHP and a brief example of hos they can be used.

I got karma to SPL_Types and added a new SplFloat() object, a few warning typos and more "flexible" strict modes for different types.

SPL_Types are a way to make PHP strongly typed (no adapting variables) to help make a PHP application "more correct" by forcing the variable's type to stay the same. His example shows the creation of a few variables with these new methods and how they can be used, both correctly and incorrectly.

tagged: spltypes stringly typed variable splfloat spltypes stringly typed variable splfloat

Link: