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

Zend Developer Zone:
A new type of PHP, part 2: Scalar types
Sep 16, 2015 @ 09:09:26

The Zend Developer Zone has posted the second part of their series (from community member Larry Garfield) about scalar types in PHP 7, one of many features in this "coming soon" release. You can find part one of the series here.

In our last installment, we talked about the benefits of more robust variable typing in PHP 7, and specifically the new support for typed return values. That is already a big boon to the maintainability of our code, but PHP 7 goes a step further. So far, we’ve only talked about typing against classes and interfaces. We’ve been able to type against those (and arrays) for years. PHP 7, however, adds the ability to type against scalar values too, such as int, string, and float.

But wait. In PHP, most primitives are interchangeable. [...] Much the same as return types, scalar types offer greater clarity within the language as well as the ability to catch more bugs earlier. That, in turn, can help encourage more robust code in the first place, which benefits everybody.

He starts by looking at the four new types that have been added in PHP 7: int, float, string, and bool. He includes a code example showing each of them in use on class interfaces and functions. He steps through the code example, explaining how the return type checking is handled for each instance. He also talks about how return type hinting can also benefit static analysis tools, allowing them to potentially find bugs in return values easier than before. Finally he covers strict mode, the method for enforcing types in your code and preventing PHP from doing any "magic" type switching for you. He also includes a code example of this functionality and how, with it enabled, it would have caught an error in his example on a integer vs string input.

tagged: scalar type hints introduction php7 strict example

Link: http://devzone.zend.com/6622/a-new-type-of-php-part-2-scalar-types/

SitePoint PHP Blog:
Drupal 8 Custom Plugin Types
Sep 14, 2015 @ 11:08:06

The SitePoint PHP blog has posted an overview from Daniel Sipos covering custom plugin types in Drupal 8 and how you can combine them (custom forms) with node entities.

In this article series of two parts, we will use this system to build a feature that allows the use of custom forms together with node entities. After we’re done, we’ll be able to do the following: configure node bundles to use one of multiple form types to be displayed together with the node display and easily define new form types by extending from a sensible base class. [...] We will get started by creating our custom plugin type. T

He starts with the plugin manager, showing you how to create a custom ReusableFormsManager in the module to set up the manager and add it to the system. He then sets up the plugin interface the manager is expecting to find. This piece defines methods to get the name of the plugin and to build the form. He then creates a simple ReusableForm annotation class and builds out the plugin base. This base class includes a form builder object used to build and output the custom form. Finally he gets into building the form and its matching interface. It's a simple "Contact Us" kind of form that outputs three fields (first name, last name, email) and a "Submit" button.

tagged: drupal8 custom plugin type tutorial form contactus

Link: http://www.sitepoint.com/drupal-8-custom-plugin-types/

Alejandro Celaya:
Working with custom column types in Doctrine. Enums.
Jul 30, 2015 @ 08:37:45

Alejandro Celaya has a post to his site showing you how to work with custom types in Doctrine, more specifically with the "enum" type.

Doctrine is currently the most used ORM in PHP. It makes it very easy to work with databases in an object oriented way. It comes with a set of built-in column types that map database types with PHP types. For example, the datetime column type, persists the value of an entity column as a datetime in the database and handles it as a DateTime object when the entity is hydrated.

Type conversions work both ways, so column types take care of casting database to PHP types and vice versa. In this article I'm going to explain how to define custom column types so that we can persist our own objects into the database and hydrate them back.

He points out that, while PHP itself lacks the "enum" data type, you can simulate it with a library like this. He uses this library to create a custom Doctrine object type that mimic enums in the getting and setting of a value to one of a few options. In this case it's values representing the CRUD methods. He shows the code to link the Type back to the Action which then gives it understanding of what the valid enum values can be. He also points out another package that he published recently that takes some of the work out of creating the boilerplate code for the enum.

tagged: package action tutorial enum type doctrine custom library

