Lucid MVC – Development Update 1

Development Update 1 For Lucid MVC

This is a small update on Lucid MVC. Although I haven’t started work on the FormHelper or Database classes yet, I have made several improvements, and fleshed out the demo site (which will be included with the release).

A contact form page

A contact form page

Most of the work has been on improving form validation, which has been further simplified and made more flexible;

It didn’t really make sense to have the validators created from within the controller, and due to the way validation worked previously, it had to be done after a call to Model::load();. Validators can now be added anywhere within the Model and at any time, which also allows for ad-hoc validation if you want to add/remove validation at any time during the Model’s life-cycle.

With that in mind, validators can be removed simply by passing in the same parameters that match the validator that was added via Model::addvalidator();.

So, if we wanted to remove email validation, we could, for example, remove it before validating our form within the controller;

This is just a simple example. You might wish to remove validation based on user-submitted data. For example, if the user wanted to skip a part of the form, or if the user only had to fill in at least one of two form inputs, you could check if one input was filled in, then remove the “required” validator from the other.


Another area I’ve been working on are the Helper classes, so far we have;

  • AlertsHelper – for rendering Bootstrap alert messages
  • SessionHelper – various methods for handling sessions
  • UrlHelper – various methods for generating links (absolute, relative, etc..)

As well as the FormHelper class, I’ll also be creating a DebugHelper for logging output to file/screen.


The final area I’ve been working on has been the configuration settings within the project, and how best to represent them. Two of the more common approaches are arrays and .ini files, however both would require additional coding to require them automatically where needed, or additional code to call them.

In the end I decided to use a static class representation;

This made the most sense, given that Lucid MVC requires in classes automatically. Configuration settings can be included anywhere within your project, just by accessing the static class property. For example;

You can of course create your own configuration classes if you wish. The private class constructor is just there to prevent the class from being instantiated.

Other ideas

I’m also considering adding in some of the following;

  • Events/Listeners – Add events to Models/Views/Controllers at certain execution points
  • Dynamic Models – the ability to create new model attributes dynamically outside of the class
  • Modifiers that can change/convert data before/after it’s loaded into the model using callables.

Lucid MVC – A light PHP framework for small online projects

Building an MVC framework from the ground-up

Having worked with several PHP MVC frameworks now (Laraval, Nova Framework, Yii2, Joomla), I thought it would be a rewarding challenge to develop my own framework, based on what I liked about these frameworks, and what benefits me as someone who works with PHP frameworks.

The main goals of this project are;

  • Light MVC framework that’s fast to learn
  • Easy to work with, with minimal boiler-plate code
  • Fast form-building
  • Easy form validation
  • Automated URL routing
An early screenshot of the Lucid MVC Framework

An early screenshot of the Lucid MVC Framework

With that in mind, I’ve been working on Lucid MVC; a light PHP Model-View-Controller framework designed for small projects, but with enough features and functionality to avoid repetition and too much boiler-plate code. For example, there is no manual URL routing to speak of, so URLs are automatically translated to direct to the correct controller, passing any additional parameters to the controller if the controller method accepts them.

For example, the URL:


Would instantiate the libraryController, run the books method, and pass in the parameters “author” and “richard-dawkins”. The route will only be successful if the controller method can be called successfully, otherwise, the user will be redirected to the ErrorController, telling them the URL they have reached is invalid (our standard 404 “page not found” page). If the controller allows for optional parameters, then they can be omitted from the URL, and the default parameters will be used. This allows for flexible controller paths, leaving it up to the developer to handle any further validation if they require it.

Like Yii2, a layout template can be applied to the entire view, rendering the view content inside it. Different layouts can be applied in the controller before a view is rendered, or the default layout can be used.

Views themselves are fairly straight-forward. Model attributes, variables or other values are passed to the view via the controller, and can be accessed like normal variables.

Contact Form Controller

This is the controller for the contact form page at the beginning of this post. So far, models can load POST data (assigning it to the model attributes), store data, and perform data validation through a growing-list of validation filters.

Controllers can render views, and redirect to different routes. Controllers also have some basic functionality for dealing with web requests such as checking for POST data.


Automated PHP file-inclusion

Because this is designed to be an MVC framework for small projects, the framework uses a custom autoload function to automatically include any PHP files when you call a new Class. So, rather than using Namespaces, every PHP file will be included automatically at the moment it is needed. For large-scale projects this might cause problems with naming conflicts, but for a library that’s designed to be quick to use and build with, it’s perfectly serviceable.

What’s next

