When to use Front Controller pattern in PHP?

Using the Front Controller pattern in PHP always seemed fairly pointless to me. I believe that using PHP’s native features with care usually leads to the best code; simple, re-usable components. For example, passing in an object of class ‘MyLogger’ to every object in the system not only ties all the components to using MyLogger, making their use in a system without MyLogger awkward, but the reverse too: any imported code would need to some how be converted to use MyLogger.

The paths-to-controller mapping felt like it was best done by having one publicly accessible php file for each page, meaning no logic necessary for mapping.

But there was one issue with this that always bugged me. The code in those publicly visible php files, as simple as they may be, (requiring a core bootstrap file and instantiating a couple of objects that do the heavy lifting), were a pain to write automated tests for. Every page would need a test, to check that the cut-and-paste between them had not been broken. Every new page introduced a new weak spot.

But with the Front Controller, all that was reduced to a single page.

So there’s the biggest advantage in my opinion.

Saying that, I don’t think that Front Controller is always the best idea. It can still involve very complex code, or complex debugging. Most who’ve used Apache’s mod_rewrite would agree, I’m sure.

In Martin Fowler’s book, “Patterns of Enterprise Application Architecture”, he points out that there is no reason not to have a system that uses both Front Controller(s) and Page controllers. There’s a trade-off, and like I’ve heard a thousand times in programming, it ‘depends’. On the requirements, the developers, the technology, and so on. But at least I have now consolidated what to me is the most significant reason for considering the Front Controller.

 

How much will 1 minute of exercise save the NHS?

Now IMO there’s a good question.

How hard is one minute of exercise? It won’t exactly take you long. About 60 seconds. Go up and down two flights of stairs perhaps.

Now say everyone in the UK did 1 minute more exercise per day than they already do.

I reckon for each minute of exercise that everyone added to their daily routine, the NHS would save in the order of 1 billion pounds every year. I’ve pulled that figure totally off the top of my head, but whether it’s 10 million, 100 million or whatever, doesn’t that make it worth doing?

I don’t vote, because I don’t think one vote can really make a difference. But one minute of exercise might. Maybe I can’t make other people exercise, but I can make a difference to my own health. And if other people read this, who knows, maybe our beloved NHS might stand a chance of surviving. Do we really want to go back to having to pay £15k for a one night stay in hospital? I don’t fancy my chances of staying eligible for affordable healthcare insurance when I reach retirement. I’m only 35, but it’s the one thing that really scares me about the future.

Now we shouldn’t expect everyone to exercise 1 minute more a day – some people cannot. Not just the wheelchair bound; I’m going to stick up for sufferers of mental health problems, to make a point: if one were planing on taking their life, exercise would seem pretty pointless.

But if there is a 10:1 ratio of well to unwell, then just an extra 6 seconds will make up for those who are unable. So, is 66 seconds so much to ask, to look after yourself, the NHS, and the unwell? I think not.

So: Go and do some exercise NOW! Just go up and down those stairs twice. Or 30 pushups, build up those abs and look sexy. Or dig out that awful bush in your garden that you hate. That’s 20 minutes of intense exercise right there! You’ll get a real buzz out of that, I guarantee you. Or do 45 star jumps. Just one minute. For Britain!

 

 

 

Lol @ Andrex

I had a few moments to spare and something on the back of a pack of Andrex rolls caught my eye.

On most household items these days, brands always try to make their product more exciting, easier to use, more attractive to the consumer, and where appropriate, instructions for use.

I always liked a good read of the back of a cereal packet while having breakfast. It doesn’t have to be much; at that time in the morning, even an ingredients list can be more than adequately stimulating. And quite frankly, I’ll also find myself reading just about anything while on the crapper.

But what I found on the back of this Andrex pack was surprisingly surprising:

IMG_20150801_132759

I mean, really, who on earth needs instructions for wiping their arse? If someone is capable of reading, then they should surely already know how to wipe their arse.

What’s even more surprising, is they seem to have missed out some crucial steps.

So maybe my assumption that everyone is aware of the steps in procedure was incorrect. I mean, if the manufacturers of toilet roll can’t get it right, then perhaps it would be a great public service if someone were to outline a complete procedure – so those looking to become proficient have a definitive reference. After all, mistakes could cause quite a mess.

So I’ve decided to take the liberty of preparing such a guide, which I will include in a future post once complete. It should be noted that while I’m not a professional, I do have a lot of experience.

If anyone has any suggestions on refining my process, please do let me know, so I can work towards a neater, more refined, more accurate, and more useful revision. I will then send it to Andrex – I suspect it will be well received.

 

 

Rear van accessory sockets: energize when doors open

I often do work in the load bay of my van, so I need a source of electricity there. I use both 12v and mains equipment, so I need to install a 12v socket and also a power inverter to get mains out of it. Then I can use battery chargers, power drills, a glue gun, laptop, maybe even a TV and kettle. Funky!

