Non-abstract method Foo::bar() must contain body

I was working on one of my PHP based projects, and I was getting the following error:

Non-abstract method Foo::bar() must contain body in Foo.php on line 10

The problem was that I had accidentally put a semicolon at the end of the first line of the function:

WRONG WAY:

class Foo 
{ 
    function bar(); // extra semicolon! 
    { 
        // stuff 
    } 
}

RIGHT WAY:

class Foo 
{ 
    function bar()    // no semicolon, yay 
    { 
        // stuff 
    } 
}

Letting the database to the heavy lifting: Capitalization via SQL

Years ago I had the pleasure of meeting Ben Forta and listen to his thoughts on web development. One thing that stuck with me was his statement that:

“Dynamic programming languages like PHP and Coldfusion are great, but developers need to leverage the database more. Allow the database to do it’s job, the heavy lifting”

With that being said, I was recently working with a database that contained users first names. The data was not snantized at entry, and contained a mix of lower cased (ex. josh) and upper cased (ex. Josh) first characters in the firstname column. I needed to always present the users first name in a capitalized format.

The first solution that most developers would reach for is dynamically parsing the users first name, then upper-casing the first character. This works, but in my opinion isn’t the most elegant solution.

With a simple SQL statement, like the one below, we can return the users first name already in a capitalized state.

SELECT (UPPER(LEFT(firstname,1))+SUBSTRING(firstname,2,LEN(firstname))) AS firstname 
FROM users
It’s quick to implement, its easy to work with, and it’s also very fast on the performance scale.
Yes, I understand that there are cool convenience functions in like ucfirst() in PHP, but I’m a lazy programmer and I don’t want to have to remember to use ucfirst() every time I’m dealing with the users name. I’d rather get the information in the correct format the first time, then remembering to groom it every time before I use it. Set it and forget it.

Detecting Character Encoding In Coldfusion

Most of the time I only work with UTF-8 or UTF-16 character encodings. With that said, recently I was working on an old coldfusion project. Do to database restrictions, required all inputs from the user into the application to be Latin-1 (ISO-8859-1) character encoded.

Coldfusion does not have a good way of checking the character encoding of a string, so I had to pull out some of my Java skillz.

Dropping into Java from ColdFusion is a lot easier then it sounds. Check it out

<cfscript>   
   //use this to test character encoding. We only want Latin-1
    encoder = createObject("java", "java.nio.charset.Charset").forName("ISO-8859-1").newEncoder();
 
    if(encoder.canEncode(testVar))
    {
        //its Latin-1
    }
    else
    {
        /NOT Latin-1
    }
</cfscript>

The Java layer of Coldfusion is very powerful and under utilized by many developers.

If you want to see some true ColdFusion magic, check out the ColdBox Framework, http://ColdBoxFramework.com

PHP and cURL in parallel

I was recently working on a php based project for a client. I needed to pull in content from 10 different URLs, then process that data into something useful.

I have a growing love affair with cURL, so it was my natural selection for the project.

In my first approach, I looped over the list of URLs, sent the cURL request, wait for the response, stored the data. repeat until done.

That worked, but it was very slow process. The page was taking between 30 – 45 seconds to load. That doesn’t sound long, but in terms of web applications, that’s an eternity.

I tried several tricks to speed the application up, but the bottle neck was the cURL calls. Each call was done in a synchronous manner. Every call to cURL had to be completed before the next could be made.

After doing to research on how to speed this up, I came across the following php cURL functions:

curl_multi_init();
curl_multi_add_handle();
curl_multi_select();
curl_multi_exec();
curl_multi_getcontent();
curl_multi_info_read();
curl_multi_remove_handle();

Using these methods allow for cURL to send asynchronous requests, solving my pervious problem.

A little more searching, and I found an awesome wrapper library that takes the guesswork out of the using the curl_multi methods. “ParallelCurl” https://github.com/petewarden/ParallelCurl

The sample code provided with the library was very straight forward and super easy to use.

After using asynchronous cURL calls via the ParallelCurl library, I was able to reduce the page load time from 45 seconds to 15 seconds. It’s still slow, but it’s a HUGE improvement, and it makes the application usable, and reduces load on my server. It’s a win-win-win situation!