The next two features that need to be implemented are the Database class, and the first of the Helper classes to aid in quick form-building. The Database class will be a wrapper class for PHP’s PDO extension, providing easy-to-use methods for standard database CRUD operations, and will handle variable binding automatically for the highest security and less coding.

The FormHelper class will aid in building HTML forms programmatically, automatically handling the form action, various input types and options, and binding the model attributes to the form.

Once the FormHelper and Database class are complete, I think the project will be complete enough to release on GitHub for anyone who wishes to use it.

How to replace keys in an associative array

PHP has a pretty decent set of array functions for manipulating arrays, but I was a little surprised to see few functions that dealt with key manipulation.

It might not be quite as common a need to manipulate or replace the keys of an associative array, but it could be quite useful when maintaining a standardised format, for example when dealing with user data. Another possible use could be when displaying data directly from a database table.

It’s often convenient to use the database table column names when displaying table data, but those column names might not necessarily be in a user-friendly format. It’s fairly common to see underscores or CamelCase used in column names for readability. Great for programmers, but not so great for published content.

When working with query results as an associative array, this function could be one way to deal with that issue. For underscores, a simple find/replace. For CamelCase, a space before the second capitalised letter. Depending on your circumstances, this might be better accomplished with SQL aliases for simpler column name replacements.

Replacing a key is a little trickier than replacing a value, because you can’t change the key name directly. Instead, you copy the value from the old key, assign it to the new key, then delete the old key where the value was originally held.



This function is relatively straight forward. It checks to see if a key exists in a 2-dimensional array, and replaces the key with function parameter $newkey. The function returns true on success, and false on failure.

Example usage


This example demonstrates directly replacing one key with another, and using a foreach loop to iterate over all of the keys and make them upper case. This second example actually closely resembles the built-in PHP function array_change_key_case(), however by iterating over the keys with replaceKey(), any sort of string manipulation may be performed on the keys.

Logging: A quick and simple way to log output in PHP

If you’ve ever used JavaScript or jQuery, then you’ve probably been spoiled by the built-in console that comes with most modern web browsers. A quick call to console.log() is all it usually takes to quickly inspect your data, providing a fast and invaluable debugging tool while you’re developing your project.

Unfortunately, being a server-side language, PHP isn’t afforded the same luxury, and although plenty of solutions do exist to solve this problem, most of them are often overkill when all you want to do is peek at an array or the contents of a variable during code execution. Debuggers and Unit Testers are available in many IDEs, which are great for complex object-oriented code and when you need to do more than just peek at a value, but more often than not, I find myself using this;

It’s probably one of my most-used code-snippets, because the majority of the time I just want to ensure the data I’m returning from a database query is correct, or that an array I’ve dynamically-built contains the correct data in the right structure. It means I can make alterations quickly, when stepping through a debugger or setting up Unit Tests would take longer. The pre tags ensure the browser preserves spacing and line-breaks, and print_r() provides a nice clean readable format for arrays;

Although this method can be quick, it can also be inconvenient to stop the entire execution of your project. Sometimes however this is unavoidable, especially when working with MVC frameworks, as the final page your browser displays might not be the one you’ve printed your output to. You may wish to see output from your controller or your model, but without stopping execution flow, your application may render your view and prevent you from seeing your test output. Then there’s production code. If you have no other options than to work directly on a live system, then die() is one of the last functions you’d want to use.


A more elegant solution would be to create a helper class that would allow you to log data at any stage during your application.

Then call the static method, passing in your array or variable;

The method will append to the log by default, unless you pass in a different mode. It will generate a html log file in the source directory of the class. You can of course change this if you wish to output your log within a particular directory. Reloading the log is then just a simple matter of hitting refresh in your browser.


While these quick code snippets are by no means a solution to every debugging problem you’ll face, they’re still a useful and time-saving strategy when you need quick access to variable output.

Colourful Random Walks in Processing

A language suited for quickly prototyping your ideas

Processing is a great language for quickly prototyping and designing visual representations of processes. Built on top of Java, it provides a great layer of abstraction that takes away a lot of the verbosity of Java, allowing you to focus purely on creating your sketch (what Processing calls its programs). A quick search online will reveal some wonderful visual eye-candy created within the language, as well as representations of complex data processes, such as fractals and cellular automata, to evolutionary, physics and A.I simulations. Indeed, it’s a language well-suited to scientists and mathematicians just as it is to graphic designers and digital artists.

It’s also supported by a whole host of user-created libraries to extend to the language even further, offering a good level of extensibility in many different areas of programming.


The Sketch

