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

SitePoint PHP Blog:
Can We Have Static Types in PHP without PHP 7 or HHVM?
Jul 26, 2016 @ 11:34:57

On the SitePoint PHP blog Younes Rafie asks the question "Can we have static types in PHP without PHP 7 or HHVM?" One of the main features introduced by both of these versions (or platforms) is the ability to type things strictly and enforce more correct data handling. Previously PHP has been a "lazy typing" language and would regularly shift the type of a variable depending on the immediate need. Obviously, this can lead to unpredictable behavior.

Now that PHP 7 has been out for a while with interesting features like error handling, null coalescing operator, scalar type declarations, etc., we often hear the people still stuck with PHP 5 saying it has a weak typing system, and that things quickly become unpredictable.

Even though this is partially true, PHP allows you to keep control of your application when you know what you’re doing.

They show how, through a series of examples, to add a bit of additional validation with exceptions to ensure the input is the correct type. However this can be a bit more time consuming and difficult to remember so the team at Box put together the augmented types extension that brings some of the static typing to PHP 5.x. They help you get it installed and working in your PHP installation and include an example of it in use with DocBlock-based type hints. The extension provides handling for the basic types as well as arrays, multiple arguments, default values and return types.

tagged: static types php7 hhvm extension augmented types tutorial

Link: https://www.sitepoint.com/can-we-have-static-types-in-php-without-php-7-or-hhvm/

SitePoint PHP Blog:
Can We Use Laravel to Build a Custom Google Drive UI?
Jul 25, 2016 @ 13:57:52

The SitePoint PHP blog has posted a new tutorial that asks the question "Can We Use Laravel to Build a Custom Google Drive UI??" In this case it's a simplified version of the current Google Drive functionality, but it's more of a proof-of-concept than anything.

In this tutorial, we’re going to build an app that talks to the Google Drive API. It will have file search, upload, download and delete features. If you want to follow along, you can clone the repo from Github.

They walk you through the full process of getting the application set up, including creating the project on the Google side and grabbing the API credentials for use in your code. They then switch back over to the code side and create a basic Laravel project and configure it with the Google API credentials you just created. Next up is the creation of all of the routes for the list, upload and delete handling in the Laravel app as well as the controllers/views to make them all work. They also include search functionality, letting you easily query the API for files with names matching a certain string.

tagged: laravel google drive ui tutorial api example

Link: https://www.sitepoint.com/is-laravel-good-enough-to-power-a-custom-google-drive-ui/

SitePoint PHP Blog:
Testing Your Tests? Who Watches the Watchmen?
Jul 21, 2016 @ 12:10:48

