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

Master Zend Framework:
How To Generate Class Factories The Easy Way with FactoryCreator
Jan 20, 2017 @ 10:07:57

The Master Zend Framework site has a new tutorial posted guiding you through the process of generating class factories the easy way with the help of the "FactoryCreator" tool in the Zend ServiceManager component.

If there’s one thing that’s always frustrated me when working with Zend Framework, it’s having to create factories for classes. Sure, it’s gotten easier as Zend ServiceManager’s continued to ever improve. And PhpStorm and Zend ServiceManager Grand Master, Gary Hockin, has given me a number of great tips and suggestions.

But it’s always been something I’ve felt frustrated by. Perhaps you feel the same. [...] But, what I’ve felt for some time is that they could also make it easier for us to follow these best practices too, such as with some tooling support. In the latest release of Zend ServiceManager, version 3.2.0, they have.

He goes on to talk about two tools that are included in this latest release: the ConfigDumper and FactoryCreator. He helps you get the FactoryCreator tool installed and provides a simple example of it in use, generating the factory for a "JournalService" class. He includes the results of the generation of the simple example before moving on to a more complicated example: a TableGateway object. The final example shows the generation of the factory for an "Actions" class, handling the controller processing for a simple MVC application. If you're a bit shorter on time, he's also created a screencast version of the tutorial you can view in-page or over on Vimeo.

tagged: zendframework generate class factory factorycreator tutorial screencast

Link: http://www.masterzendframework.com/simple-factory-generation-with-factorycreator/

SitePoint PHP Blog:
The Delicious Evils of PHP
Dec 07, 2016 @ 09:50:49

On the SitePoint PHP blog Christopher Pitt is back with another interesting article, this time talking about two "delicious evils of PHP" - the eval and exec functionality.

I want to look at two PHP functions: eval and exec. They’re so often thrown under the sensible-developers-never-use-these bus that I sometimes wonder how many awesome applications we miss out on.

Like every other function in the standard library, these have their uses. They can be abused. Their danger lies in the amount of flexibility and power they offer even the most novice of developers. Let me show you some of the ways I’ve seen these used, and then we can talk about safety precautions and moderation.

He then talks about some of the "interesting" things you can do with these two pieces of functionality including:

  • Dynamic Class Creation
  • [Creating] Domain Specific Languages
  • Parallelism (with exec)

He ends the post with some advice how to avoid issues with the topics he's mentioned and how to "stay safe" while still using these two dangerous pieces of functionality.

tagged: evils language eval exec dynamic class dsl parallelism tutorial safe

Link: https://www.sitepoint.com/the-delicious-evils-of-php/

Fabian Schmengler:
Using class_alias to maintain BC while moving/renaming classes
Sep 09, 2016 @ 11:55:12

In a post to his site Fabian Schmengler has shown how to use class_alias to prevent breakage while renaming or moving classes around in your application during refactoring.

Sometimes you want to rename a class or move it to a different namespace. But as soon as it is used anywhere outside the package, this is breaking backwards compatibility and should not be done lightheartedly.

Luckily there is a way in PHP to have both, the old class and the new class, while deprecating the old one: class_alias().

He then gets into the details of using this handy function to define the links between the files, necessary in two different places to prevent autoloading breakage. He also offers an alternative, making use of the "autoload.files" option in the Composer configuration (but this means adding each one to that list). He finishes the post by suggesting one more thing as you update your code: making it with an @deprecated annotation to help locate it later (and flag it in your IDE of choice).

tagged: classalias function maintain backwardscompatibility move rename class refactor

Link: https://www.schmengler-se.de/en/2016/09/php-using-class_alias-to-maintain-bc-while-move-rename-classes/

Gary Hockin:
ConfigAbstractFactory in ZendServiceManager
Sep 02, 2016 @ 10:31:16

Gary Hockin has a post to his site today introducing you to the new ConfigAbstractFactory class to work with ZendServiceManager in Zend Framework applications. The library helps make the creation of configuration service factories easier than having to write them in code.

I wanted to introduce the new ConfigAbstractFactory that has been written for ZendServiceManager 3 and got merged to develop today and will be included in the next 3.2.0 release of the ServiceManager.

[...] Laravel has shown us that developer usability is a real thing and that by making things easier for your target audience you gain traction and Good Things Happen. This is why in response to an issue on the Service Manager repository, I’ve written the catchily named Config Abstract Factory. Essentially, it allows you to create service factories from configuration rather than having to write all the code.

He talks about being a fan of the "configuration over magic" approach the Zend Framework has and how, with this new new library, it makes it even easier to directly link configuration files and the objects created based on their contents. He gives a simple example of a UserServiceFactory, first showing the "old" way of handling it then how to shift over to the new abstract handler just defining the same setup in the module configuration.

tagged: configuration abstract factory class servicemanager zendframework

Link: https://blog.hock.in/2016/09/02/configabstractfactory-in-zendservicemanager/

Matt Stauffer:
Introducing Mailables in Laravel 5.3
Aug 05, 2016 @ 10:57:32

Matt Stauffer has posted the next in his "what's coming in Laravel 5.3" series today with this look at "mailables" to help make sending mail simpler in Laravel-based applications.

For the longest time, sending mail in Laravel has felt clumsy compared to the relatively light APIs of most other Laravel features. I'm not saying it's awful—it's still so much cleaner than its competitors--but it's often confusing to figure out what goes in the closure and what doesn't, what the parameter order is, etc.

Mailables are PHP classes in Laravel 5.3 that represent a single email: "NewUserWelcome", or "PaymentReceipt". Now, similar to event and job dispatching, there's a simple "send" syntax, to which you'll pass an instance of the class that represents what you're "dispatching"; in this context, it's an email.

He gives an example of the updated syntax for calling these "mailables", how to create them with the artisan command and their structure/usage. He also shows how to pass data into the object and some other included features (like customizing the delivery list, queuing and working with attachments).

tagged: mail sending laravel mailable class interface overview

Link: https://mattstauffer.co/blog/introducing-mailables-in-laravel-5-3

Freek Lijten:
Final, private, a reaction
Jun 21, 2016 @ 10:39:37

In response to a few other posts about the use of "final" in PHP development, Freek Lijten has posted some of his own thoughts and some of the things he came to realize about its use in his own development.

I read a blog by Brandon Savage a couple of weeks ago and it triggered some thoughts. He refers to a blog by Marco Pivetta which basically states "Final all the things!". Brandon comes back with a more mild opinion where he offers the notion that this approach might be overkill. Since both posts got me thinking I tried to organise my thoughts on this in the following post.

Freek talks about a pretty common trend in the PHP world: the very rare use of "final". He suggests that "extension" of classes is a bad idea (or at least should be used a lot less) and how he has seen it commonly misused. He then shares two reasons why he thinks "final" is a good idea, mostly centering around how easy it is and how the Open/Closed principle applies. In the end, he notes that he'll be trying to use more "final" in the future and see where it takes him and his code.

tagged: final private reaction development practice class oop openclosed

Link: http://www.freeklijten.nl/2016/06/17/Final-private-a-reaction

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/