Just as an example of what is possible in a short amount of time with Processing, I decided to experiment with random walks. I wanted to visually represent the walk by changing its colour at every single step, however the colours changed so frequently that it looked like a jumbled mess. It looked more like image noise than any sort of representation of a Random Walk, so that wasn’t going to work.

So, instead of randomly changing colour at every step/iteration, I added in a counter that changed the walker to a random colour value every 5000 iterations;

A random walk in processing

A random walk in Processing using random RGB values.

While this is visually appealing to look at when the walk doesn’t cover too much of the application window (right), it quickly becomes rather garish after many iterations, as is apparent on the left-hand side of the image.

So in the next version of the sketch, I created an array of colours (represented by hex values) that worked well together, and passed that array into my walker object’s constructor. Inside the Walker class, I pick a random index value from the array, and assign the hex value stored at that position in the array to be the colour of the walker every 5000 steps;

A random walk in processing

A random walk in Processing using a random hex value from a predefined set of colours.

Many more iterations later

Even after many iterations, with fewer colours, and by using a complementary colour-set, the effect is much nicer to look at;

A random walk in processing

Many more iterations later.

All of this took surprisingly little code, thanks to Processing. A little under 70 lines of code, including white-space. A great language to learn, whether you’re new to programming, or an experienced programmer.

Because Processing abstracts out a lot of the underlying Java code, it allows you to be much more succinct, without compromising on any of the syntactic sugar that makes Java an easy object-oriented language to work with.

Function overloading in PHP

Why it is and isn’t supported.


Although PHP doesn’t handle function overloading in quite the same way as some strongly typed programming languages, it does support the ability to overload functions. Just in a different way.

Because PHP uses type inference to deduce the type of values your variables contain, it isn’t able to differentiate between two functions of the same name with different parameters.

For example, if we wanted to create an add() function that could add two numbers, or concatenate two strings, in some languages we could write something like this;

The first thing you may notice if you use an IDE is that it reports an error. In NetBeans, we get the following error:

Method or function “add” has already been declared

PHP doesn’t allow you to declare two functions in the same namespace with the same name. This is because If you were to invoke the function, it wouldn’t know which of the two functions you meant to call. Although PHP supports explicit type casting, it does not support declaring a variable of a particular type, so it doesn’t support “classic” function overloading. The manual refers to this as Type Juggling.


Solution: One function, and a little extra logic.


Okay, maybe more than just a little.

PHP has several built-in functions which allow you to access the parameters of a function. This is what gives you the ability to overload your functions. Although the syntax is somewhat more verbose, it is possible to achieve function overloading in much the same way as the example above. However, instead of creating multiple functions with the same name, you create one function, and call different code blocks based on the number and the type of parameters. The main difference here is that you have to do it in a very explicit way.

When function_num_args() is called inside a function, it will return the number of parameters that were passed to the function during that function call. Using this, we can specify which section of code we want to run, based on the number of parameters passed into the function when it is called.

func_get_arg() allows us to access the values passed to the called function. The function takes a zero-based index value, so the first parameter is at position 0, the second at position 1, and so on.

Finally, we can call the is_string() and is_int() functions on the returned values. These functions will return either true or false, based on whether the values passed to them are integers or strings, respectively. There are several of these functions available in PHP. For example, if we wanted to handle floating point numbers as well, we could use the is_float() function. Using additional logic, we could call the same block of code whether a number is a floating point or an integer value, or handle these types of number entirely differently.

Now when we call the function;



Should you use it?


This is entirely up to you. It could be argued that function overloading in this manner violates the Separation of concerns principle (although it is a principle, not a hard-and-fast rule). Provided the code is readable and easy to maintain, and makes sense for its purpose, then used wisely, it can add additional flexibility to your code when handling various data-types. You might, for example, want to round floating point numbers to a certain level of precision, or ensure integer values are always returned to two decimal places, such as when dealing with currency values.

Multidimensional arrays of any depth in PHP

Creating multidimensional arrays in PHP

There are times when you need to generate an array structure in advance of the data you want to store inside it. In most cases when I need this, I’ll create a few for loops to create my array structure, and insert my data as I build up my array. While this is fine, it can get a little messy for arrays of more than two or three dimensions, so a clean approach is to separate the array generation from the array content generation. You can always iterate over your array after you’ve generated it.

Recently, I needed the ability to create an array of any depth, so I wrote a function that would allow me to do so. PHP’s arrays are dynamic, but the built in array() function in PHP doesn’t allow you to specify a size when you create an array. However, because arrays in PHP are dynamic, it’s very easy to create dynamically sized arrays on the fly.

