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

SitePoint PHP Blog:
Getting to Know and Love Xdebug
Apr 20, 2017 @ 17:55:59

On the SitePoint PHP blog editor Bruno Skvorc has posted a tutorial introducing you to Xdebug, the powerful debugging tool for PHP applications.

It’s been 15 years since Xdebug first came out. We think this is the perfect opportunity to re-introduce it to the world, and explain how and why it does what it does. Xdebug is a PHP extension (meaning it needs to be compiled and installed into a PHP installation) which provides the developer with some features for debugging.

It starts off by explaining some of the functionality that Xdebug brings to your debugging practices and the features that can help make it flow a little easier. It talks about how it differs from some of the IDE debugging tools and services like Blackfire.io. Next up is the example putting it to use and what the resulting errors look like. The post then gets into the integration of Vagrant with PhpStorm, using the profiler and how to force the rendering in Laravel output (it normally overrides the exception output with its own formatting).

tagged: tutorial know love xdebug introduction php debug debugging

Link: https://www.sitepoint.com/getting-know-love-xdebug/

Laravel News:
An Introduction to Laravel Authorization Gates
Apr 20, 2017 @ 15:21:20

On the Laravel News site there's a new post introducing you to "authorization gates" in Laravel, a feature that allows you to ensure a user has the permissions to perform the action being requested.

Laravel Gate has an elegant mechanism to ensure users are authorized to perform actions on resources. Before version 5.1, developers used ACL packages such as Entrust or Sentinel along with middlewares for authorization.

The problem with this approach is the permissions you attach to users are just flags; they don’t encode the complex logic of the permission for some use cases. We have to write the actual access logic within controllers.

They mention some advantages to using Gate over other external packages (like Sentinel or Entrust) by being "opinionated" about its use and the decoupling of access logic from business logic. They then share an example in a basic Laravel application, protecting "posts" based on the user's current roles. Models, migrations the auth generation are all included. They then show how to define policies in the AuthServiceProvider for CRUD operations on the posts and how to enforce their checks in the Post controller execution flow.

tagged: laravel gates authorization introduction tutorial

Link: https://laravel-news.com/authorization-gates

Toptal.com:
PhalconPHP: A Solution for High-load RESTful APIs
Apr 11, 2017 @ 10:26:37

The Toptal.com blog has a tutorial posted from Andrew Belousoff today sharing what he sees as a solution for high-load RESTful APIs in your application: PhalconPHP.

Suppose you need to create a high-load project based on a PHP MVC framework. You would probably use caching wherever possible. Maybe you would build the project in a single file, or maybe even write your own MVC framework with minimal functionality, or rewrite some parts of another framework. While, yes, this works, it’s a little bit tricky, isn’t it? Fortunately, there is one more solution that makes most of these manipulations unnecessary (save for the cache, perhaps), and this solution is called the PhalconPHP framework.

He starts off the article with a brief introduction to the PhalconPHP framework and some of the recent (2016) benchmarks of its performance against both raw PHP and other smaller, lighter MVC frameworks. With that out of the way he starts in on the creation of a sample project, first pointing out the difference between the "micro" and "full-stack" versions. He chooses the "micro" option for his API and walks you through installation of the framework extension, the directory structure it requires and what the code for the front controller looks like. From there he works up the rest of the code:

  • configuration handling
  • working with the DI container
  • creating the RESTful routes/controllers
  • building models
  • developing some business logic to work with user data

The post ends with a look at performing some testing on the result and mentions the addition of logging and caching functionality. He also points out one of the main disadvantages around using PhalconPHP - that it's an extension and is harder to customize than a PHP-land framework could be.

tagged: phalconphp rest api tutorial introduction framework benchmark

Link: https://www.toptal.com/phalcon/phalcon-php-restful-apis

BugSnag:
Building maintainable PHP apps using Composer
Apr 03, 2017 @ 12:14:02

The BugSnag blog has a post by guest author Graham Campbell sharing some best practices when using Composer in your PHP applications. It's written mainly for those that haven't used Composer much yet and want to get started quickly and easily.

Composer has made big waves in the PHP community in recent years. Thanks to Composer’s creators, Jordi Boggiano and Nils Adermann, Composer has become the absolute backbone of PHP’s package infrastructure today.

In this blog post, we shall be introducing Composer, from the ground up. We will see what packages are, how they should be versioned, and how to install them into your application. Learn about Composer and never look back!

He starts out by defining what a package is in the world of Composer and how it differs from a "library". He then briefly touches on the early days of the tool before showing how to get it installed and creating your first "composer.json" configuration file. He then gets into one of the more tricky subjects when dealing with Composer and packages - versioning. Finally he covers a few of his suggested best practices when using Composer including defining your own package installation constraints and how the autoloading works to your benefit.

tagged: composer bestpractices introduction configuration package library tool

Link: https://blog.bugsnag.com/best-practices-using-composer/

Nicola Malizia:
Understanding Laravel’s Automatic Facades
Mar 27, 2017 @ 09:24:36

On his Unnikked site Nicola Malizia has written up a post sharing some insight into how Laravel's automatic facades work, a feature that was introduced in Laravel v5.4.

Another cool feature shipped with the release of Laravel 5.4 is the ability to use Facades on the fly. Automatic Facades with the official documentation is quite informative. So I assume you are familiar with them.

He then starts in on the creation of custom facades: defining the functionality (a class), creating the facade and updating the app config to load it in. He then covers what's happening behind the scenes when the "Facades" namespace is loaded and the facade is called (lots of code snippets included here).

My suggestion is to start using automatic facades whenever is possible, their impact is negligible. To see when to use facades in general a good read is available in the official documentation.
tagged: laravel automatic facade tutorial introduction

