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

Loïc Faugeron:
Short Identifier
Jun 22, 2018 @ 13:12:21

Loïc Faugeron has a post on his site covering the concept of short identifiers and covering some of the common types along with some of the downsides of the use of each.

Sometimes resources can be identified by one of their attributes (a name, title, slug) and sometimes they can't (no name, or confidential name). In the later case, an ID needs to be artificially crafted.

Two of the popular strategies is to use either an auto incremental one or a universally unique one, however when it comes to share them publicly, both strategies present some drawbacks

He lists several different types including:

  • Auto Incremental IDs
  • Short IDs
  • Hashes
  • Alphabetical representation

In the end, the method he recommends is hashing the ID value and using the first few characters (much the way you can refer to commit hashes in the Git version control tool).

tagged: short identifier list tutorial hash autoincrement shorten

Link: https://gnugat.github.io/2018/06/15/short-identifier.html

Vladimir Goncharov:
Computer vision and machine learning in PHP using the opencv library
Jun 22, 2018 @ 12:02:15

Vladimir Goncharov has a tutorial posted to his Medium.com site showing how to integrate machine learning and computer vision into a PHP application using the opencv library. The php-opencv library is a C-level implementation (via an extension) making it easer to use it directly from PHP.

Now, “Machine learning” is developing very fast, it has already written a lot of articles, including the ones on the medium, and almost every developer would like to start using it in their work tasks and home projects, but where to start and what to use is not always understandable. Most articles for beginners offer a bunch of literature, on the reading of which there is not enough life, “inexpensive” courses, etc.

[...] I was considering writing a php-opencv module by myself using SWIG and spent a lot of time on it, but I did not achieve anything. [...] Then I found the library of php-opencv on the github space, it is a module for php7, which makes calls to opencv methods.

He then starts in on the code making use of the library to read in images, perform face detection, facial recognition, and locating facial marks/landmarks. He also includes a section covering the use of neural networks to improve the quality of images, classify them and the use of Tensorflow models to detect certain kinds of objects. The post ends with a look at getting the requirements installed to use the tool and links to other examples.

tagged: computer vision machine learning opencv library tutorial extension

Link: https://medium.com/@morozovsk/computer-vision-and-machine-learning-in-php-using-the-opencv-library-3131fe9df94b

SitePoint PHP Blog:
PHP-level Performance Optimization with Blackfire
Jun 22, 2018 @ 11:49:40

On the SitePoint PHP blog they've posted a tutorial that continues their series covering the creation of a simple image gallery blog application. In this latest part of the series the author covers the use of the Blackfire.io service for performance testing.

Throughout the past few months, we’ve introduced Blackfire and ways in which it can be used to detect application performance bottlenecks. In this post, we’ll apply it to our freshly started project to try and find the low-points and low-hanging fruit which we can pick to improve our app’s performance.

If you’re using Homestead Improved (and you should be), Blackfire is already installed.

While it’s useful to be introduced to Blackfire before diving into this, applying the steps in this post won’t require any prior knowledge; we’ll start from zero.

The tutorial walks you through some of the basics of Blackfire and what kind of information it will report back. After a small change to the Homestead YAML configuration, they show how to profile the landing page and what the resulting graph looks like. It then goes through the code, finding each of the pain points and refactoring them to improve performance.

tagged: blackfireio series performance optimize tutorial

Link: https://www.sitepoint.com/php-level-performance-optimization-blackfire/

Freek van Der Herten:
Breaking Laravel's firstOrCreate using race conditions
Jun 22, 2018 @ 09:46:52

In this new post to his site Freek Van der Herten shares a time when he was working on a data import that ended in some unexpected results thanks to an interesting race condition.

Recently I was working on a client project where a data import was performed via queues. Each record was imported in its own queued job using multiple queue workers. After the data import was done we had more rows than expected in the database. In this blogpost I'd like to explain why that happened.

He starts by digging into the code that made use of the firstOrCreate method in Laravel's Eloquent handling to find if an entry had already been created for the given data. The method uses two queries, one to determine if the record exists and another to create it if not. The issue was with the fact that it was being handled in a queue meaning that the select could happen and return false while another process was creating the record. He even created a demo app to show it happening and includes screenshots showing the result. He recommends moving the process to a separate queue and having only one worker executing at a time. There's not a good code-based solution for it as it's more of an issue with the architecture than the application itself.

tagged: laravel eloquent firstorcreate tutorial race condition

Link: https://murze.be/breaking-laravels-firstorcreate-using-race-conditions

SitePoint PHP Blog:
Building an Image Gallery Blog with Symfony Flex (Parts 1 & 2)
Jun 21, 2018 @ 11:48:56

On the SitePoint PHP blog they've kicked off a series by Zoran Antolovic walking through the creation of a blog application using the latest from the Symfony project - Symfony Flex.

Our journey towards a stable, robust, high-performance web app will start with the simple but functional application — the so-called minimum viable product (MVP). We’ll populate the database with random content, do some benchmarks and improve performance incrementally. Every article in this series will be a checkpoint on our journey!

This article will cover the basics of setting up the project and organizing files for our Symfony Flex project. I’ll also show you some tips, tricks and helper scripts I’m using for speeding up the development.

So far they've posted the first two articles of the series:

  • Part one explaining some of the basics of Flex and getting the application set up (including some sample fixture data)
  • Part two showing how to populate the application with more realistic data, run some basic performance tests and create a first unit test

