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

Mark Baker:
Anonymous Class Factory – The Results are in
May 13, 2016 @ 12:15:17

Following up on his previous post about anonymous classes and a factory to generate them, Mark Baker has posted about the results of some additional research he's done on the topic and four options he's come up with.

A week or so ago, I published an article entitled “In Search of an Anonymous Class Factory” about my efforts at writing a “factory” for PHP7’s new Anonymous Classes (extending a named concrete base class, and assigning Traits to it dynamically); and about how I subsequently discovered the expensive memory demands of my original factory code, and then rewrote it using a different and (hopefully) more memory-efficient approach.

Since then, I’ve run some tests for memory usage and timings to assess just how inefficient my first attempt at the factory code was, and whether the new version of the factory really was better than the original.

His four options that finally worked somewhat as he'd wanted were:

  • A factory that returns an instance of a concrete class using the traits he wants
  • A factory that returns an anonymous class extending a concrete class that uses the traits
  • His original Anonymous Class factory and extending the result with the traits
  • His second version of the Anonymous Class factory that creates the instance, caches it and returns a clone

He also includes the code he used to run the tests of each factory method and shares some of the resulting benchmarks (with a few surprises).

tagged: anonymous class factory results options benchmark

Link: https://markbakeruk.net/2016/05/12/anonymous-class-factory-the-results-are-in/

Ibuildings Blog:
Working with Entities in Drupal 7
May 05, 2016 @ 12:29:26

On the Ibuildings site there's a tutorial posted talking about working with entities in Drupal 7 and how creating your own classes for them can make them easier to manage.

Developers love object-oriented code. But how can this be achieved with Drupal 7 entities? By default Drupal uses a single class for all entities of a given type. For example, all node objects are standard classes (stdClass) and all entity objects have the Entity type. I personally like to have an entity model that only exposes the functionality that is applicable for the logic of your domain.

[...] Wouldn’t it be nice to create your own classes for entities?

First off, he starts with a refresher on what entities are and how they relate to the database schema. He points out the difficulties in using them and testing their types. He then provides his suggested solution for "all" of your entity problems - the creation of classes for the different entity types. He gives an example using an Article type and how to create/use them in your Drupal code.

tagged: drupal7 entities custom class stdclass tutorial

Link: https://www.ibuildings.nl/blog/2016/04/working-entities-drupal-7

Mark Baker:
In Search of an Anonymous Class Factory
May 03, 2016 @ 10:49:25

In a new post to his site Mark Baker take a look at anonymous classes, a new feature in PHP 7, and a challenge he took on to figure out how to apply traits to them at runtime.

One of the more interesting new features introduced to PHP with the arrival of version 7 is Anonymous Classes. [...] Then back in January (as I was waiting for my flight to the continent for PHPBenelux) I was intrigued by a request to find a way of dynamically applying Traits to a class at run-time. With time on my hands as I was sitting in the airport, I considered the problem.

His first idea was to build an anonymous class, extending the requested class that would come along with the traits/properties/functionality of the original class. He includes some of the code he tried to implement this solution and ultimately figured out that a factory would be a good approach to creating the structure. After doing some research he found a way to create the factory using some eval magic. However, this wasn't "the end of the story" as he found out some other interesting things about anonymous classes (such as the fact that they're linked to only one instance of a class, making them less reusable).

tagged: anonymous class php7 factory eval example

Link: https://markbakeruk.net/2016/05/03/in-search-of-an-anonymous-class-factory/

Evert Pot:
Drop 'public' not 'var'!
Mar 28, 2016 @ 12:23:32

In a recent RFC that's been proposed and is now up for voting, the suggestion has been made to drop the var keyword in PHP 7.1 and completely remove it in PHP 8 (made a bit redundant buy the public keyword in classes). Evert Pot, however, disagrees and suggests dropping public instead.

A PHP RFC vote has started to deprecate the var keyword in PHP 7.1 and remove it in PHP 8. At the time of writing, there 23 who say it should be removed, and 18 who say it should not. [...] I’d like to offer a different opinion: I think people should be using var instead of public. I realize that this is as controversial as tabs vs. spaces (as in: it doesn’t really matter but conjures heated discussions), but hear me out!

He goes through an example on one of his own projects, showing how he's mostly removed the public level of exposure from his development (using final and statics instead). He then suggests three common thoughts he sees people having being in favor of dropping var versus public:

  • #1: Everyone doing the same thing is good
  • #2: It’s ugly!
  • #3: The public keyword is useful to convey intent

He also points to one place where he does see the need for a public but also suggests that in that case var would do juts fine too.

tagged: public var class exposure level rfc proposal voting

Link: https://evertpot.com/drop-public-not-var/

SitePoint PHP Blog:
Control User Access to Classes and Methods with Rauth
Mar 17, 2016 @ 13:55:22

The SitePoint PHP blog has posted a tutorial from Bruno Skvorc showing you how to use Rauth, a tool that's designed to control access to parts of your application as set by annotations in the code.

Rauth is SitePoint’s access control package for either granting or restricting access to certain classes or methods, mainly by means of annotations.

[...] Traditional access control layers (ACLs) only control routes – you set anything starting with /admin to be only accessible by admins, and so on. This is fine for most cases, but not when: you want to control access on the command line (no routes there) or you want your access layer unchanged even if you change the routes Rauth was developed to address this need. Naturally, it’ll also work really well alongside any other kind of ACL if its features are insufficient.

He starts by dispelling the common thought (at least in most of the PHP community) that annotations are a bad thing and relying on them for functionality isn't a good practice to follow. With that out of the way, he shows a simple example: a set of users and fake routes that are evaluated by Rauth based on the annotations in a One controller-ish class. He describes what the evaluation is doing and how changing the annotations would make a difference in the results. He also includes a dependency injection example with PHP-DI and the Fast-Route package and a more "real world". He ends the post with a look at another handy feature of the library: bans (blocking based on other types of annotations, @auth-ban).