The code:

If we want to create a 3×3 array:

If you wanted to create a 3x3x3 array, you can use create2dArray() with the PHP built-in function array_fill():

If you wanted to create a 3x3x3x3 array, aka a 4-dimensional array:

Using a combination of the techniques above, you can create an array of any depth, and you can tailor the create2dArray() function to your specific multi-dimensional array-creating needs.

Multidimensional arrays can be a little tricky to get your head around at first, so if you’re having trouble visualising what your multidimensional array looks like, use this snippet:

Let’s go one step further. You might not even know what level of depth your array will need to be at run-time, so it would be great if you could dynamically determine what level of depth you want your array to be via user-input or some other method, and then generate your array based on those values.

Now, using both createDynamicArray() and create2dArray(), we can create an array of any depth dynamically;

Create a 4x4x3x3x2x2 array:

Create a 4x4x3x3x10 array:

Tokenizer, a simple lexer in PHP to extract tokens

Tokenizer V1.0.0

Recently, after discovering Emmet for Sublime Text 3, my interest in parsing grammars and generating my own basic programming language and syntax was rekindled. Having the ability to use a simple, easy to understand syntax to quickly generate common boilerplate code (or, in the case of Emmet, html) is a tempting prospect.

Why build a Lexer?

Although most modern PHP IDEs do have snippet support; the ability to create re-usable sections of code, they’re not grammars. You can’t use them to construct code from many little building blocks or keywords. Just one at a time. Snippets are useful, though. If you find yourself writing for loops on a regular basis, then having a keyword you can type into your editor that quickly expands into a for loop ready for programming is a life saver.

But what if you wanted nested for loops? Or a for loop within a for loop, with three nested if statements inside it, followed by a while loop proceeding the first for loop? Yes, you could use snippets, but they wouldn’t be quite as fast. It would be nice to have a syntax as simple as Emmet’s, but designed specifically for PHP.

My first attempt at creating a Lexer was all-too basic, and I found myself trying to write an ever-more complex regular expression to try and extract all the tokens I wanted from my string. Which is why I decided to build a simple Lexer class, instead.

Tokenizer in action


This is a basic example of the tokenizer in action. You create a series of patterns to match using regular expressions, and the tokenizer will take an input string and extract the tokens from it. If the tokenizer encounters any problems, it will report where in the patterns array the error occurred.

Why PHP?

Why not? As a scripting language, PHP makes it much easier to focus on developing the language itself than getting too bogged down in the syntax of a more complex programming language. This may be at a performance cost due to PHP being an interpreted language, though. However, one of the benefits of an interpreted language like PHP, Javascript or Python is the function eval(). This function interprets a string as inline code, and then processes it as code. Although some people will tell you eval() is evil, I think it’s just misunderstood. Let’s say you wanted to a parse a mathematical expression in your new programming language, and return the result. You could write your own math parser. Going through the process of adding in operator precedence, including parsing expressions in brackets first, or..

However, if you had performance in mind, but still wanted to maintain the ability to have some of the work done for you, then you could look at Lua and ChaiScript, two scripting languages you can use within C++. This gives you the performance of a compiled language, and the flexibility of a scripting language, all in one.

What next?

There is of course much more to creating a programming language than just recognising the individual tokens the syntax is made of. Once you have your tokens, you’ll need to parse them with a parser which can then interpret the tokens as a grammar, and then perform actions based on that grammar. One common approach is creating what is known as an abstract syntax tree. This can be accomplished by using the Interpreter design pattern, for example.

There are also pre-built lexers and parsers out there if you want a good head start, such as ANTLR and Bison, however you may find building your own will give you a much better understanding of how grammars can be built. I recommend looking into Chomsky hierarchy and Backus–Naur Form as they’re a good place to start.

If you’re looking for a light introduction into building a simple programming language, howCode have an excellent set of tutorials on their youtube channel. The playlist can be found here. The tutorials are written in Python, but the techniques can be applied to a variety of different imperative programming languages such as PHP, C++, Java and even Javascript.

Download Tokenizer on Github

The example.php file on github includes a more fleshed out example file to get you started on creating your own programming language.

Building an Online Retail Store – Update 4

The store is almost in a functional, usable state. I still have several extensions to install, which deal with the following;

– SEO Module: SEO friendly links, auto meta data generation, and more
– Extended Coupons: More functionality, time limits, auto-generated banners
– Extended searching: AJAX auto-suggestions in the search bar, ala Google
– Enhanced Shipping – Custom creatable shipping methods, based on multiple rules/options