Link: https://unnikked.ga/understanding-laravels-automatic-facades-d623e4774e5f#.hi2vq6igs

Benjamin Eberlei:
Explicit Global State with Context Objects
Mar 24, 2017 @ 11:50:12

In a post to his site Benjamin Eberlei looks at global state in PHP using something called "context objects" and how they can be used as an alternative to true global state.

Global State is considered bad for maintainability of software. Side effects on global state can cause a very nasty class of bugs. Context objects are one flavour of global state. For example, I remember that Symfony1 had a particularly nasty context object that was a global singleton containing references to very many services of the framework.

As with every concept in programming, there are no absolute truths though and there are many use-cases where context objects make sense. This blog posts tries to explain my reasons for using context objects.

He starts by getting everyone on the same page by defining a context - the "circumstances in which something can be fully understood". He then moves into the world of context objects, talking about how they encapsulate the information other objects need to execute. They're essentially "container" objects that allow for more control that something like the normal PHP superglobals. From there he helps you define what kind of context objects you might need in your application and provides a real-world example from his own experience at Tideways.

tagged: global state context object tutorial introduction definition

Link: https://beberlei.de/2017/03/12/explicit_global_state_with_context_objects.html

SitePoint PHP Blog:
Parallel Programming with Pthreads in PHP – the Fundamentals
Mar 24, 2017 @ 10:40:07

The SitePoint PHP blog has posted a tutorial that introduces some of the fundamentals of parallel programming in PHP. In their examples they make use of the pthreads extension to help bring simpler parallel programming to the language (otherwise you'd have to do odd things with shell commands and foreground/background controls).

PHP developers seem to rarely utilise parallelism. The appeal of the simplicity of synchronous, single-threaded programming certainly is high, but sometimes the usage of a little concurrency can bring some worthwhile performance improvements.

In this article, we will be taking a look at how threading can be achieved in PHP with the pthreads extension. This will require a ZTS (Zend Thread Safety) version of PHP 7.x installed, along with the pthreads v3 installed.

Despite the article being about the use of pthreads, it starts out talking about when not to use it, possibly saving you some time in the long run. With that out of the way it then starts in on the handling of "on-off tasks" with an example of fetching the "title" value from Google.com. This is then enhanced showing how to use the "Threaded" base class to define other classes that can be used inside of threads. The article moves on covering other topics including:

  • recycling threads
  • pthreads and (im)mutability
  • synchronization of threads

Each item in the list comes with plenty of example code showing you how to create the classes that execute the threads and the output they should generate.

tagged: parallel programming fundamentals tutorial introduction pthreads extension

Link: https://www.sitepoint.com/parallel-programming-pthreads-php-fundamentals/

Leonid Mamchenkov:
Getting started with workflows in PHP
Mar 21, 2017 @ 12:12:24

Leonid Mamchenkov has put together a post for those out there trying to get started with workflows in PHP and offers some tips from his own experience.

For a large project at work, we need to integrate or develop a workflow engine. I worked a little bit with workflow engines in the past, but the subject is way to big and complex for me to claim any expertise in it.

So, I am looking at what’s available these days and what are our options. This post is a collection of initial links and thoughts, and it’s goal is mostly to document my research process and findings, and not to provide any answers or solutions yet.

He starts off by defining the requirements of the system he's looking for, both on the technology side and the functional side. He outlines his expected flow and then links to some other resources he found to help define common terminology and some of the standards he found. He found the BPMN v2.0 and tracked down several PHP packages that implement that workflow structure (all linked as well).

tagged: workflow gettingstarted introduction concepts packages

Link: http://mamchenkov.net/wordpress/2017/03/20/getting-started-with-workflows-in-php/

Nicola Malizia:
Understanding Laravel’s HighOrder Collections
Mar 14, 2017 @ 09:11:59

Nicola Malizia has written up a tutorial that helps to explain Laravel's HighOrder collection functionality, a feature that was added in Laravel 5.4.

A new version of Laravel is available from 24 January 2017 and, as usual, it comes with a lot of new features.

Among them, there is one that takes advantage of the dynamic nature of PHP. Some out of there will contempt this, but I find it awesome!

He talks briefly about what the normal Collection class provides and provides an example of creating a collection and using the "map" function to return an average. With the new functionality the methods can be called directly on the collection with a simplified format. With the example out of the way he then dives into the source code for the feature, showing how it defines the "proxy" methods allowed and uses the __get and __call magic methods to map the method calls back to a collection.

tagged: laravel highorder collection tutorial introduction code

Link: https://unnikked.ga/understanding-laravels-highorder-collections-ee4f65a3029e#.uo1gmhbgu

TutsPlus.com:
How to Program With Yii2: ActiveRecord
Mar 09, 2017 @ 12:07:53

In the latest tutorial in their "Programming with Yii2" series the TutsPlus.com site shows you how to work with the ActiveRecord functionality included with the framework.

In this Programming With Yii2 series, I'm guiding readers in use of the Yii2 Framework for PHP. In today's tutorial, I'll walk you through using Yii's object-relational mapping, known as ORM, for working with databases. It's called Active Record and is a key aspect of programming database applications efficiently in Yii.

Yii offers different ways to work with your database programmatically, such as direct queries and a query builder, but using Active Record offers a complete set of benefits for object-oriented database programming.

The article goes on from there and defines some of the basics around what Active Record is and how it works. It then starts on the code, showing how to create an ActiveRecord model and execute queries to:

  • locate single or multiple records
  • build queries
  • counting records
  • and accessing the records returned

They also talk about mass assignment, saving data via model instances, deleting records and creating relationships between the models.

tagged: yii2 framework series activerecord database tutorial introduction

Link: https://code.tutsplus.com/tutorials/how-to-program-with-yii2-active-record--cms-27434