tagged: rauth access control class method annotation tutorial

Link: http://www.sitepoint.com/control-user-access-to-classes-and-methods-with-rauth/

Robert Basic:
Tags for PHP in Vim
Mar 10, 2016 @ 11:32:34

In a post to his site Robert Basic has shared some helpful plugins for PHP developers using Vim as their primary editor. These plugins not only help you jump around in your code but get more context on where you're at.

One thing I was missing for a long time in Vim is to be able to "jump to definition" in an easy and painless way. The other thing I wanted to improve is to be able to tell easily where am I actually in the code base; to see the current class and method name of wherever the cursor was.

With a bit of googling and poking around, I finally came up with a perfect combo of 5 plugins (yep, five!) that enables me to do both, and a little bit of extra.

He shows examples of using three different things he wanted to be able to do when working in his code and the plugins that satisfy each:

One line examples are included showing how to configure them with your current Vim use.

tagged: tags vim plugin jump definition context class method

Link: http://robertbasic.com/blog/tags-for-php-in-vim

Symfony Blog:
Paving the way for Symfony 3 with the "Deprecation Detector" tool
Oct 22, 2015 @ 10:48:31

On the Symfony blog there's a post talking about a tool they've introduced that is helping to "pave the way" for the upcoming version 3 release of the Symfony framework - the Deprecation Detector tool.

Symfony 3 will be released at the end of November 2015. Learning from our own history, the transition from Symfony 2 to 3 will be much more pleasant than the transition from symfony 1 to 2 that happened in July 2011.

Technically speaking, Symfony 3 includes no new features comparing it with Symfony 2.8, which will be released at the same time. [...] This means that your Symfony applications won't work on Symfony 3 unless you remove all their deprecations. In order to simplify the task of finding which deprecations affect your applications, a new tool called Deprecation Detector has just been released.

The tool runs static analysis against your codebase and finds locations where you're using deprecated methods/classes/interfaces/etc and reports them back for fixing. The post includes the commands you'll need to get the tool installed and how to run it against your code. You can find out more about the project and get details on command line options on its GitHub repository.

tagged: deprecation detector symfony2 symfony3 method interface class service tool tutorial

Link: http://symfony.com/blog/paving-the-way-for-symfony-3-with-the-deprecation-detector-tool

Joe Fallon:
Immutable Objects in PHP
Aug 27, 2015 @ 11:53:36

Joe Fallon has a post to his site talking about immutable objects in PHP, objects that once the property values are set they cannot change.

When I first learned to program, I made many objects that were mutable. I made lots of getters and lots of setters. I could create objects using a constructor and mutate and morph the heck out of that object in all kinds of ways. Unfortunately, this led to many problems. My code was harder to test, it was harder to reason about, and my classes became chock full of checks to ensure that it was in a consistent state anytime anything changed.

[...] Now, I favor creating immutable objects. The folks over in the functional camp are super excited about this concept. However, it’s been around for quite a while and it has so many benefits.

He talks about how immutable objects make it easier to not only test code but also allow for more rational reasoning about their contents. He points out that they also make it easier to understand the state of an application should an exception arise. He then gets into some examples of immutable objects, creating an ImmutableClass and a ImmutableClassBuilder to help create instances based on values provided.

tagged: immutable object introduction class builder example benefits

Link: http://blog.joefallon.net/2015/08/immutable-objects-in-php/

PHPClasses.org:
How to Create a PHP C Extension to Manipulate Arrays Part 2: Adding ArrayAccess and
Aug 13, 2015 @ 12:33:04

Dmitry Mamontov has posted the second part of his "How to Create a PHP C Extension to Manipulate Arrays" series on PHPClasses, building on part one and adding in the ArrayAccess and Traversable interface functionality.

In the first part of this article we learned how to create an extension for PHP written in C to create a class that works like arrays. However, to make the class objects really behave as arrays you need to implement certain interfaces in the class.

Read this article to learn how to make a PHP class defined by a C extension implement ArrayAccess and Traversable interfaces, as well understand how to solve problems that you may encounter that can make your extension slower than you expect.

He takes the class he defined in part one and walks you through the addition of the two interfaces. He shows you where they're defined in the PHP source, what the code looks like and how they integrate with the class. He also shows you how to customize the object class handlers, making it possible to use the custom class (object) as an array. Adding Traversable is easier, adding an iterator return method that allows for the data internal to the class to be iterated through.

tagged: phpclasses series part2 extension class array manipulate arrayaccess traversable

Link: http://www.phpclasses.org/blog/post/306-How-to-Create-a-PHP-C-Extension-to-Manipulate-Arrays-Part-2-Adding-ArrayAccess-and-Traversable-interfaces.html

Paul Jones:
Service Classes, Payloads, and Responders
Aug 12, 2015 @ 10:52:27

Paul Jones has written up a post talking about service classes, payloads and responders and how they can help pull logic out of controllers and into more reusable chunks. It's inspired by comments and methods mentioned in another earlier post from Revath Kumar.

Revath Kumar has a good blog post up about extracting domain logic from controllers and putting that logic in a service class. After reading it, I commented that with a little extra work, it would be easy to modify the example to something closer to the Action-Domain-Responder pattern. In doing so, we would get a better separation of concerns (especially in presentation).

Paul applies some of the concepts that Revath outlined to the ADR pattern, suggesting that service classes should always return Payloads and the reduction of functionality in the controller overall. He includes an example of what the resulting code would look like, following along with the "orders" scenario outlined in Revath's post.

tagged: service class payload responder adr action domain responder designpattern

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