With these in place, the store will be in a state in which I’ll be happy with using. Before installing these final modules, however, there is one more extension I need. Unfortunately, despite extensive searching, I’m unable to find what I’m looking for. The built-in order display system in OpenCart is extremely basic. You have to go into each order manually to see what has been ordered, and navigate multiple tabs to see products, customer address, order status, and so on. This is extremely slow and inefficient, not to mention very difficult to manage. When dealing with the shipping of customer orders, this is going to take up an inordinate amount of time. With no way to filter orders by status, or a quick way to pull customer data or product purchases, it’s just not feasible to use for shipping orders.

Because of this, I need to develop a custom order picking script. It doesn’t need to be overly complex at first, but it does need to be well-designed enough to be extensible in future. It needs to display orders sorted by date, and filtered by order status. Additionally, each order needs its own “complete order” button, which will change an order’s status from “processed” to “complete”, thereby removing it from the processed orders list. A timestamp may need to be placed in the order notes of when the order was shipped/completed.

The order picking list needs to display the customer’s order number, their name, address, purchases, including product name, model number, and purchase price. The available stock quantity needs to be displayed next to each product. The data needs to be formatted into tables, as to be easy to read.

One task will be in writing the necessary MySQL queries to retrieve all the data. For ease, this will comprise of three queries; the first will obtain the customer orders, based on order status, and auto-arranged by date. The second query will obtain the product information for each of these orders. The third query will update each order’s status. The queries will need to be written in such a way that any part of the query that is likely to change will be inserted as a variable, and then later linked to any forms or buttons that may be added at a later stage for filtering on orders or updating them. A drop-down list to filter orders by status is highly likely, but this can be added later. Beyond that, being able to filter orders by date range or overall product availability may be advantageous, but not a priority initially.

The table formatting code could be written as procedural inline code, but it makes much more sense to create a table-generating utility class, which will be easier to manage, and flexible enough to be re-used in future. This class can be generalised enough so that I can determine the number of columns, the number of rows, and insert the data row by row, building up the table until I’m finished with it, then move on to the next order. Most of scripting time will be spent in creating the table utility class, but this doesn’t require the database data beforehand, so I’ll likely work on this first, and create some mock orders through testing before I create the main picking list script, as I want to be able to work on it from start to finish without having to take time away from it to work on the table class.

Building an Online Retail Store – Update 3

I decided to opt for a relatively low cost extension to import my products into the database via CSV, rather than write an entirely new product importer from scatch. For the most part, it does everything I need it to do. I had to write another small PHP script to format the CSV feed to some of the product importer’s specifications though, as the importer only has one secondary image field, but the field does accept multiple image locations split by a custom delimiter. With the amount of secondary images and products I had to import, it was quicker to write something that would combine all the secondary images into a single string split by the custom delimiter, and insert an additional column into the CSV file. As I’ve done extensive work with PHP and CSV files in the past, I already had most of the necessary functions.

The products have now been imported into the website, but they will require some manual editing, as the descriptions provided by the product feed weren’t html formatted.

Today I’ve been looking at the shipping options for the store, and I’ve finished setting those up using the built-in “Weight Based Shipping” module in OpenCart. It’s a little too basic for my needs, though, so I’ve already picked out a suitable extension that will provide additional flexibility, which I’ll install tomorrow.

I’ve also fixed two major issues that I’ve had with OpenCart 2.0. The first problem was with installing new extensions. I was constantly getting “method not implemented” errors every time I tried to install an extension from a zip file. The problem turned out to be with how OpenCart handles uploads. If you don’t configure it to use FTP, it can’t handle zip files. I got around the problem by using a small extension that bypasses the need for an FTP connection.

The second problem was with not being able to modify customer orders, which meant I wasn’t able to change an order from pending to processed. This problem was a little more difficult to figure out as the error message wasn’t particularly clear;

SyntaxError: JSON.parse: unexpected end of data at line 1 column 1 of the JSON data

It turned out to be a problem with the default .htaccess file that was created in the root of my store’s installation directory. Removing it fixed the problem.

I have a few more extensions left to install and configure, but once that is done, I can focus on the product descriptions, and start testing the store fully by placing a few orders using Paypal’s sandbox mode.

I’ve already decided on a ticket support system for the store. I’ve decided to go with Hesk, which I’ll be manually installing for my customer support system. It was either that, or osCommerce, but as I’ve used osCommerce before, Hesk looks like a much better alternative by comparison, with more features, so I’ll install that fairly soon.