The solution: Lay a line to the back of the van, and install 12v sockets. I’ll have the sockets mounted in a metal box which I’ll fasten down. I hereby christen that box the “Auxiliary Panel” for reference.

What will it all involve? The core of it will be a 12v line to the back which will power the sockets and the inverter. There were a number of solutions, but only one seemed the right choice. Some ideas were:

  1. Add a single wire; one end to the positive of the cigarette lighter socket, then feed the other end to the load bay. For ground, attach another wire to the chassis in the load bay. Voila, these two wires I attach to a socket that I mount to a box and fasten in the load bay.
  2. As with option 1, but take the positive from the battery (via a suitable fuse located near the battery) rather than the cigarette lighter socket.
  3. The complicated option I chose. See below.

The problem with option 1 was the cigarette lighter circuits cannot handle more than 20A. This is fine for a low power inverter, or battery chargers, but not much more.

The problem with option 2 was that my auxiliary panel would have power 24/7, so if I were  to accidentally leave anything connected, the battery would drain and I’d be stranded, unable to start the van.

So option 3 takes the best of both worlds. It takes a feed from the positive terminal of the battery which gives it ample power. It also takes a feed from the cigarette lighter socket as a signal, which disconnects my box from the battery when the cigarette lighter socket has no power. (The cigarette lighter socket only has power when the key is in the ignition). All this needs is one relay (plus suitable fuses and of course a 12v socket).

And then I took this one step further. I also want my box to have power either when the rear doors are open, or when the key is in the ignition. This complicated things though. Not only does this entail an additional relay, but a number of diodes.

So, to follow up the last diagram I posted of my van’s interior rear lights, here is the modification which uses that signal from the interior lights (and the cigarette lighter socket) to energize the rear accessory sockets when the rear doors are open or when  the key is in the ignition:

circuit-to-switch-on-accessory-socket-in-load-area-when

RLY1 is the big boy relay. When either the doors are open or the key is in the ignition, this relay will be energised, thereby giving power to the auxiliary panel socket from the battery.

Getting RLY1 energised when the key is in the ignition was the easy part: the positive from the lighter socket could have gone straight to the relay coil. The complication is the doors. I need the relay to be energised when the doors are open – so when the door switches are closed, and therefore when the wire coming from them is held at zero volts. So I need RLY2 to flip this around: when the doors are open, their switches are closed, the wire coming from there is at zero volts – this energises RLY2, which then via D2, energises RLY1 with +12v.

D1 and D2 are needed so the cigarette lighter socket signal and the signal from RLY2 don’t interfere with each other. Without D1 and D2, having doors closed would actually energise the cigarette lighter socket! Plus all sorts of weird things could then happen. In theory only D1 is needed, but D2 makes things symmetrical and gives peace of mind if I expand the circuit at a later time.

D4 is (probably) needed to stop current flowing when it shouldn’t. If the doors are shut, (so their switches are open), no current should be flowing through the ceiling lights. But if RLY2 was closed when the doors shut, the relay could pull current through the lights. Weird things could happen.  So I thought I’d add it just in case.

I used a total of 4 fuses: one near the battery, one for RLY2, and one for the socket at the auxiliary panel. (The fourth is not shown on the circuit diagram: a 0.25A fuse at the cigarette lighter socket)

Voila. I now have convenient power in the back of my van.

For the sake of brevity, in the above diagram I omitted the aftermarket timer that was installed which turns the lights off after 30 minutes (when the doors have been left open). This is shown below. It doesn’t alter how the circuit above is connected.

 

van-rear-interior-lights-with-timer

Interior ceiling lights and rear door switches

This is basically how my ceiling lights in the van switch on/off:

Simplified circuit diagram of ceiling lights of van's loading bay interior, controlled by loading bay rear and sliding door ajar switches

The above is unmodified. Below are the mods I want:

  1.  Adding a third lamp. (done).
  2. Switching on the  accessory sockets in the loading bay when one of the loading bay doors are open, by attaching a wire to the circuit above, (between the lamps and the switches) to use as the signal to the loading bay’s accessory sockets. More detail coming…

Antistatic thing for vehicles – with style!


Basically: I was tired of getting electric shocks when getting out of my van.

I found some interesting stuff while looking for a solution. Scroll down to the pictures if you want to see what I ended up doing about it.

My Research and Theories

A lot of my knowledge on this comes from answers to a question I asked on Electronics Stack Overflow.

What NOT to do: the antistatic strips that hang from the vehicle on the road were actually banned from being marketed because they don’t actually achieve anything. If the vehicle was what stored the static electric charge, then the strip would be useful, since they do actually conduct. But it isn’t, so they aren’t. The reason is that vehicle tires do actually conduct enough to dissipate the charge from the vehicle to the ground. Not only do they have metal wire embedded in the rubber, but the rubber itself is partly composed of “carbon black” which is slightly conductive, but more than enough to discharge static to the ground.

