Tag Archives: Class

Constant Constants. Finally! (On the inconstancy of constants)

One of the many new features of PHP 8.1 is the ability to declare class constants as final, so that they can no longer be overridden in child classes. The same applies when constants are defined as final in an … Continue reading

Posted in PHP | Tagged , , , , | 1 Comment

Closure Binding for Unit testing of Private and Protected Methods

Some years ago, I wrote about using closure binding to access protected and private properties when unit testing an object, to verify internal state; and I created the SpyMaster library to simplify that task. One feature that I didn’t include … Continue reading

Posted in PHP | Tagged , , , , | Leave a comment

Using PHP Anonymous Classes as Package Private Classes

I’ve written before about the benefits of using PHP’s Anonymous Classes for test doubles; but Anonymous Classes also have potential usecases within production code as well. In this article I’m going to describe one such usecase that can be particularly … Continue reading

Posted in PHP | Tagged , , , , | 6 Comments

Closures, Anonymous Classes and an alternative approach to Test Mocking (Part 4)

In a prior article in this series, I described the use of a SpyMaster Class to create proxy spies as anonymous classes, that allow external visibility (and potentially update) of protected and private properties within an object. The same basic principle … Continue reading

Posted in PHP | Tagged , , , , , | 3 Comments

Closures, Anonymous Classes and an alternative approach to Test Mocking (Part 3)

I have heard people say that you shouldn’t test abstract classes or traits, only the concrete classes that implement or use them. I don’t follow that approach: unit testing is all about testing in isolation from anything that might affect those … Continue reading

Posted in PHP | Tagged , , , , , | 1 Comment

Closures, Anonymous Classes and an alternative approach to Test Mocking (Part 2)

The last time I posted here, I was writing about Anonymous Functions and how they can be bound to any object (or class) to execute as though they are a method within the scope of that class (Closure Binding as … Continue reading

Posted in PHP | Tagged , , , , , | 2 Comments

Anonymous Class Factory – The Results are in

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 … Continue reading

Posted in PHP | Tagged , , , , , , | Leave a comment

In Search of an Anonymous Class Factory

One of the more interesting new features introduced to PHP with the arrival of version 7 is Anonymous Classes. Anonymous Functions (or Closures) have been a part of the language since version 5.3.0, and are something that I find incredibly useful; … Continue reading

Posted in PHP | Tagged , , , | 13 Comments