There's much more to come in the series including the creation of file (image) upload handling, user registration and login and the creation of image galleries from uploads.

tagged: image gallery blog tutorial symfonyflex symfony flex part1 part2

Link: https://www.sitepoint.com/building-image-gallery-blog-symfony-flex-setup/

Sameer Borate:
PHP Simple HTML DOM Parser Script
Jun 21, 2018 @ 09:26:38

Scraping content from other sites (while slightly controversial) can be a helpful way to pull information into your application without the overhead of manual interaction. In this new post to his site Sameer Borate shows how to use a DOM parser to extract data from a remote site.

In this post I have explained some elements to scrap data from external websites. Simple HTML DOM parser is a PHP 5+ class which is useful to manipulate HTML elements. This class can work with both valid HTML and HTML pages that do not pass W3C validation. You can find elements by ids, classes, tags and many more. You can also add, delete or alter DOM elements. The only one thing you should care about is memory leaks – but you can avoid memory leaks as explained later.

He starts by walking through some of the basics of creating a new instance of the class and loading the content (either as a string or as a file) to be parsed. He then give several examples of how to query the contents of the document and locate multiple or single elements (including the use of CSS-type selectors for fuzzy attribute matching). He finishes out the post showing how to access element attributes and append content back to the original HTML.

tagged: simpledom parser script tutorial introduction html dom

Link: https://www.codediesel.com/php/php-simple-html-dom-parser-script/

NetTuts.com:
Authentication and Authorization Using Auth0 in PHP
Jun 20, 2018 @ 11:16:08

The NetTuts.com site has posted a tutorial for those looking to simplify the authentication of users in their PHP applications. In this new article they show how to use the Auth0 service to offload some of the burden of user management and authentication flow.

In this article, we're going to explore the Auth0 service, which provides authentication and authorization as a service. Auth0 allows you to set up basic authentication and authorization features for your apps in the blink of an eye.

The article starts with a brief overview of what the Auth0 service offers and what particular ones will be involved in the tutorial. To help make it simpler to get up and running, they've created a base application. They show how to get this application installed and configured to use the secrets values from your Auth0 account. It then walks through the various scripts included in the sample application including a login, logout and code to perform the actual authentication flow process (including validating the access token).

tagged: authentication auth0 tutorial application authorization

Link: https://code.tutsplus.com/tutorials/authentication-and-authorization-using-auth0-in-php--cms-31134

Exakat Blog:
5 usages of static keyword in PHP
Jun 20, 2018 @ 10:28:21

On the Exakat blog there's a new post sharing five uses of the "static" keyword in PHP applications. This includes the less common static closures and static class names.

Static is a PHP keyword with many usages. It is almost universally used, though there are many variations of it. Let’s review all the five of them :
  • static method
  • static property
  • static closure
  • static variable
  • static as a classname

The post goes through each of the items in the list giving a brief explanation of how it's used and a code example showing it in action. The post finishes with some tips on evaluating your own code for the use of "static" and tips for each to make your code easier to wrangle and maintain in the future.

tagged: static keyword example tutorial method property closure variable classname

Link: https://www.exakat.io/5-usages-of-static-keyword-in-php/

Rob Allen:
Dependency Injection with OpenWhisk PHP
Jun 20, 2018 @ 09:55:13

Rob Allen has continued his series of posts covering the use of PHP on the OpenWhisk platform. In his latest tutorial he shows how to use dependency injection for use in "non-trivial PHP applications".

Any non-trivial PHP applications use various components to do its work, from PDO though to classes from Packagist. It’s fairly common in a standard PHP application to use Dependency Injection to configure and load these classes when necessary. How do we do this in a serverless environment such as OpenWhisk?

This question comes up because we do not have a single entry point into our application, instead we have one entry point per action. If we’re using Serverless to write an API, then we probably

He uses a common connection type - PDO to a database - to illustrate a method for injecting dependencies in a serverless application. In his example, he starts with a simple script that returns a set of "todo" items from the database (with the PDO connection code embedded in it). Using dependency injection is a better way to manage that PDO connection so he shows how to update it to use the Pimple DI container and inject it via the constructor to make it available to the rest of the application.

tagged: dependency injection serverless openwhisk tutorial pdo database

Link: https://akrabat.com/di-with-openwhisk-php/

Ben Sampson:
Creating a decent Laravel deploy script
Jun 15, 2018 @ 13:45:38

Ben Sampson has posted a quick guide on his site sharing a deployment script for Laravel applications he's found useful.

A good deploy script can save you time and speed up your application, and it only takes a few minutes to set one up.

I have a standard deploy script which I use for almost all of my projects, which I'm going to break down and share with you.

Note: This article assumes that you're using Laravel Forge. If you're not, many of the settings will be applicable, but the setup steps may be a bit different.

He starts with a brief description of what a deployment script is and goes through the functionality he has included in his:

  • switching the site to maintenance mode
  • clearing caches
  • clearing and optimizing routes
  • clearing and optimizing the configuration
  • clearing expired password reset tokens
  • compiling front-end assets

He puts it all together into a single file that's then included in the Forge deployment process. He doesn't provide other instruction as to how to use this script with another deployment method but it can potentially be used anywhere a bash script could.

tagged: laravel deployment script steps laravelforge tutorial

Link: https://sampo.co.uk/blog/creating-a-decent-laravel-deploy-script