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

ThePHP.cc:
Typed Arrays in PHP
Feb 16, 2016 @ 15: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

Dan Miller:
Comparing the PHP 7 and Hack Type Systems
Apr 29, 2015 @ 13:31:43

Dan Miller, a core platform engineer at Etsy, has a new post on his personal site sharing his results from a comparison of the variable typing systems between the Hack language (created by Facebook) and what's coming in PHP7.

One of the exciting things about PHP 7, aside from the incredible performance improvements, is the introduction of scalar type hintingHack. I wanted to find out if you could execute the same code in PHP 7 and Hack, and what the differences in execution might be. Here's what I found out.

He starts by describing his setup (the versions of PHP7 and HHVM he's using) and shares a few simple examples. He uses the same(ish) code in both and points out some of the differences in what happens when each is executed. He also points out some of the differences in the features between the two (such as Hack not allowing for default arguments with a value of null). He tries a few more complicated things too, like mixing strict and non-strict files, and the findings. He ends the post with some of his overall thoughts of his results and his excitement about what the future holds for PHP7 and the hinting it will provide.

tagged: compare php7 hack type systems variable statictypehints hinting hhvm

Link: http://www.dmiller.io/blog/2015/4/26/comparing-the-php7-and-hack-type-systems

Nikita Popov's Blog:
Scalar type hinting is harder than you think
Mar 07, 2012 @ 16:03:47

In this new post to his blog Nikita talks about scalar type hinting and why it's harder than most people think to accomplish.

One of the features originally planned for PHP 5.4 was scalar type hinting. But as you know, they weren’t included in the release. Recently the topic has come up again on the mailing list and there has been a hell lot of discussion about it. Yesterday ircmaxell published a blog post about his particular proposals. The reactions on reddit were mixed. On one hand it is clear that people do really want scalar type hints, on the other hand they didn't seem to like that particular proposal.

He gets into some of the details of some of the current proposals and their problems like the strict versus loosely-typed nature of PHP and type hinting that was included but not enforced. One he does like, however, is one based on casting - how the variable ends up being cast rather than the specific type it is when it comes into the function/method. This one still has its flaws, so he suggests another method - weak type hints but with stricter input validation (without casting). He also briefly mentions something called "box based type hinting" that would allow users to define their own hinting rules.

Don't worry - code examples (pseudo-code obviously) are included for each of these proposals to help you understand the differences.

tagged: type hinting static strict looselytyped proposal scalar

Link:

PHP in Action:
Type hints are more useful for scalars than objects
Sep 11, 2008 @ 15:08:33

On the PHP in Action blog, there's a new post looking at a recent library that was posted to support type hinting on scalars. They agree with his choice of subjects, noting that they see type hinting as much more useful on scalars than on objects.

I admit that these judgments are hard to make. I could be wrong, more or less. Type hints are probably useful when code becomes stable enough and at the boundaries between modules. But I still tend to avoid using them until I get an actual bug that might have been prevented by a type hint. Their usefulness is and has to be an empirical question. The purpose of using them has to be catching errors earlier, so if they don't have that effect, there's no point.

He lists three reasons why he had given up on type hinting before, one being the limited usefulness when it came to objects. Applying it to scalars is a different matter, though, and can prevent improper passing of array/scalars when the other is needed.

tagged: type hinting scalar object array string class library

Link:

DevShed:
Enforcing Object Types in PHP - Using the Type Hinting Feature in PHP 5
Mar 02, 2006 @ 12:42:19

DevShed has posted the last article in their "Enforcing Object Types in PHP" series today - "Using the Type Hinting Feature in PHP5".

[Returning] to the subject of this last tutorial, I'll introduce another method for enforcing object types in PHP 5: the "Type Hinting" feature. It can also be used in conjunction with the "instanceof" operator that you learned about before, in order to develop PHP applications that implement thorough routines for filtering unwanted objects. Generally speaking, when you finish reading this article, you should be armed with a few more methods for forcing object types in PHP, in this way expanding your overall knowledge of object-oriented programming.

They start with an introduction to type hinting and some basic examples of its use. From there, they build up a relevant example with the help of their (X)HTML widget class they've used throughout the series.

tagged: enforcing object types using type hinting php5 enforcing object types using type hinting php5

Link:

DevShed:
Enforcing Object Types in PHP - Using the Type Hinting Feature in PHP 5
Mar 02, 2006 @ 12:42:19

DevShed has posted the last article in their "Enforcing Object Types in PHP" series today - "Using the Type Hinting Feature in PHP5".

[Returning] to the subject of this last tutorial, I'll introduce another method for enforcing object types in PHP 5: the "Type Hinting" feature. It can also be used in conjunction with the "instanceof" operator that you learned about before, in order to develop PHP applications that implement thorough routines for filtering unwanted objects. Generally speaking, when you finish reading this article, you should be armed with a few more methods for forcing object types in PHP, in this way expanding your overall knowledge of object-oriented programming.

They start with an introduction to type hinting and some basic examples of its use. From there, they build up a relevant example with the help of their (X)HTML widget class they've used throughout the series.

tagged: enforcing object types using type hinting php5 enforcing object types using type hinting php5

Link:


Trending Topics: