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.

Helpers

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.

Configurations

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:

library/books/author/richard-dawkins

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.

Solution

 

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.

Solution

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.

Summary

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.

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 php.net 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:

An FTP wrapper class for file handling in PHP

Some time ago, I had to develop a system for handling a large amount of CSV files that were automatically being uploaded to an FTP location. As it was an automated system, I needed a class flexible enough to handle a lot of the common ftp functions I would be making use of. It also contains a couple of additional functions that were more specific to my needs. The latest_file method will take the latest file from the current directory based on the date the file was last modified. This was pretty essential to my needs at the time as the directory was being filled with new CSV files on a regular basis and were irregularly named, so I had to ensure I was only taking the latest file.

The class is also able to make several connection attempts to an FTP, which you can modify using the methods set_attempts and set_delay, which sets the delay between attempts in seconds.

Here’s a basic usage example;

Here is the entire class;

Filter bad input characters using whitelists in PHP

When working with input data using PHP and storing it using MySQL, it can be a bit of a nightmare to filter out bad characters. Any time I’ve had to work with a large dataset full of customer data or product data, it was inevitable that there would be invalid characters, either because it wasn’t supported by the charset of the database, or because whatever system created the original datafile file didn’t support the characters, leading to garbage text. When dealing with customer data it was important not to lose any information during the importing process. This usually involved a conversion table of one kind or another, converting the invalid characters to friendly equivalents so they could be viewed correctly. Especially important, for example, when dealing with customer addresses. Even if it was possible to store the original characters, many third-party shipping services didn’t support them anyway.

So for customer data this was fine, but where this manual approach wasn’t very practical was when creating a large retail product database, which then also had to be uploaded to a third-party platform to list the products in other e-commerce stores. The product data was obtained from a variety of difference sources, which further increased the likelihood of compatibility issues. Time and time again, there would be an unusual character somewhere in the data that would break the entire product description when it was uploaded to the third-party platform. I traced the problem down to the third-party’s MySQL query, which wasn’t robust enough to handle unsupported characters. It broke the query right at that point the invalid character was found, and the rest of the data wasn’t uploaded. Because this data also included a html template for the product description, it led to some very broken product listings. It wasn’t possible to fix this problem at the source, so I had to look at ways of sanitising my data.

Given the amount of products that were being uploaded, it simply wasn’t practical to manually correct each one, especially when there was no real way of knowing which product listings were correct and which were broken due to invalid characters, and in almost every case it was just one stray invalid character that didn’t have any impact on the product description. The conversion table option was also out, as this would have taken just as long. Given there were time constraints, this gave me two options; a blacklist of invalid characters, or a whitelist of supported characters. A blacklist seemed just as much work, as it would still require finding all of the invalid characters, and also dealing with the issue of finding them correctly in PHP, which is rarely straight forward when dealing with different charsets. This can involve needing to find the hex code of a lot of invalid characters in order to replace them. This left me with the option of using a whitelist; a list of characters I knew would not break the third-party’s MySQL query, and would result in almost negligible data loss in the product listings.

This is what I came up with;

This function takes in a whitelist string and an input string. Anything not in the whitelist is removed, and then the string is returned by the function with only the characters from the whitelist remaining. For ease of use with my script, I stored my whitelist string in an external .txt file so it could be edited when needed. This was the whitelist I used;

    ! “#$%&'()*+,-./0123456789:<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~

Which was sufficient in removing the stray unsupported characters from the imported product listings without having any major impact on the quality of the data. While not a perfect solution, it is a good compromise, and additional characters can be added or removed from the whitelist to refine it over time.

Obtain the first or last word inside a string in C++

If you do a lot of work with strings in C++, one thing you’ll probably find yourself doing is writing utility functions. The String class provides many useful built-in methods, but when it comes to searching, replacing and text manipulation, you may find that you’ll need to create your own functions to carry out these tasks, sometimes using and combining the built-in class methods to carry out more complex tasks within your projects.

The two following functions can be used to obtain the first and last words contained within a string. An empty string is returned if the function fails. The functions make use of the built in String methods find_first_of and find_last_of;

Having a decent set of your own utility functions can save a lot of time, and will make your code a lot more manageable. A good strategy for code re-usability is to break up a bigger task into smaller functions that you can see yourself re-using in the future, saving yourself both time and making your code easier to understand.

This is akin to your toolbox, and if you find yourself needing the same tool over and over again, then that’s when it’s a good idea to write your own utility function or use someone else’s. There’s always that temptation to write everything for yourself, but one of the key aspects of code re-usability is to not re-invent the wheel.

Writing your own utility functions does have some benefits, however. Firstly, you’ll understand the code very well and learn new techniques, and secondly, it can improve your overall skills as a programmer, especially in problem solving. So yes, there are times when it is better to write your own, and other times when using someone else’s solution is the best approach. You may even decide to compromise by checking other people’s solutions to help you write you own. There’s no right or wrong way to approach it. Take the approach that suits you or the situation best.

Prime numbers, a simple function in C++

A prime number is simply a number that is only divisible by one and itself. One of the most basic ways to find out if a number is prime, then, is to loop through every number (except for 1, as all positive integers are divisible by 1), and return true if the number is equally divisible by any other number except for itself.

This is a good example use of the modulus operator. The modulus operation returns the remainder of a division of two numbers. So, for example, 15 % 5 would equal 0, as 5 goes into 15 exactly 3 times with no remainder. 17 % 5 on the other hand would equal 2. As 5 goes into 17 3 times, with 2 being left over.

How it works:

Knowing this, and knowing what makes a prime number, we can check if a number is prime by checking every number between two and itself minus one, and if any of these numbers give us a modulus result of 0, then it is exactly divisible by another number and therefore not prime. If no modulus results of 0 are found by the time i is equal to x-1, then we know x to be prime. As each integer would always be divisible by itself, we have no need to check whether this is true or not. If we did want to check this, then our loop conditional statement would be i <= x;

This type of function is known as a predicate. A predicate is simply a function that returns true or false. In this instance, given an input of x, it will return true if the number is prime, and false if it is not.

If you wanted to implement this function and check every number beyond 1 to see if it was prime, you could do it like so;

This will iterate indefinitely until the maximum value that is able to be held in unsigned long int on your machine is reached. At that point, it will wrap around back to 0. You can use a larger data type, such as long long int, if your compiler supports it. But as you'll see, the main drawback of this function is that the larger the number gets, the more numbers it needs to check itself against to determine whether or not it is prime, so you may never reach that limit as it will take progressively longer each time to check the next number in the infinite while loop. The speed at which the program runs will depend on the processing power of the machine you are running it on.

If you needed to quickly check if a number is prime without needing to calculate it, then you could store the results in a text file, and check to see if that number exists in the file. If you store the results numerically, then you can stop searching once you reach the next prime number in the file beyond the one you're searching for. This is a very simple example of a lookup table, which saves computation time by storing the results of an operation rather than having to re-calculate them each time. This makes sense when you're performing a large number of checks of a particular calculation where the result is always going to be the same, and especially if you're going to be performing the same calculations many thousands of times over, and performance is a factor.

There are much more efficient techniques out there for finding prime numbers too, known as prime number sieves. One such algorithm is known as the Sieve of Eratosthenes, and I may follow up this post in future with a discussion on that.