Link: http://blog.alejandrocelaya.com/2015/07/28/working-with-custom-column-types-in-doctrine-enums/

SitePoint PHP Blog:
Record Retrieval and Pagination in Bolt CMS
Jun 02, 2015 @ 12:29:59

The SitePoint PHP blog has a new tutorial posted showing you how to set up pagination in the results provided by the Bolt CMS that includes handling to grab content from the database and display the results.

Bolt is a lightweight CMS built on Silex with Symfony components that’s fast, easy, and fun to develop with. My recent affinity for Bolt has turned it into my CMS of choice as I make a conscious effort to choose wisely and step away from bloated frameworks. Previously, I gave a very detailed insight into what it’s like developing with Bolt. Today, we’re going to break down a very popular task into steps in order to accomplish it with ease.

He starts with an installation of the Homestead Improved virtual machine and checks out a new copy of Bolt. He sets up a basic Bootstrap-based theme, including header and footer partial views. He then shows how to create "contenttypes" and fetch the current content records. He updates the Twig template to show the results and integrates the simple pagination. He then creates the single page version to view the content and "previous" and "next" links to accompany it.

tagged: bolt cms tutorial pagination content type management silex

Link: http://www.sitepoint.com/record-retrieval-pagination-bolt-cms/

Dan Miller:
Comparing the PHP 7 and Hack Type Systems
Apr 29, 2015 @ 08: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

PHP7: More strict! (but only if you want it to be)
Mar 18, 2015 @ 10:48:38

In this new article Er Galvao Abbott talks about the struggle (and finally, inclusion) of type hinting in PHP, more specifically coming in PHP7, and how strict they can be.

It wasn’t easy (we knew it wouldn’t be) and certainly wasn’t pretty (we sort of knew that as well), but it’s finally official: PHP7 will come with Scalar Type Hints (STH) and an optional “strict mode”. [...] This is basically a step towards a more strict way of coding in PHP. Will we see more steps in that direction in the future? We don’t know and we’re OK with that for now. What’s brilliant about the body of work represented by these RFCs is that by implementing their concepts and specially making the “strict mode” optional the choice of being more strict remains with the programmer.

He talks some about the background of the want and need for strict typing in PHP and mentions three RFCs that will influence the type hints and handling in PHP7:

He summarizes each RFC and what it contributes to the language. He ends the post by dispelling one thing about all of this new typing functionality - PHP will remain loosely typed, this new functionality is in a "strict mode" only used when specified.

tagged: php7 strict type hint mode rfc introduction feature

Link: https://medium.com/@galvao/php7-more-strict-but-only-if-you-want-it-to-be-78d6690f2090

Anthony Ferrara:
Scalar Types and PHP
Feb 12, 2015 @ 11:25:47

Anthony Ferrara has tossed his own hat into the ring around the debate that's been going about the RFC for scalar type hints in PHP. In his post he agrees with (most of) the suggestions made in the proposal around strict, weak and the "compromise" of mixed typing.

There's currently a proposal that's under vote to add Pascal Martin's excellent post about it. What I want to talk about is more of an opinion. Why I believe this is the correct approach to the problem.

He starts off talking about the "all strict" angle that some suggested as the proper approach then moves into the "weak argument" explaining the difference between the two. He shares a bit of history around the problems detecting subtle bugs caused by typing issues and how it is definitely a problem that needs solving. Finally, he talks about the mixed-typing compromise and provides some code samples showing a common bug that can happen with weak typing.

tagged: scalar type hint rfc opinion example weak compromise mixedtype

Link: http://blog.ircmaxell.com/2015/02/scalar-types-and-php.html

Pascal Martin:
In favor of RFC "Scalar Type Hints"
Feb 09, 2015 @ 09:40:18

