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

QaFoo Blog:
Testing the Untestable
May 02, 2017 @ 12:32:55

On the QaFoo blog there's a new post sharing a method for testing the untestable - file upload handling in your application.

A long time ago I wrote a blog post about Testing file uploads with PHP where I have used a CGI PHP binary and the PHP Testing Framework (short PHPT), which is still used to test PHP itself and PHP extensions.

Since the whole topic appears to be still up-to-date, I would like to show a different approach how to test a fileupload in PHP in this post. This time we will use PHP's namespaces instead of a special PHP version to test code that utilizes internal functions like is_uploaded_file() or move_uploaded_file().

They update the previous method to use the namespacing built in to PHP to "trick" the test into using a method from a local namespace first. The provide the code they'll be testing and a unit test to try and evaluate its result. The tutorial then shows how to use the namespaces to define is_uploaded_file and move_uploaded_file functions that override the defaults. These are used instead of the base level PHP ones making it easier to test the results of the mocked functions rather than the originals.

tagged: testing untestable fileupload unittest namespace tutorial

Link: https://qafoo.com/blog/102_testing_the_untestable.html

Using Namespaces and Autoloading in WordPress Plugins, Part 4
Jan 19, 2017 @ 10:24:36

The TutsPlus.com site has posted the fourth part of their series covering the use of namespacing and autoloading in WordPress plugins. In this latest tutorial they take everything they've shared (and made) previously and put it all together into a cohesive whole plugin.

At this point, we've laid the foundation for our plugin, written the plugin, and defined and explored namespaces and autoloaders. All that's left is to apply what we've learned.

So in this tutorial, we're going to put all of the pieces together. Specifically, we're going to revisit the source code of our plugin, namespace all relevant classes, and write an autoloader so that we can remove all of our include statements.

He starts off talking about namespacing and how it relates to directory structure and the code you'll need for each of the plugin files for put them in the correct namespace. With just these in place, however, errors are thrown. This requires the setup of a custom autoloader and PHP's own spl_autoload_register handling. He includes the code for the autoloader, taking in the class name and splitting it up to locate the correct directory, making it easier to replace the loading of all plugin scripts.

tagged: namespacing tutorial series part4 wordpress plugin autoloading namespace

Link: https://code.tutsplus.com/tutorials/using-namespaces-and-autoloading-in-wordpress-plugins-4--cms-27342

Using Namespaces and Autoloading in WordPress Plugins, Part 3
Nov 15, 2016 @ 10:23:30

On the TutsPlus.com site they've continued their WordPress series showing you how to integrate class autoloading into your plugin development.

In this tutorial, we're going to take a break from writing code and look at what PHP namespaces and autoloaders are, how they work, and why they are beneficial. Then we'll prepare to wrap up this series by implementing them in code.

