Skip to content

Trouble Recognizing Second Monitor Since Yosemite

I’ve been having issues getting my 2011 MBP to recognize my second monitor ever since the Yosemite update. Even the Detect Displays trick didn’t help. But, finally, I found one thing that worked for me every time: Command+F2 typed on the laptop keyboard (doesn’t work on external keyboard FYI) runs an apparently superior “Detect Displays” command, and like magic the second screen is recognized and starts working again. Found this trick on the end of this 6-year-old forum:[settings][page]=4#topic-reply-list UPDATE After this worked flawlessly for a week, it stopped working altogether. Now, I can’t get the monitor to be recognized no matter what voodoo I try.

Refactoring to Metaphors: Wiring Harnesses

If you have trouble starting to write testable code, you’re not alone. So many of the concepts are written about by brilliant coders (like Michael Feathers and Joshua Kerievsky), but they tend to write for other advanced developers looking to improve their work. If you’re just trying to get started down the road of testability, reusable code, and reliable changes, it can be daunting.

PHP 4 Constructors and PHP5 Namespaces Don’t Mix

Using a class defined in a namespace will disable a legacy Constructor. All classes in namespaces *must use* __construct. You might run into this problem trying to convert an old library to be PSR-2 (autoloader) compliant. If you want to use a legacy library in Laravel or Symfony, you’ll have to watch out for this.

Batching Inserts by Combining Queries

Obligatory warning: Always escape your inputs! This assumes you are migrating known safe data! I see lots of legacy code (especially bulk data migrations) that perform database operations inside a loop. Like this:

This results in a series of queries:

but we can do better. SQL inserts can be stacked into one query:

This still inserts all 3 records, in just one transaction. And it’s easy to retrofit our existing code:

A little more code of course, but this will speed up your inserts dramatically.

Tired of dealing with Leap Years? Use DateTime::diff()

Doing this the old-fashioned way was pretty complex. It was bad enough when a normal February would mess up your math, you also had to take leap years into account. Using the DateTime functions in php >= 5.3 lets you think about your business case, not calendars.

Don’t take my word for it! **Test it**!

For more info see the php docs:

Fake Multiple Inheritance

Leveraging code reuse in a world without mixins First, we need our interface. All the other objects conform to this interface, so they are all polymorphic.

The DiscountCart is the most primitive of the Discount types. It gives the customer a 15% discount on their entire cart. Inheritance.

The VolumeDiscount inherits from the DiscountCart, and overrides with its own discount based on the volume of each product in the cart. Inheritance with a method override.

There is another discount which adds free shipping. Inheritance with a property override.

Now, there is a very special customer type: the Frequent Customer. We give them a Volume discount AND they get free shipping all the time. If we had mixins, we could just “mix in” the free shipping, but since this is php (or C# for that matter) I do it this way:

This lets the FreeShippingDiscountCart be compatible with any IDiscountCart polymorphism and leverage inherited traits from its private objects. That way, if Free Shipping gets complicated (ex: drop-shipped or sale items are not eligible) I don’t have to re-implement the exceptions.

Add page slug to class list using wp_list_pages()

Add this code to your functions.php file.

Ordinal Suffixes in PHP

Continue reading Ordinal Suffixes in PHP

Counting Characters: PHP vs Javascript

On a recent project, an issue cropped up where we were exporting xml reports to a third party, who had rules about line lengths in Description fields. So, I wrote up a quick-and-dirty character counter for the description field so the client can keep their descriptions under the 1500-word limit. Here’s how it was done:

Now the html and javascript:

All was working well, except I underestimated the descriptions. The next morning, the thing was broken. The php side was kicking validation errors (“over 1500 characters”), while the javascript was counting a mere 1480. Why? Turns out the data was full of bullets and other multibyte characters. To make the counts match, I needed multibyte-safe php:

Hooray! Now the counts match!