So in short, the vehicle can be assumed to be at ground voltage/potential. I imagine other factors like air humidity and large surface area add to this conductivity between the vehicle and ground.

So where is the static stored? It’s stored in you. Where does the static come from? Most likely the friction between you and your seat. I still have research to do on this, but I’m satisfied that this is the source. Some of my reasoning is below:

I only get shocks when exiting the vehicle. At no point, while inside, do I get the shock. Admittedly this doesn’t explain why getting into the vehicle doesn’t result in a shock (either before or after brushing on the seat), but I don’t care, because my solution works. Although this is going to be a niggling question to be answered in a post some other day.

Solutions

Non-solution: If you skipped the above, note that anti-static strips that drag on the ground do NOT work.

Solution 1: This one is very simple, requires no modifications, just a few seconds’ thought when exiting the vehicle. Hold onto some metal part of the vehicle as you step out, until you are off the seat. It works because it discharges the static from you to the vehicle continuously as you slide off the seat; so the static doesn’t have a chance to build up into enough charge that would give a nasty shock. The problem is it’s not always convenient, especially if you don’t have any exposed metal frames on the inside of your vehicle’s cabin. But I’ve found that it does work most of the time. HOWEVER: it will NOT work if you only touch the frame *after* you’ve finished sliding off the seat. Since this is how we get electric shocks!

Solution 2: Spray your car seat with anti-static spray. This needs to be done periodically. I’m told it works well, but the spray isn’t that cheap. One bottle will cost more than the solution I chose below.

Solution 3: This is the one I have opted for, because I hate having to think repeatedly. (Remembering each time I get out of the car to hold onto the metal door frame). But it took some time to install. The advantage is that you can touch it even after you’ve exited the vehicle, since it will discharge your static slowly enough for you not to feel it.

As a minimum, you will need:

  1. A bit of metal, (a bare wire, or adhesive foil, or strip of aluminium – just about anything)
  2. A resistor, (electronic component) who’s resistance is roughly between 1MΩ and 5MΩ. (i.e. between 1,000,000 ohms and 5,000,000 ohms). EDIT: I sometimes still get a shock with the 1MΩ. I don’t know if I need to bump it up to 5M, or if the spark is bypassing the resistor, or if there is gunk on it (e.g. moisture/sweat) lowering it to something like 100k. I will try a 5M-10M resistor and see how it goes.

Then follow these steps to fit it:

  1. Physically attach the bit of metal to a convenient place you’d put your hand when getting out of the vehicle (screw, nail, glue, whatever). Just make sure it is NOT making electrical contact with the metal of the vehicle.
  2. Connect one end of the resistor to that bit of metal, and the other to the metal of the door frame. You can use any wire for that.

For the electrical connections in step 2, you can either solder them, or use electrical terminal blocks (the things with screws that you tighten on a wire), or just about anything you can think of that will ensure good electrical contact.

Then the fun begins: how to make it convenient when getting out of the vehicle to touch that bit of metal that is in series with the resistor. I used a combination of adhesive copper foil, and rigid copper wire. It was a lot of fun! Spent way too long on it. Looks cool though, “Bling” in a tacky way…pure style 😉

wpid-img_20150517_175135.jpg
Anti-static metal strip for vehicle door trim panel
wpid-img_20150517_175256.jpg
Anti-static metal strip for vehicle door trim panel

I had two areas covered by metal wire and adhesive foil strips. I had to take the interior door panels off to get behind and feed the wires through, connect them to the resistor, and connect the resistor to the van’s frame. But job done, it looks like a cross between Mr T’s neck and Frankenstein’s forehead, but hey, it works. I’m sure other you can find a prettier way of doing it.

Random facts about static electricity

I’ll share some info that fixed confusions I had while deciding how to stop getting shocks from my van. Static electricity can be confusing, since it seems to behave differently to other kinds of electricity we’re all more familiar with. Mains, batteries, and so on.

Static vs non-static

Electronics and electrical appliances and so on usually need at least two wires for the supply. For example, a mains plug needs a neutral and a live. A battery has a positive and negative terminal. Electricity will only flow when there is a connection that allows current to flow from the higher voltage (positive) to the lower voltage (negative). Like a stream of water, it will only flow if it has somewhere to go.

Static electricity on the other hand can be imagined as two pressure tanks (maybe imagine balloons) of water. If you get a static shock from something, imagine both you and that something are a balloon, one had no water and the other one was full with huge pressure. When they connect, the water will gush from the full one into the empty one, until the pressure between them has equalized. This normally happens very quickly, and can cause a spark (and pain!) My solution above makes it happen slowly.

So this explains why rubber shoes do not stop you getting a shock from your car as you get out: the electricity isn’t flowing *through* you, (e.g. via your shoes). It is just flowing from you, or to you. (Assuming your tires are the slightly conductive kind).