Archive for the ‘Nostalgia’ Category

Yes, Virginia, There Is Such A Thing As A Stupid Question.

Tuesday, May 8th, 2012

In reading the Eric Raymond’s classic essay How To Ask Questions The Smart Way I found proof that, no matter how kind and gentle, now matter how considerate and co-operative we want to be, no matter how much we want to nurture participation and camaraderie, there really are stupid questions.

The Singleton Design Pattern in PHP

Thursday, May 3rd, 2012

The Singleton design pattern creates a class that can only have one instance. This is useful for tasks such as creating a globally available database connection for your script.

The prototype for the singleton class, in PHP, goes like this:

Singleton Pattern

class ClassName {
    static $instance = NULL;
    private function __construct() {}
    private function __clone() {}
    static function getInstance() {
        if(self::$instance === NULL) self::$instance = new ClassName();
        return self::$instance;
    }
}

$obj = ClassName::getInstance();

It’s All About Scope

PHP variables have scope restrictions that prevent them from being visible in all parts of your program. We often need these variables inside classes and functions. One way to provide globally accessible variables is to pass them into classes or functions as parameters. This method makes it clear that the function of class method is dependent on an external variable or object. Here’s an example:

(more…)

Windows Newlines Will Kill Your Linux Scripts

Tuesday, June 21st, 2011

Here’s a little something I give to my online Ruby and Python students every year.

(more…)

Hotlinking newbies and how to stop them

Friday, May 20th, 2011

A human generation is 33 years, a dog’s 7, a mayfly’s 24 hours. I figure that a netizen generation is about 3 years. I mean, every three years or so, a new generation of clueless users crashes into the china shop and starts smashing things up.

It goes like this. Some newbies get hooked on the web, they get a blog/website and they start foraging for content to put on their blogs, which usually means copying other bloggers’ images, jokes, and downloads. The easiest way to add content, it turns out, is to borrow other people’s stuff. The Web is a cornucopia of free stuff, to which it’s easy to apply the old Hippie mantra, “Hey, let’s share your stuff.”

I’m not saying that all newbies are malicious. I’m just saying they they’re clueless about web etiquette. Instead of saying, “Hey, go visit so-and-so’s blog and download that cool thing”, they’ll say, “Check out this awesome download”, and put up a link to your resource.

If that link leads to a 12MB zipfile, they’re giving away 12MB of your bandwidth while they borrow your content to build their site. This shabby practice has been around since the beginning of the Web. Fortunately, there’s a solution for it: our old friend, the .htaccess file.

If you want to stop hotlinkers/poachers, put something like this in directories that hold your precious resources.

# Turn on the rewrite engine (to rewrite the URL)
RewriteEngine On
# If the referring site is not YOURDOMAIN
RewriteCond %{HTTP_REFERER} !^http://(.+\.)?yourdomain\.com/ [NC]
# Or the refering domain is missing
RewriteCond %{HTTP_REFERER} !^$
# redirect the request to the hotlinking is not cool
# image ON  ANOTHER SERVER. Thisis a must
# to avoid a "too many redirects" error.
RewriteRule .*\.(jpe?g|gif|bmp|png|zip|pdf|mp4a|mov)$ http://yourdomain.com/images/hotli
nkingisnotcool.jpg [L]

If you’re at a loss to create your own custom image, check out these rude examples. Oh, and don’t hotlink to them!

Learning PHP by osmosis

Wednesday, May 11th, 2011

Kids say the darndest things. I’m talking about 25-year old guys who end up in my PHP class. One student—I’ll call him Werner—was having a hard time understanding the concept of True and False.

“So, tell me what programming languages you know,” I said.

“None,” he said. “But it’s not a problem. I can learn anything.”

“How’s that?” I asked.

“I have a special knack for learning. I can just absorb knowledge. I can walk into a room where people are speaking a foreign language and an hour later I can carry on a fluent conversation.”

“That’s a gift,” I said. “What languages have you learned?”

“Farsi, Greek, Mandarin, German, and Italian, all in one summer when I was traveling through Europe.”

“Say something in Mandarin,” I said. I happen to know a little Mandarin.

“Oh, that’s not necessary,” Werner said. “It’s something I can do in the moment, when I’m in the flow of the conversation. But I’m having trouble with this PHP. It’s not coming to me yet and it’s bothering me. Really, I should be on top of it. I have so many great ideas for making money, if this PHP would cooperate. Maybe it’s something you’re not doing.”

“I don’t get it.”

“I learn by osmosis, like I said. If I’m in the room with you, I’ll absorb everything in your head. My roommate works at Yahoo and he’s always hacking on PHP at home. That’s how I learned PHP, by being there when he’s hacking. I think you must be blocking the osmosis.”

“Got it,” I said.

He dropped the course after a month of waiting for the osmosis to kick in. I saw him a couple of years later. He was taking an intro Java course.

“I figured out that it wasn’t you. Osmosis doesn’t work for programming languages,” he said. “So I’m doing it the easy way, just taking courses. Anyone could do it.”