Pascal Martin has a new post today sharing some of his thoughts around one of the currently proposed PHP RFCs for < href="http://blog.pascal-martin.fr/post/in-favor-of-rfc-scalar-type-hints.html">scalar type hinting. PHP has had type hints for custom objects and some things like arrays but this proposal would add in additional ones for things like "string", "int" and "float".

The Scalar Type Hints RFC for PHP 7 has first been initialized in December 2014. It went on with version 0.2 at the middle of January 2015, after changing several major ideas, and is now in version 0.3, integrating return types, as RFC Return Type Declarations has been accepted a few days ago. [...] I’ve been following this RFC (and the previous ones) with some interest, and, as I’ve taken some time to play with it a bit last week, building PHP from the sources of the corresponding Git branch, I’ll try summarizing here why I think it is interesting. Please note this is my personal opinion.

He starts with a look at what the proposal entails around these new scalar type hints and why he thinks they're a good idea. He looks at some of the things that PHP's current weak typing allows and how it has made the language very flexible as a result. He also shows how the proposal suggests the use of the "declare" function to define a strict typing constant to essentially turn on the checking only where needed. He provides a few code snippet example including object/method handling, setting a custom error handler and which of the calls work in which typing method. He finishes the post looking at the "per-file" idea of enabling the strict typing checks and some of his confusion around the point. He also talks about return types, the directives that are proposed to enable the feature and the current status of the RFC.

tagged: scalar type hint rfc summary proposal php7 opinion overview

Link: http://blog.pascal-martin.fr/post/in-favor-of-rfc-scalar-type-hints.html

Design Patterns: The Simple Factory Pattern
Jan 27, 2015 @ 11:53:20

NetTuts.com has posted the next part of their series focusing on design patterns (and more specifically implementing them in PHP). In this latest post they look at a simple version of the Factory design pattern.

When you think of a factory, what comes to mind? For me, it's a place where things are created - that is, it's a centralized placed where things are produced. Later, the delivery of said products are done by the factory based on an order. Let's say that you're requesting a car. A factory will create one based on the specifications of the work order and will then deliver it once it's complete. Just as their real world counterparts, a software factory (that is, software that implements the factory design pattern), is an object that is responsible for creating and delivering other objects based on incoming parameters.

They mention the three different versions of the factory pattern but focus in on the simplest one (hence the "simple" in the title). They continue on with the car example, showing how to use a simple factory (a "carFactory") to build an instance of the "Car" class based on different classes of car types. The object is constructed when a "build" method is called with the type.

tagged: designpattern simple factory car type example tutorial introduction

Link: http://code.tutsplus.com/tutorials/design-patterns-the-simple-factory-pattern--cms-22345

Rob Allen:
Registering Doctrine Type Mappings for standalone migrations
Nov 18, 2014 @ 10:50:47

In a previous post Rob Allen showed you how to use Doctrine migrations as a standalone tool in your applications. In this new post he takes that a step further and shows you how to use the type mapping functionality (allowing for more customized column handling).

Shortly after starting to use Doctrine Migrations as a standalone tool in my project, I came across this error message [about an unknown database type "bit"]. This means that I have a column in my database of type bit which is used for booleans in SQL Server, but confuses the MySQL platform as it's not a default mapping. To support this, you need to modify the database connection's Platform object to know about the new mapping. However, with the setup that I'm using, I didn't have access to the connection object that's automatically created in the Migrations AbstractCommand object. After poking around in the code for a bit, I discovered that the solution is to create the connection object myself and then attach it as a new helper to the ConsoleApplication object.

He includes the code you'll need to add to your "migrations.php" file to set up the mapping relating his "bit" type example back to a "boolean" type. While this specific example is for the "bit" mapping, it shows how any mapping type can be added in. Finally he adds the connection (the one he set the type on) to enable it to be included in the helper set collection.

tagged: register type migration doctrine database tutorial custom mapping

Link: http://akrabat.com/php/registering-doctrine-type-mappings-for-standalone-migrations/