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

SitePoint PHP Blog:
Quick Tip: Convenience Hacks for Passing Data to Views
Aug 16, 2016 @ 11:09:38

On the SitePoint PHP Blog Reza Lavaryan has shared a "quick tip" about making it easier to pass data out to the views in your MVC application. It relates more specifically to when you have a lot of values to pass out rather than just a few bits of data.

In MVC based architectures, working with template engines is an inevitable part of the development routine. It usually goes like this: we prepare and pass the data to the view. In the view, we print them based on our layout design.

[...] There are times, however, when the number of variables might be much higher than this: ten or more. In that case, we’ll have a tall list of variables (as an associative array), being passed to the respective template. It gets messy and unreadable quickly. If only there was a way to just list what we need by name, and have PHP take care of the rest for us. Well… there is!

The example shows how to use the compact function built into PHP to grab values from the current scope and return them as an array. Unfortunately it does loose the array keys with this method, so they propose an alternative with the get_defined_vars function and some simple key handling to return a more correct version of the array.

tagged: quicktip hack data view compact getdefinedvars tutorial

Link: https://www.sitepoint.com/quick-tip-convenience-hacks-for-passing-data-to-views/

SitePoint PHP Blog:
A Pokemon Crash Course on CouchDB
Aug 12, 2016 @ 10:02:56

The SitePoint PHP blog has a new tutorial posted giving you a "Pokemon Crash Course" on CouchDB, the popular NoSQL database. The "Pokemon" part comes in related to the data the tutorial uses to show you common operations and the use of a PHP interface to perform them.

In this tutorial, we’ll walk through working with CouchDB, a NoSQL database from Apache. This tutorial will focus more on the practical side, so we won’t cover what CouchDB is good for, how to install it, why use it, etc. We’ll focus on how to perform database operations through CouchDB’s HTTP API and how to work with it in PHP, laying the foundation for future, more complex posts.

The article is then broken up into different sections by operation, starting with the use of the CouchDB database via a console then via PHP:

  • Creating a Database
  • Talking to the HTTP API
  • Creating New Documents
  • Bulk Insert
  • Retrieving Documents
  • Updating Documents
  • Working with PHP

Each section includes code snippets and (where relevant) screenshots of the results to help you ensure you're on the right track.

tagged: tutorial couchdb pokemon data introduction crud library example

Link: https://www.sitepoint.com/a-pokemon-crash-course-on-couchdb/

Jordi Boggiano:
Common files in PHP packages
Apr 21, 2016 @ 09:29:15

Jordi Boggiano has a new post to his site today sharing some interesting PHP package statistics he gathered as a part of the metadata in the Composer/Packagist ecosystem.

This one started in a peculiar way. Paul M. Jones announced a new version of his Producer tool, I had a look at it and saw that it recommended having a changelog called CHANGES.md by default. [...] My first thought was to report an issue asking to change the default, but then I thought it's Paul, he will not just take my word for it, he will want hard facts. So here I am two days later. I queried GitHub's API for the file listing (only the root directory) of all PHP packages listed on packagist.org. What this let me do is look at what files are commonly present (and not), which is quite interesting to get a picture of the whole ecosystem.

He queried about 79,000 packages and found some interesting patterns in the results. These included findings like:

  • 8% have a DependencyInjection/ directory, which I believe indicates Symfony bundles
  • 3.6% have a examples/ and 3.5% a docs/ directory
  • 49% have some file or directory indicating the presence of tests (phpunit.xml & co)
  • 14% have committed their composer.lock
  • 8% show a presence of some code quality/style CI (scrutinizer, codeclimate, styleci)

There's some other interesting statistics in the post around license files, changelogs and CLI binaries too. He's also posted the full data set for anyone interested in running some of their own statistics on the results.

tagged: package statistics packagist composer data results summary

Link: https://seld.be/notes/common-files-in-php-packages

Adam Wathan:
Cleaning Up Form Input with Transpose
Apr 15, 2016 @ 11:50:34

Adam Wathan has a post on his site showing you how to use "transpose" functionality to clean up form input, transforming a set of arrays from submitted data back into a better structure.

Transpose is an often overlooked list operation that I first noticed in Ruby. The goal of transpose is to rotate a multidimensional array, turning the rows into columns and the columns into rows.

In his example, he adding multiple "contacts" at once. He shows the difficulties with this, the form structure and the data returned. There's several formats that could be returned but none are the correct structure to push into a model (his examples are in Laravel there's a generic array_map example too) He shows how to update the Laravel collection handling using a custom macro, transpose, to handle the array_map functionality in a more self-contained way. He includes the code to make the macro work and an example of it in use to correctly format his incoming contact data into something he can use in his collection.

tagged: transpose laravel collection array data format arraymap

Link: http://adamwathan.me/2016/04/06/cleaning-up-form-input-with-transpose/

Jeff Madsen:
Using Faker to seed dummy data for Laravel application
Apr 13, 2016 @ 12:07:18

Jeff Madsen has a quick post to his site showing how to use Faker to populate data in Laravel directly in the framework's generated "seeders".

Hello, Mr. asdfgh qweefg! Welcome to kgjhjgjh! Thank you. Only the name's not "asdfgh qweefg".

Sorry! It's just easier.. You're always showing everyone on Laravel Quick Tips these helpful little tricks. Why not show them how to give me a proper name?