In the previous part of the series they built up the environment and some of the basic structure of the plugin (you'll need this to follow along with this new tutorial) and continue on, starting with the basics of namespacing and autoloading. They then move over and start applying this functionality to the plugin classes and what happens in the autoloader when they're referenced.

tagged: wordpress autoload namespace tutorial part3 series

Link: https://code.tutsplus.com/tutorials/using-namespaces-and-autoloading-in-wordpress-plugins-3--cms-27332

Using Namespaces and Autoloading in WordPress Plugins, Part 2
Nov 03, 2016 @ 11:55:25

The TutsPlus.com site has continued their series looking at namespace-based autoloading in WordPress applications with part two. In this latest article they build on the simple plugin from part one and enhancing it with more functionality and autoloaded classes.

In the previous tutorial, we began talking about namespaces and autoloading with PHP in the context of WordPress development. And although we never actually introduced either of those two topics, we did define them and begin laying the foundation for how we'll introduce them in an upcoming tutorial.

Before we do that, though, there's some functionality that we need to complete to round out our plugin. The goal is to finish the plugin and its functionality so that we have a basic, object-oriented plugin that's documented and works well with one caveat; it doesn't use namespaces or autoloading.

This, in turn, will give us the chance to see what a plugin looks like before and after introducing these topics.

They start off with a quick review of the setup and previous development work done on the plugin making it easier to load in Javascript templates in a dynamic way. The plugin is then ready to start helping with the plugin use. They add in a basic CSS file to the site's "assets" folder and enqueue it. They start updating the plugin code, adding in an assets interface, a CSS loader and some styling for the box shown on the edit post interface.

tagged: namespace autoload wordpress plugin introduction part2 series autoload css loader

Link: https://code.tutsplus.com/tutorials/using-namespaces-and-autoloading-in-wordpress-plugins-part-2--cms-27203

Simon Holywell:
Importing and aliasing PHP functions
Oct 24, 2016 @ 11:34:29

In this recent post to his site Simon Holywell continues his look at namespacing in PHP with a look at importing and aliasing specific functions, not the entire class.

As a follow on to my short post about namespaces and functions from a year ago I thought it would be worth covering importing a specific function and aliasing functions via namespace operators too. This has been possible since PHP 5.6, but there is a nice addition in PHP 7 I’ll cover towards the end.

He starts with a refresher example of pulling in a namespace and using a method with the "use" statement. Following this he shares an update that just imports the one method via a "use function" call rather than the entire class/namespace. He again refactors this into something more usable (the original method name is quite long) with an alias. He then ends the post with the PHP 7 only trick using the braces to define grouped namespace handling (however, this doesn't allow for function level aliasing).

tagged: import alias function namespace grouping php7 tutorial

Link: https://www.simonholywell.com/post/2016/10/importing-and-aliasing-php-functions/

Using Namespaces and Autoloading in WordPress Plugins, Part 1
Oct 21, 2016 @ 10:43:38

The TutsPlus.com site has posted a new tutorial for the WordPress developers out there showing you how to get started with namespacing and autoloading in your WordPress installation.

Namespaces and autoloading are not topics that are usually discussed when it comes to working with WordPress plugins. Some of this has to do with the community that's around it, some of this has to do with the versions of PHP that WordPress supports, and some of it simply has to do with the fact that not many people are talking about it. And that's okay, to an extent.

Neither namespaces nor autoloading are topics that you absolutely need to use to create plugins. They can, however, provide a better way to organize and structure your code as well as cut down on the number of require, require_once, include, or include_once statements that your plugins use.

The article then starts in by listing the things you'll need to have installed and working to follow along. It then talks about what they're going to help you build - a simple plugin that adds an "Inspirational quotes" widget to your post editor page. They walk you through the basic setup of the plugin, adding the box to the page and setting up the "questions.txt" file to pull the quotes from. Code is provided for each step including the creation of the "quote reader" class and the class to display the meta box.

tagged: namespace autoload wordpress plugin introduction part1 series quotes

Link: https://code.tutsplus.com/tutorials/using-namespaces-and-autoloading-in-wordpress-plugins-part-1--cms-27157

Matt Trask:
Looking at Ramsey UUID
Aug 24, 2016 @ 09:16:56

Matt Trask has put together a new post spotlighting a handy library that's widely used across the PHP ecosystem for generating UUIDs: ramsey/uuid.

Welcome to the first installment in my 2113918230981 part series, "Better know a Package!". Tonight's package: the famous/infamous Uuid package that that taught us all what Ramsey is in Scottish, Rhumsaa. Created to give PHP a library to generate Universal Unique Identifiers, this library has been a stallwort in the community. Ben Ramsey created it first under the Rhumsaa namesapce before moving it to the Ramsey namespace, saving us all from learning more Scottish then we needed to ever learn.

[...] A UUID, or Universally Unique Identifier, will generate a 128 bite unique key in different series based on the version you asked for. RFC-4122 dictates how Uuids should be generated, and recommends 4 types.

Matt then goes on to describe each of the different UUID types and provides some code examples as illustration:

  • Version 1: Time and MAC addressed based Uuid
  • Version 2: DCE-based
  • Version 3: UUIDs based on a namespace and then it is MD5 hashed
  • Version 4: Random generation (based on the output of random_bytes

He also includes examples of the UUIDs output by each method (not much difference there as the structure of the resulting UUID is all the same).

tagged: uuid ramsey library introduction types namespace random mac time tutorial

Link: http://matthewtrask.net/blog/Looking-At-Ramsey-Uuid/

Marc Morera:
Namespaces in Traits
Oct 16, 2015 @ 13:53:14

In this post to his site Marc Morera talks about traits, namespaces and how they fit together (or don't).

Some projects using PHP 5.4 are actually using Traits. If you don’t know yet what a trait is, there are some interesting links for you. [...] This post is about the usage of "use statements" in Traits.

He starts with an overall picture of what he's trying to accomplish in a contribution to an open source project (with a word of caution). He talks about how you can make traits and classes more "friendly" with a refactoring example of his initial code snippet. In the end, though, he recommends basically avoiding namespaces if possible in traits, reducing headaches that could be caused either by conflicts or missing dependencies.

tagged: traits namespace difficulty dependency interaction

Link: http://mmoreram.com/blog/2015/10/16/namespaces-in-traits/

Acquia Blog:
Quick Tips for Writing Object Oriented Code in PHP
Jul 13, 2015 @ 10:58:14

On the Acquia blog Adam Weingarten has shared some tips for writing good (and modern) object-oriented code in PHP:

Recently I began working on a D8 module, but this isn't a story about a D8 module. The work I did provided me an opportunity to get back to my pre-Drupal object oriented (OO) roots. Writing OO code in PHP presented some curve balls I wasn’t prepared for. Here are some of the issues I encountered:

His tips touch on things like:

  • Using a code structure that can be autoloaded via PSR-4
  • Namespacing your classes
  • Working with types and type hinting
  • Using docblock comments for autocomplete in IDEs

There's also a few other quick topics he finishes the post out with: the lack of enums in PHP, working with associative arrays, no functional overloading and assigning responsibility to classes.

tagged: oop tips objectoriented code modern psr4 namespace typing docblock missing

Link: https://www.acquia.com/blog/quick-tips-for-writing-object-oriented-code-in-php/09/07/2015/3285651

Paul Jones:
Modernizing Serialized PHP Objects with class_alias()
Jul 01, 2015 @ 09:57:50

Paul Jones has posted an article to his site with another helpful hint to modernize your legacy PHP application. In the post he looks at updating serialized object handling with the help of the class_alias function.

Several weeks ago, a correspondent presented a legacy situation that I’ve never had to deal with. He was working his way through Modernizing Legacy Applications in PHP, and realized the codebase was storing serialized PHP objects in a database. He couldn’t refactor the class names without seriously breaking the application. [...] Before I was able to reply, my correspondent ended up changing the serialization strategy to use JSON, which was a rather large change. It ended up well, but it turns out there is a less intrusive solution: class_alias().

He talks about how this function could be useful to prevent the need for updating the class name in every serialized instance by setting up an alias to the new name. You can even use namespacing in the alias that will let the autoloader work with the PSR-0/PSR-4 handling to correctly load the class. With this in place, you can then refactor to the new version of the class without worry of breakage.

tagged: modernize serialized object classalias namespace psr0 psr4

Link: http://paul-m-jones.com/archives/6158