In a tutorial posted to the SitePoint PHP blog Claudio Ribeiro tries to answer the question of "who watches the watchmen" (your application's tests) to ensure they're functioning as expected and are correct. In this new tutorial he introduces the Humbug mutation testing tool and how it can be used to verify your own tests.

Regardless of whether you’re working for a big corporation, a startup, or just for yourself, unit testing is not only helpful, but often indispensable. We use unit tests to test our code, but what happens if our tests are wrong or incomplete? What can we use to test our tests? Who watches the watchmen?

[...] Mutation Testing ( or Mutant Analysis ) is a technique used to create and evaluate the quality of software tests. It consists of modifying the tests in very small ways. Each modified version is called a mutant and tests detect and reject mutants by causing the behavior of the original version to differ from the mutant. Mutations are bugs in our original code and analysis checks if our tests detect those bugs. In a nutshell, if a test still works after it’s mutated, it’s not a good test.

He starts by helping you get it installed (a quick composer require) and creating a simple "calculator" test to show it in use. He then creates the test for the class with some simple testing methods for the basic calculator functionality. He then configures the Humbug installation (via a JSON config file) and executes it on the current tests, sharing the resulting output. He goes through the results showing how to interpret them and points out places where the tests could be improved.

tagged: testing unittest humbug mutation variation example tutorial output

Link: https://www.sitepoint.com/testing-your-tests-who-watches-the-watchmen/

Alejandro Celaya:
Project Scalability with Zend Expressive
Jul 21, 2016 @ 09:46:10

In a new post to his site (also posted on the Zend Developer Zone) Alejandro Celaya looks at scalability with Zend Expressive, a lightweight framework from Zend, the creators of the Zend Framework.

I've been working with some different frameworks lately. One of them is Zend Expressive, and I've come to the conclusion that I don't need to choose between different frameworks; depending on the project, Expressive always fits my needs and scales from small projects to bigger applications.

He starts off by looking at the "microframework approach" that Zend Expressive takes, making it easier to get up and running for smaller applications. He points out that this setup is fine when the application is small, but what happens as it grows - it just wouldn't scale well and be manageable. He talks about the setup he uses for larger scale applications, moving the configuration to dynamic config files and making use of more complex dependency injection. He also talks some about modularity in applications, the "middleware paradigm" and how he set up controller-style dispatching (versus just the default closures method).

tagged: zendexpressive tutorial scalability framework microframework update functionality

Link: http://blog.alejandrocelaya.com/2016/07/21/project-scalability-with-zend-expressive/

Loïc Faugeron:
Mars Rover, Landing event
Jul 20, 2016 @ 09:33:05

Loïc Faugeron has posted the latest tutorial in his series about developing a "Mars Rover" script using various design patterns and principles. In this latest part of the series he looks at handling the "landing event" of the rover.

In this series we're going to build the software of a Mars Rover, according to the following specifications. [...] Previously we've created a navigation package, and in it a LandRover class that validates input parameters for our first use case. [...] We've then refactored it to extract coordinates and orientation in their own classes.

In this article we're going to create the actual landing logic, using Event Sourcing

He then starts in, creating the tests first because of TDD, on the LandRoverHandler to integrate a command architecture (using the Command Bus pattern). He then sets up the structure for the event sourcing and an basic event for the landing of the rover. This includes the handler for the event, the event itself and the storage method for capturing that the event happened. He fills in the generated classes with a bit of logic and a few updates to the test for the new structure.

tagged: mars rover tutorial series landing event sourcing orientation phpspec

Link: https://gnugat.github.io/2016/07/20/mars-rover-landing-event.html

Securing client-side public API access with OAuth 2 and Symfony
Jul 18, 2016 @ 12:30:26

On the Codevate.com blog there's a tutorial posted by Chris Lush showing you how to secure your client-side public API with OAuth 2 (based on the Symfony platform).

Say you’ll be developing a web application for a customer to create and manage restaurant bookings, exposing restaurant information (name, opening times, menu contents etc.) and booking creation as RESTful API endpoints, which are consumed by secure admin backend. You’ll need to authorise access to the API, but there is no end-user involved since the web app is its own resource owner, so the previous flow doesn’t apply.

[...] However, you also need to develop a booking widget that will be embedded in a company or restaurant’s website for visitors to use. In this case, the client-side is no longer trusted enough to share the OAuth client secret that’s required to authenticate with your API. [...] We encountered a similar use-case for a client project recently, and this blog post details the steps taken to address it.

He then shows how to integrate the FOSOAuthServerBundle bundle into your current Symfony-based application and the updates you'll need to make to your security.yml file. He includes the code needed to create a "client" and associating it with a company already in the customer list. Next is the creation of access tokens and linking them to the restaurants in their system (a unique identifier to use externally for the restaurant rather than an ID). He shows an example of handling the token requests and the code/config changes needed to set it up. Finally he talks about scoping API requests down to certain functionality and an example cURL call to the API to show the results of it all combined.

tagged: clientside api access security oauth2 symfony tutorial bundle

Link: https://www.codevate.com/blog/12-securing-client-side-public-api-access-with-oauth-2-and-symfony

Matt Allan:
Understanding Dependency Injection Containers
Jul 18, 2016 @ 11:54:54

In this recent post to his site Matt Allan introduces a concept that's become an integral part of most major PHP frameworks and applications recently: dependency injection containers.

If you are writing modern PHP, you will run across dependency injection a lot. Basically all dependency injection means is that if an object needs something, you pass it in. So if you have a class [...] you would pass in (inject) the object it needs (the dependency) instead of instantiating it in the class. Dependency injection makes your code more flexible and easier to test. If you want to learn more about dependency injection in general, check out this summary in the PHP The Right Way guide.

He then breaks down the main concept, the container, and how it is usually used to store instances of various objects and other functionality. He includes the code to create a simple container, allowing for closures to be set to "entries" values. He also shows how to update the simple container to allow for singleton handling, creating an object once and returning it over and over (useful in some cases).

tagged: dependency injection container tutorial introduction

Link: http://mattallan.org/2016/dependency-injection-containers/

How to Program With Yii2: Uploading Files
Jul 15, 2016 @ 12:15:45

On the TutsPlus.com site there's a new installment in their "How to Program With Yii2 Series" looking at implementing file uploads with some simple examples.

In this How to Program With Yii2 series, I'm guiding readers in use of the Yii2 Framework for PHP. In this tutorial, I'll guide you through the basics of uploading files and images in Yii2.

For these examples, we'll continue to imagine we're building a framework for posting simple status updates, e.g. our own mini-Twitter. The image above demonstrates writing a short update while uploading a picture I took of the Taj Mahal.

They start with a look at some of the file upload plugins that seemed like the best they found to use with Yii2: FileInput and the 2Amigos BlueImp File Uploader. They go with the first option for the rest of the tutorial, showing you how to get it installed (via Composer), updating your current database tables and changing the model to reflect these updates. Next they help you create the view with the image upload form and one to display the image result once the upload is successful (including the controller code needed).

tagged: yii2 framework series tutorial upload files plugin

Link: http://code.tutsplus.com/tutorials/how-to-program-with-yii2-uploading-files--cms-23511

Thijs Feryn:
What are Type Errors in PHP 7?
Jul 15, 2016 @ 09:58:19

In a new post to his site Thijs Feryn has shared an overview of the different types of type errors that can pop up in PHP 7. This includes both a text-based and video based versions depending on your preference.

PHP 7 has a concept called Type Errors. These errors are thrown on a type mismatch when interacting with functions. They can be caught just like exceptions. I created a video that explains the situation. This blog post goes into more detail and has some code examples.

The post gets into the details of the type errors including topics like:

  • new type hints that were introduced
  • how you can manually throw type errors
  • when PHP itself would throw the errors
  • type coercion

There's also a brief section about strict typing and how that changes what errors PHP might throw in your scripts.

tagged: type error php7 introduction overview video tutorial

Link: https://blog.feryn.eu/type-errors-php-7/

Implementing Smart Search with Laravel and Typeahead.js
Jul 14, 2016 @ 12:12:41

The Scotch.io blog has posted a tutorial combining Laravel and the Typeahead.js library to create a "smart search" in your application. Most of the tutorial centers around the Typeahead.js code though, so with a bit of tweaking it could sit on top of just about any framework out there.

In a big web application, like an online market or a social network, one of the most important parts of the app is the search functionality. Having a great search feature will help your users find the right content(users, products, articles) quickly and easily.

In this tutorial, we are going to learn, how to enhance your default search field in your site with real-time suggestions from the database. We will be using Laravel, jQuery and Typeahead (Bloodhound as the suggestion engine) to implement the smart search and Bootstrap for the styling. So, let's get started.

He starts off by introducing the Typeahead.js library and the Bloodhound suggestion engine. He shows the installation (either via Bower, standalone download or included from a CDN) and a simple template with a search form and other Javascript requirements included. The tutorial covers some of the options you can set, what kinds of datasets it can work with and templates to use for the results. Next up, he brings in a default Laravel project and a "search" endpoint that queries user data and returns a JSON dataset of the results. The two pieces are then finally tied together with a few updates to the Javascript already created.

tagged: smart search tutorial laravel typeaheadjs javascript bloodhound

Link: https://scotch.io/tutorials/implementing-smart-search-with-laravel-and-typeahead-js