He includes an example of creating a Faker object in the seeder and using it to make name, email, age and city values. Naturally, not everyone's going to have English names or locations, so he shows how to set the language, locale and a few other tips:

  • shortcuts for optional data
  • randomized email addresses
  • random elements from a given set

He also mentions custom providers but points to the Faker documentation for a bit more information about that.

tagged: faker laravel seed seeder dummy data tutorial examples

Link: http://codebyjeff.com/blog/2016/04/hello-mr-asdfgh-qweefg-welcome-to-kgjhjgjh

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/

SitePoint PHP Blog:
Your First Drupal 8 Migration
Mar 10, 2016 @ 12:20:06

In this tutorial on SitePoint.com author Daniel Sipos introduces you to the concepts behind the Migrate module in Drupal 8 and how it can be used to move content over from other sources.

Migrate is one of the most established modules in the Drupal ecosystem. So much so that with Drupal 8, a decision has been made to get some of its functionality ported and added to Drupal core. An important reason was that the traditional upgrade between major releases was replaced with a migration of Drupal 6 or 7 content and configuration to Drupal 8.

[...] In this article we are going to look at how migration works in Drupal 8 by migrating some content into node entities. For simplicity, the data we play with resides in tables in the same database as our Drupal installation.

He starts but outlining the "migration theory" and parts of the process to get the data in to your Drupal 8 instance: the source, the process and the destination. He uses a movie content example to show the setup needed to create the tables and define the configuration for the relationship to genres. He then shows how to make the migration configuration, defining the three parts and how to define the related migration classes. He ends the post with the drush command to execute the migration, get the current migration status and roll them back if something unexpected happens.

tagged: drupal8 migration data import source process destination

Link: http://www.sitepoint.com/your-first-drupal-8-migration/

Rudi Theunissen:
Efficient data structures for PHP 7
Feb 09, 2016 @ 10:49:37

In this recent article over on Medium.com Rudi Theunissen looks at more efficient data structures for PHP 7 and the introduction of a library that wants to help replace some of the current functionality and dependencies on things like arrays.

PHP has one data structure to rule them all. The array is a complex, flexible, master-of-none, hybrid data structure, combining the behaviour of a list and a linked map. But we use it for everything, because PHP is pragmatic.

[...] The recent release of PHP 7 caused a lot of excitement in the PHP community. We couldn't wait to start using the new features and get a taste of the ~2x performance boost. One of the reasons why it runs that much faster is because the array was redesigned. But it’s still the same structure, “optimised for everything; optimised for nothing” with room for improvement.

He starts with a brief mention of some of the current topics around data structures - the SPL functionality, why fixing them would be difficult and, finally, introducing the library that seeks to supersede them. The remainder of the post goes through the pieces of functionality the library offers and explains how each works including videos and graphs of the performance results:

  • Collection
  • Sequence
  • Vector
  • Deque
  • Stack
  • Queue and PriorityQueue
  • Hashable
  • Map
  • Set

He also answers some of the most common questions he's gotten about the library including the level of testing, documentation for the project and why he structured certain elements and features how he did.

tagged: data structure php7 library ds spl optimized video code

Link: https://medium.com/@rtheunissen/efficient-data-structures-for-php-7-9dda7af674cd#.xg74fpxwg

Data Encoding: A Guide to UTF-8 for PHP and MySQL
Jan 28, 2016 @ 13:22:56

The Toptal.com blog has posted a guide to data encoding in PHP and MySQL looking specifically at the use of UTF-8 and related handling. They talk about some of the updates you'll need to make to configurations, code and the MySQL settings to fully support this character set.

As a MySQL or PHP developer, once you step beyond the comfortable confines of English-only character sets, you quickly find yourself entangled in the wonderfully wacky world of UTF-8.

[...] Indeed, navigating through UTF-8 related data encoding issues can be a frustrating and hair-pulling experience. This post provides a concise cookbook for addressing these issues when working with PHP and MySQL in particular, based on practical experience and lessons learned (and with thanks, in part, to information discovered here and here along the way).

They start with the changes on the PHP side, updating the INI settings to make UTF-8 the default character set and which functions you'll need to update and replace. With those changes out of the way they move to the MySQL side, changing up settings in the my.cnf file and a few other things to consider on the database side (including that the MySQL support for UTF-8 is only a partial character set).

tagged: toptal data encoding mysql utf8 update configuration code

Link: http://www.toptal.com/php/a-utf-8-primer-for-php-and-mysql

Lorna Mitchell:
Insert Data with Phinx
Dec 18, 2015 @ 10:49:07

Lorna Mitchell has a new post to her site with a quick tip for any of the Phinx users out there around a new feature they've added: database seeding.

Database patching is a wicked hard problem, one that's got a bit easier in my world lately as I've been using Phinx on a few projects. [...] One thing I didn't immediately find was how to insert data. Phinx has seed functionality but in this case I needed to put in a lookup table to go along with a data structure change.

She includes a code examples of this new feature, showing how to create a roles table and seed it with a new record with a "name" value of "admin". She also mentions one "gotcha" in the name of the function used to save the data to the new table (saveData versus just save).

tagged: phinx insert data database migration example tutorial

Link: http://www.lornajane.net/posts/2015/insert-data-with-phinx