Creating a fake enum in PHP.

In a current project I needed to solve a problem where I wanted a class to only accept a set list of string values in a setter. In other languages you could use an enum to easily solve this. Unfortunately, PHP does not have anything like an enum natively. I needed to come up with a solution myself. I had two requirements:

  1. Limit input to a set list of strings
  2. Code completion of the values

Doing some research there were a number of developers looking for something like a enum and just as many solutions.

I ended up combining a few tricks I saw from my research. I wrote an abstract class with constants named and set to the strings I wanted to limit input to. Then I wrote a static method that checks if a string is a valid member of the class by seeing if there is a constant defined with that name.

Here’s a simplified version of the class:

Now I could setup my setter on the other class like this:

public function setSomething($value)
    if (FakeEnum::isValid($value)) {
        $this->something = $value;

Then call the setter like this:


Typing FakeEnum:: in my IDE would use auto-completion to pull up a list of all the constants in the FakeEnum class.

WordPress Plugin Framework version 2.0.0 released.

The framework has almost been completely rewritten with this release to make the creation of WordPress objects more strongly typed and automatic.

WordPress objects now have supporting classes for arguments. This cuts down on having to type in optional parameters on construction.

The Settings class now has supporting classes for SettingsSection and SettingsField.

The View class is no longer a static and each WordPress object will have its own View saved as a property.

The View’s render() method now echos the output instead of returns it.

Check out the documentation and view the repo on Github.


Vagrant and Puppet, Part 4: Writing a Module

A module is a collection of Puppet resource blocks that work together to carry out a related task. All the blocks are grouped together in a class that is included in your main manifest.

This article will go through creating a very simple module for the Nginx web server.

Step 1: File Structure

Inside the puppet/modules folder we created in Part 3 create a folder named nginx. Inside the nginx folder create a folder named manifests and a folder named files. Inside the nginx/manifests folder create a file called init.pp.


This is the basic file structure for a Puppet module. The module folder will have the same name as your module. Inside it you have a directory for the module’s manifest. The init.pp file in the nginx/manifests folder is the main manifest file for the module.

The files directory is used to store any files that you might call from your manifest. We will not be using it in this article but I want to make you aware of it.

Step 2: Define the Class

Open the puppet/modules/nginx/manifests/init.pp file in an editor and add the following code.

This defines the Nginx module. All your resource blocks will be placed inside this class.

Step 3: Add Resources

Inside the Nginx class we’ll add the Puppet resource blocks we need to completely install Nginx.

The first block will be a package block telling Puppet to make sure the “nginx” package is installed. Place the following code inside the nginx class.

We define the package we want to install by naming the block nginx. We ensure the package is present and we require the update-package-list exec block we defined in our main manifest to be run before this block.

Next we add a service block to make sure the Nginx service is running.

We use the service name as the name for the block. We ensure the service is running and we require the nginx package block to be run first.

This is all you need for a very simple module to setup Nginx with all the default settings and virtual host.

Step 4: Include the Module

Open the puppet/manifests/init.pp (main manifest) file in an editor.

At the bottom of the file add the following line of code.

This will let Puppet know that we want to include the nginx module. Puppet will look in the module path defined in our Vagrantfile for a folder named nginx then will run the resource blocks found in the init.pp file under the modules manifests directory.

Run vagrant provision. Puppet will see the new blocks and apply the changes. We now have Nginx running on our virtual machine.

Next Steps

This concludes my series on using Vagrant and Puppet. I have only covered very simple tasks to keep it easy to follow.

If you would like to learn more here are some good resources to get you going.

The Puppet online documentation is a good place to start. It will take you through all the different resource blocks and directives you can use as well as more advanced Puppet features. They also offer a free course with a VM to guide you through lessons.

The Forge is a repository where people can submit and download Puppet modules.

You can also view more modules I have written and use in my Puppet-Modules repository on GitHub.


Vagrant and Puppet, Part 3: Introduce Puppet to Vagrant

After finishing Part 2 we now have a working virtual machine ready to use. The only problem now is there is barely anything on it to use, it is just a basic Ubuntu install.

At this point you normally go through the manual process of adding repositories, installing packages, setting up services, etc. Then if you wanted to reuse this machine you’d have to go through the process again. This is what Puppet fixes.

Puppet allows you to write a manifest of your virtual machine’s desired configuration and have it happen automatically. Let’s get started.

Step 1: Directories and Files

Puppet Directory StructureCreate a new folder in your project and name it puppet.

Create a new folder named manifests and one named modules inside the puppet folder.

Inside manifests create a new text file named init.pp. This will be our main Puppet manifest. A manifest has the Puppet blocks that provision the machine.

We’ll go into Puppet modules in another article.

Step 2: Tell Vagrant About Puppet

Open your Vagrantfile and add the follow lines below the static IP configuration line.

This tells Vagrant that we are using Puppet and let’s it know where the manifests, modules, and main manifest file are.

Step 3: Tell Puppet to Update the Package List

If you vagrant up now you nothing is different than before we added the Puppet config. We still need to tell Puppet to do something.

The first thing we want to do on a new Linux install is update the package list. Open init.pp in an editor and enter the following code.

This is a basic Puppet block, in this case an execution block specified by exec.

Inside the block we give it a name update-package-list. The next line tells Puppet what command to execute, in this case sudo apt-get update. Then we specify the path of the executibles.

Now run vagrant provision from the command-line. This checks for and applies changes to an already running Vagrant box.

If everything is correct you should see the following line in the Vagrant output.

notice: /Stage[main]//Exec[update-package-list]/returns: executed successfully

We now have Vagrant and Puppet talking to each other and we are ready to further automate the provisioning of our virtual machine.

In the next article we will use Puppet to install and setup the Nginx web server by creating a module.

Update To My WordPress Plugin Framework

I have refactored my WordPress Plugin Framework to be independent of the plugin boilerplate that I packaged with it.

The classes for Pages, Settings, Custom Post Types, Taxonomies, and Views were extracted and updated. They can now easily be included and used in any plugin.

I have also set the framework up as a Composer package.

You can check out the updated documentation and view or download the source on the WordPress Plugin Framework page.



Vagrant and Puppet, Part 2: Vagrant Up!

In Part 1 we downloaded and install VirtualBox and Vagrant. In this article we’ll create the virtual machine and SSH into it.

Step 1: Create the Vagrantfile

Inside your project folder create a text file named Vagrantfile, there is no extension. This is the file that Vagrant will read to provision the virtual machine.

Open the file in an editor and add the following code.

You might notice this is in Ruby syntax. Don’t worry if you are unfamiliar with Ruby, you will not really need to know any.

Line 1 opens the configuration block and tells Vagrant that we want to use version 2.

Line 1 declares what operating system we want  our virtual machine to use. This particular example is for a Ubuntu 12.04 64-bit base box that HashiCorp, the company behind Vagrant, hosts. has a good list of all the base boxes available to use.

Line 3 sets a static IP for the virtual machine. This line is optional but one I always take since I use Vagrant to set up servers.

Line 4 ends our configuration block.

Save the file and close your editor. This is all we’re doing with the Vagrantfile today but if you’re interested in learning more check out the documentation on Vagrant’s website.

Step 2: Starting the Vagrant Box

Open a terminal or command-line window and navigate to your project directory.

Type and enter the command vagrant up.

At this point Vagrant will download the base box then setup and boot the virtual machine. If it finishes successfully you’ll see a line that says, “Process finished with exit code 0.” Your machine is now ready to use.

Step 3: SSH into the Vagrant Box

Using SSH to connect to your new virtual machine is just as easy as starting it. In your command-line type vagrant ssh and wait until it logs you in. From here you’re in a full have access to the shell of your virtual machine. When you’re ready to log out just type exit.

If you’re on a Windows machine you might get a message that you do not have a SSH client installed. You will need to find and install a client.

Step 4: Stopping and Deleting the Vagrant Box

To shutdown the virtual machine just use the command vagrant halt in the Vagrantfile’s directory using the command-line.

If you want want to completely delete the machine, say if you’re finished with the project and want to recover hard driver space use the vagrant destroy command.

That is it for this article. In Part 3 we’ll start using Puppet to further configure the VM.


Vagrant and Puppet, Part I: Getting Started

Vagrant and Puppet are two tools I use every day for work and side projects.

Vagrant provisions a virtual machine and Puppet configures packages, services, and even users on the VM.

These tools allow me to create a set of configuration files for a virtual machine, keep it in my repository, then use it to easily create the virtual machine on any computer I’m working on. This keeps my environments consistent as well as anyone working on a project with me. The configuration can even be used to create a production environment for further consistency.

Vagrant and Puppet also end the need to have web server software running locally on my machine.

There are tools out there to graphically guide you through the creation and setup of a Vagrant box, the most popular being PuPHPet, but I prefer the control and flexibility of writing the files myself. This series will guide you through this.

In order to get started you will need to download and install Vagrant and a provider for Vagrant to use.

Step 1: VirtualBox

VirtualBoxVagrant needs a provider to run the virtual machine. A provider can be VMWare, VirtualBox, or even AWS and Digital Ocean. For these tutorials we’ll be using VirtualBox.

Download and install VirtualBox for your platform. That’s it. You will not even need to start the program. You’re done with this step.

Step 2: Vagrant

Next you’ll need Vagrant itself. Download and install the version for your platform and make sure it is in your OS’ environment path.

That is all for this article. Next we will work through creating a Vagrant box.