Skip to main content

Posts

Showing posts with the label programming

Component cohesion

Image: Pixabay Breaking your application down into components can be a useful approach to a "divide and conquer" methodology.  Assigning specific behaviour to a component and then defining interfaces for other components to access it allows you to develop a service driven architecture.  I'm in the process of decomposing a monolithic application into services that will eventually become standalone micro-services.  Part of the task ahead lies in determining the service boundaries, which are analogous to software components for my micro-service application.  I want components to be modular to allow them to be developed and deployed as independently as possible.  I'm using the approach suggested by Eric Evans in his  book on domain driven design  where he describes the concept of "bounded contexts".  I like to think of a bounded context as being for domain models as a namespace is for classes.  These contexts are spaces where a domain mo...

Writing SOLID Laravel code

Image: Pixabay SOLID is a mnemonic acronym for five object-oriented design principals that are intended to make software designs more understandable (see Wikipedia ). They were promoted by a chap called Robert C Martin who has been programming since before I was born and is an authority on writing clean code.  Laravel is a PHP framework that implements the model-view-controller (MVC) pattern. A lot of people think that their responsibility for OOP design ends with adopting a framework, but actually Laravel is relatively un-opinionated on your OOP design and you still need to think about writing code that is testable and maintainable.  The reason that SOLID principals matter becomes apparent when you work on a single project for a long time. If you're writing throwaway applications for clients that you never expect to work on again (presumably because the client won't hire you again) then the quality of your code doesn't matter. But if you're the guy stuck ...

Why is it important to understand how PHP matches a catch block?

Image: Pixabay PHP allows you to have multiple catch blocks.  The manual says that when an exception occurs PHP will try to find the first matching exception block, but it isn't entirely clear on exactly how  PHP matches "catch" blocks when handling exceptions. Why is it important to understand how PHP matches a catch block? It really relates to the fact that you are able to extend the base "Exception" class in PHP to make your own classes.  Throwing exceptions that are specific is good practice because it allows you to write catch blocks that are focused on solving just one sort of problem at a time.  The SPL exceptions are a good example of hierarchical exception inheritance in case you're wanting to see some practical usage. In PHP a catch block is a match for an exception if the exception class is either the same as or inherits from the class in the catch clause. Here's a trivial example: In this example I've set up an excepti...

Send a vCard in PHP with an SMPP gateway

I was recently asked to write a program that will send a broker's phone number as a vCard to a list of leads. There is a surprising number of pages dedicated to this problem, but no simple solutions. Here's a quick and easy PHP solution to the problem: $header = "//SCKL23F4 "; $vcard = bin2hex("BEGIN:VCARD\r\nFN:Cellsmart JHB\r\nTEL;PREF:+27116467353\r\nEND:VCARD\r\n"); $message = $header . $vcard; Then send the $message using your SMSC SMPP gateway as usual. The W3C has some notes on the vCard that has a useful example of a card but a Google search should quickly reveal the full standard details. Just by the way the $header variable is set to //SCKL23F4 because this is a newer format and is supposed to support vCards that need to be sent over two messages.

PHP email Class

I was looking for a PHP SMTP email class and stumbled across this PHP email class by Manuel Lemos. PHPclasses.org is rapidly becoming my first port of call when I'm looking for a class to fill some general function in my code. Of all the scripting sites out there, and there are hundreds, this is possibly one of the few that I would recommend to people. One of the reasons that I liked the script was the debugging feature that showed the server responses as they happened. I've been busy setting up firewalls, SMTP relays, and IIS (hate hate hate) so it was useful to be able to get debug info from my client software. In any case the class worked flawlessly first time, it's free to use, and my client is happy.

Prevent XSS attacks

XSS (Cross Site Scripting) is a surprisingly easy weakness to exploit on unprepared websites. To describe it at its highest level an XSS attack involves injecting code into a webpage and having a user execute it on the grounds that they trust the website you have hijacked. There are a great many vectors for an XSS attack to come through, but for the most part applying a few simple safety precautions will greatly improve your site security. XSS attacks can be split into one of three categories: Stored XSS attacks - are those where the attacker stores malicious code in your database, forum, comment section or elsewhere on your site. The victim receives the code when they request that particular content from your website. Reflected XSS attacks - are those where the malicious code is reflected off the server and sent to the victim as part of search results, emails, error messages, etc. This can be set up by tricking the victim into clicking a specially crafted link (or filli...

"Word of the Day" PHP script (with word list)

I was looking around for a way to generate a word of the day on the web and didn't find anything. So I coded a quick and dirty script to do it. Just in case anybody does a Google search and manages to find my blog: here is my Word of the Day PHP script : Copy this code snippet into a wordoftheday.php file: $file = fopen("interesting_words.txt","r"); $raw_string = fread($file,filesize("interesting_words.txt")); fclose($file); $words_array = explode("|",$raw_string); echo $words_array[array_rand($words_array)]; Of course the real issue I had was finding a list of interesting words in the right format. Here is the list of interesting words that I used: Copy this into a file called interesting_words.txt : ubiquitous : being or seeming to be everywhere at the same time; omnipresent| ecdysiast : a striptease artist| eleemosynary : of, relating to, or dependent on charity| gregious : c...

Ruby on Rails resources

Installing Ruby For Windows users the quickest way to get Ruby running on your box is to get Instant Rails which provides you with a development platform right out of the box. Mac users can use Locomotive . If you're using Linux and don't know how to manage packages then install a GUI, shave your head, and pretend you actually like Macs. Getting started If you're like me you will want to try Ruby before reading a more thourough guide or online book . Then maybe have a look at database access before spending some time reading a Ruby blog . There are a number of Ruby forum s online. Cheating at ruby This Ruby cheat sheet is a 14 page PDF that will act as a quick reference. This resource website will also allow you to quickly develop your skills.

Creating layered images in C#

I'm currently developing a mobile site for a client. The site is the frontend for a competition that they will be running in 2010. Part of the project specification requires the user to be able to build an image by choosing a background, foreground, and slogan. The site will then composite these images and enter the result into the competition. In addition the site will send text messages to the users cellphone informing them of their status and (if they elect to receive it) a "word of the day" This is probably the closest C# solution to what I was looking for: public Image LayerImage(Image Current, Image Layer, Int32 LayerOpacity) { Bitmap bitmap = new Bitmap(Current); Bitmap layer = new Bitmap(Layer); Color pixel = new Color(); for (int x = 0; x { for (int y = 0; y { pixel = layer.GetPixel(x, y); bitmap.SetPixel(x, y, Color.From...