DIY PHP Dependency Injection in 10 lines
Here’s a use case for you: call a class method with a dynamic array of parameters, while ensuring the typing and presence of certain required parameters which may or may not be present in that array.
Hello, I'm Jonathon Hill. I build things for the web.
Here’s a use case for you: call a class method with a dynamic array of parameters, while ensuring the typing and presence of certain required parameters which may or may not be present in that array.
With Software Development topping 2014’s top jobs list, I thought I would share how much it cost me to become a web developer, and what my monthly expenses look like nowadays.
Why would you want to do this? My use case was a gallery application where we needed to deep-link to a specific gallery entry. Alas, the gallery would be iframed. Yes, iframes should be avoided, but sometimes in real-life you have to just deal with it.
Video from my Estimation Protips talk to the Atlanta PHP User Group in September. Slides and links here.
Since reading Phil Sturgeon’s post on PHP Static Analysis in the Sublime text editor, I have been experimenting with using phpcs and Sublime in general. Since I am currently used to the CodeIgniter coding standard, the time finally came today to try and configure my setup for that standard instead of PSR-2.
Estimation is a topic which is extremely important to running a successful software development company, whether you are a digital agency (like Brandmovers) or a freelancer working solo.
If you have ever written a PHP command line script and tried to pass a file to it using the POSIX tilde (~
) shortcut to reference your home directory, you may have been surprised to learn that the operating system does not automatically expand tildes in paths.
While evaluating the Disqus API for things like posting and flagging as a guest, I was baffled by this non-descript error:
Thanks to Benjamin Rojas, Andy Stratton, and a tip from Jasper, I was able to successfully send email from my home-brewed MAMP environment. Here’s the summary.
Writing unit tests for your code is widely regarded as a best practice. There are many excuses for not writing tests: time, cost, and the fun factor. Excuses aside, there are some Good Reasons to write tests for your code. I just discovered one today.
Employers looking for high-quality software developers are plentiful. At the January meeting of the Atlanta PHP user group Ari Waller shared some useful notes on what employers are looking for in 2013 (universally, not just in Atlanta).
My Multi-tasking in PHP talk at the Atlanta PHP User Group December 2012 meetup. Hope you can join us sometime!
So, you have A Big Idea that will Revolutionize The World. Naturally, people will flock to your site and engage with it. Naturally, you are concerned about whether your infrastructure can handle success. You wish to plan for success, it would be foolish not to, eh? Don’t let enthusiasm (or ignorance) lead you to premature over-optimization. After all, your Big Idea has not yet passed the test of time.
I was honored to deliver this presentation to the Atlanta PHP User Group‘s December 2012 meetup. Enjoy!
Any task that needs to be done more than once is worth automating.
Pre-packaged MAMP, LAMP, and WAMP stacks have been common on developer’s computers for years. Such packages are convenient because they provide a single-step install process, with all components in the server stack preconfigured to work together, and off you go.
Back in the “good old days” of MS-DOS, you could draw lines, boxes, filled areas (think progress bars), and more using the extended ASCII character set (AKA code page 437).
Update—Turns out, this isn’t actually a bug. Rasmus Lerdorf has already explained this behavior in response to a bug report:
Thanks to the guys at Engine Yard, I won a free pass to the Lone Star PHP community conference in Dallas, TX this year. Due to the choice of subject matter and presenters, I think I enjoyed this conference even more than last year’s PHPCon in Nashville. Here are some of the highlights.
(Save this, you’ll need it to install the certificate). To generate the keys for the Certificate Signing Request (CSR) run the following command from a terminal prompt:
<pre>openssl genrsa -des3 -out server.key 1024 Generating RSA private key, 1024 bit long modulus .....................++++++ .................++++++ unable to write 'random state' e is 65537 (0x10001) Enter pass phrase for server.key:</pre>
Enter a passphrase.
Now we’ll remove the passphrase from the key, so that you don’t have to enter this passphrase whenever you restart Apache:
<pre>openssl rsa -in server.key -out server.key.insecure mv server.key server.key.secure mv server.key.insecure server.key</pre>
I recently encountered this obscure error when trying to run an external program using PHP (which was bundled with MAMP Pro) on Mac OS X:
While working on a site that provides previews of URLs embedded in tweets using the awesome PhantomJS scriptable WebKit browser, and encountered difficulties when an URL shortener such as Bit.ly was used (as is almost always done when tweeting out a link to an interesting article or photo).
While on my day job over at Company 52, I encountered a textured background with a gradient overlay, using Photoshop’s overlay blending mode. I’m sure you’ve seen this effect before:
Today I encountered a bug that affects every version of Internet Explorer from IE6 all the way through IE9. Here’s what happens.
Building MySQL web applications with a team of developers will inevitably present the challenge of database schema changes. Any good web developer understands the importance of keeping all code under version control, but how many follow the same principle for the database?
Sometimes you want to do some sort of special processing in Javascript on mobile browsers, or vice versa. Here’s a quickie Javascript way to do that:
After three recent virus infections on Windows XP and Windows 7 (including at least one rootkit infection), I turned to Ubuntu Linux as a safer operating system. Two of the PCs were blessed with Atheros-based wireless network adapters, which are well-supported on Linux. The other laptop, a Dell Inspiron 2200, is blessed with one of those infamous Broadcom chipsets.
Did you know that PHP’s http_build_query()
drops any key from your query array if the value is NULL
? I wonder how many subtle API client bugs are caused by this behavior.
If you have ever touched a MySQL slave, you know that they can and do frequently halt. While sync problems can be caused by many things—network outages, schema changes, etc—one of the most common problems in a dual-master setup is primary key collision.
Phirehose is an awesomely useful Twitter Streaming API client library, written in PHP by Fenn Bailey.
When uploading a file to a PHP script on an Apache web server, there are several configuration options that if improperly set can get in the way. I just encountered yet another one of these, and decided to catalog them here.
While working on the Chrome version of a browser extension for a client of my company, I subscribed to the chromium-extensions Google group. I did this both for my own edification as well as for others’ benefit.
Get my day one takeaways here.
I’m here at PHPCon, the first PHP community developer conference in Nashville, TN. The first day consisted of two rather lengthy workshops, both of which were very informative.
Javascript’s window.onbeforeunload
event is useful for catching users that try to browse away from your page without having completed a certain action. Modern web applications such as Gmail and WordPress have made good use of this event.
I just got a really good education on how to make sure your (legit) email will navigate common spam blockers and be delivered successfully, thanks to Jeff Atwood.
I recently learned the hard way that <a href="javascript:void(0)">
doesn’t work as expected in IE6. The solution is to use the familiar #
but make sure your onclick
event returns false
:
I recently had to install the subversion client in a shared hosting environment (specifically Bluehost, but these instructions probably work with other web hosts as well). It goes like this:
I was getting this when trying to upgrade a plugin automatically within WordPress:
While working on an up-and-coming web service, I found that apparently Internet Explorer does not cope well with fading <table>
elements using jQuery. Here’s what I was doing:
If you’re getting an unreasonable “Expected Identifier” Javascript error on IE6/7, check to see if you have any variable names which are reserved words.
Jeremy Zawodny posted yet another great article over at Linux Magazine on some improved defaults that can save you a lot of grief when your network fails intermittently. In summary:
Parsing human names are not exactly easy, but they can be done. Keith Beckman’s nameparse.php
is an excellent PHP library for doing this.
Here’s the surprisingly simple solution to a fairly challenging problem. I do not understand why PHPs GMP extension does not include a gmp_not() function.
After some digging, I found a great way to convert number bases when dealing with arbitrary length integers (esp. integers > 32 bits):
Check out SNI (multiple SSL vhosts) and mod_proxy_balancer in this great article from Linux Magazine!
I got this question from a reader and thought it would be useful to post for everyone:
Here’s an excellent overview of cron, how to run a script every five minutes, and all that fun stuff.
There is a super-easy way to start a program during system boot. Just put this in your crontab:
From a presentation from Percona. This should be on every developer’s wall:
The single biggest annoyance of Windows XP (for me, at least) is the language bar. Seems like every time I reboot it’s back. Here’s how to fix it:
INSERT INTO…SELECT locks the table being read by the SELECT statement due to MySQL 5.0’s statement-based replication. Here’s a great post from the MySQL Performance Blog explaining the problem in detail and what to do about it.
I’m a big believer in readability, and I love to write code that is easy to understand. Check out my newly-posted MySQL Style Guide which covers:
Thanks to the MySQL Performance Blog for this tip:
I recently sat in on a live demonstration of a piece of software my former employer was evaluating. Here are some notes on what makes a great webinar or web demo.
Using register_shutdown_function() to do stuff on script shutdown requires special error handling. Normal error handling does not work within the function called, so if an error occurs inside your shutdown function you get this nondescript error:
This post has been moved to: http://nonstandardpolitics.com/blog/23
This post has been moved to: http://nonstandardpolitics.com/blog/18
Imagine my dismay and perplexity today when Apache suddenly stopped working on my development PC. Checking the Windows event log revealed this error:
In my quest to understand MySQL’s EXPLAIN statement and to learn more strategies for optimizing queries, I came across this excellent blog post from 20bits by Jesse Farmer:
If a MySQL slave encounters an error while replicating commands from the master, the slave will abort.
Sometimes MySQL doesn’t use the index on a column when performing a query.
tags
Until CSS3 is widely supported, if you want to wrap text inside a <pre> tag you can do it this way:
Got a shell script that you want automatically run at bootup on Ubuntu Server Edition? Here’s how:
Found a nice vi/vim cheatsheet, for all you Linux developers out there.
Ivan over at SimpleBits says:
hoverIntent is a plug-in that works like (and was derived from) jQuery’s built-in hover. However, instead of immediately calling the onMouseOver function, it waits until the user’s mouse slows down enough before making the call. Why? To delay or prevent the accidental firing of animations or ajax calls. Simple timeouts work for small areas, but if your target area is large it may execute regardless of intent.
This experimental plugin for the jQuery UI datepicker widget looks promising:
This post has been moved to: http://nonstandardpolitics.com/blog/20
To get the best locking performance out of InnoDB in 5.1, you’ll want to set the following options (“My Favorite New Feature of MySQL 5.1: Less InnoDB Locking” explains why):
This is beautiful! Check out this post on Bret Taylor’s blog:
Below is an excerpt from Issue 56 of the Sitepoint Design View newsletter that I found very interesting and useful. Perhaps you will too.
Here are some observations I’ve gathered from writing a variety of programs in PHP over the last year. Do you have one I should add? Please share it and post it in the comments.
When you spend your days staring at PHP code, you really can appreciate a visual refresh now and then. Syntax highlighting, if done well, makes reading code much easier. Excellent color schemes are not only functional, but are visually appealing as well.
Here’s a really cool library which makes it super-easy to generate the xml needed for AmCharts flash graphs in PHP.
XOR (exclusive OR) is a boolean operation, like &&
and ||
, but with the following logic: It is true if the expression on either side is true (like ||
), but not if both sides are true (like &&
).
I couldn’t resist this comical shot of my friends furry companion as he worked away on his MacBook Pro.
Tuesday I stopped along the way as I was approaching Birmingham at a Chick-fil-a for lunch. Some creative florist had furnished all the tables with these flowers. They are not fake:
Tonight I had need access a remote server through the excellent Fog Creek Copilot service. Copilot supports Windows 2000 and later and Mac OS X, and I’m not running Windows anymore…
So far so good on my experimental switch to Ubuntu Linux from Windows XP.
My two-year-old HP laptop is getting slower and slower. Time for an XP re-install, but that’s a big ordeal. I opted to give Linux another try (maybe the third time will be the charm).