As a programmer, I use multiple computers. My desktop computer is my main device, while I use my laptop when on the road or when I need a change of scenery and want to work in a different room for awhile. One issue with this is it produces a problem of knowing which machine development files are on and creates work in manually copying/moving files between them.
PHP 5.4 which is set to be released in late 2011 has several changes and enhancements from PHP 5.3. Roughly, these changes are:
- Array dereferencing and notation
- Traits
- New and Deprecated Functionality
- Closure changes
- Upload Progress, Built-in HTTP server
- Miscellaneous Changes
A very useful addition to PHP 5.4 is array dereferencing and an alternate JSON-like notation.
Arrays
Dereferencing
You can now call a function which returns an array and immediately access values from it.
Today we will build a PHP application to automatically query multiple Amazon sites for a product's sales rank. The data will be cached locally in an XML file to avoid unnecessary remote calls. A screenshot of the working program is below. Here is a link to a live demo.
Full Source Listing. A zip file of all the files is available to download at the bottom of the page.
"drush is a command line shell and scripting interface for Drupal, a veritable Swiss Army knife designed to make life easier for those of us who spend some of our working hours hacking away at the command prompt."
The drupal project page of drush
The official site of the project is http://drush.ws
with version 4.5 the latest stable release and 5.x under development.
The jQuery Cookbook (2010 O"Reilly) is great, but with the rapid advance of the jQuery API it is already becoming outdated. The cookbook covers jquery 1.3.2 and as of the time of this writing, the current API version is 1.6.2. Similarly, the version of jQuery that ships with Drupal 7 is just 1.4.4.
Using jQuery as a javascript high level API is extremely popular. Compare the following stackoverflow.com questions tags as of today (August 11, 2011):
The internet has revolutionalized the way we share and obtain knowledge. Everyone with access to a computer and a desire to learn can regardless of world location, financial status, age, etc.
The Past
In the past only the richest or most well connected citizen s had the opportunity for higher learning. Libraries were private collections and nobility would have the best minds to tutor them. Even fifteen or twenty years ago, the world was a vastly different place. The best and most accessible ways to find out information about a subject were:
After years of using Firefox as my go to browser, I have made the permanent transition to Chrome. Reasons?
- More stable - less crashes and hangs
- faster startup
- Seemless, frequent updates
Setting up bookmark (and extension) syncing was a big issue for me. With a Google OpenID account as the signin mechanism, it is even easier to use than Firefox sync was. Another big must was good developer tools. After I got the hang of the builtin developer tools, I prefer them to Firebug.
I am still trying hard to get into a testing state of mind. I have written straightforward unit tests on easily testable code. I have installed the CodeCoverage plugin for Netbeans.
I have done quite a bit of background on how best to next proceed. These are my findings:
The book 97 things Every Programmer Should Know is a unique and necessary compilation of topics. Each of the topics is covered by a distinct expertprogrammer, which enables a broad and diverse spectrum of topics and best practices.
Because each topic is limited to a couple of pages, it is presented from a high level, often with no code at all. This is great as an introductory into areas and makes for somewhat light reading.
Fresh out of University, eager to start my career I was naiive. I was sure I would follow best practices. I had read great books by Martin Fowler on refactoring and Michael Feathers on legacy code. I was also sure that companies desired programmers to use best practices.
However, the harsh reality was and is that most computer places do not place an emphasis on best practices, code reviews or testing at all. Most managers and programmers are concerned more with the immediate overhead it creates and do not understand the usefulness when modifying code and debugging later.
PHP and Java are generally used for vastly different purposes. However they share a fairly common syntax based on C++. If you use both, here are some differences to remember.
Let's say you have a table of inventory items for an electronics store. The primary key is an integer id, and there are no other constraints on the table. Other columns are
type, brand, model, price, etc
For whatever reason, windows 7 comes with the keyboard shortcut 'left shift + alt' switching the keyboard language. It is quite easy to without knowing it, switching the keyboard from US to Canadian French for example and getting weird characters like french accented e's instead of a slash, '\', for example
To prevent this, open up the control panel from the start menu.
Setup Error Handling
After establishing your PDO connection, set the following attribute:
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
this will throw an exception on PDO errors. now, you can wrap everything in a nice try/catch block
Error Reporting Levels
In PHP 5, the default reporting level is E_ALL & ~E_NOTICE. While it is nice to hide the 'Notice' messages, it is better to set the error reporting level to E_ALL. In fact, since PHP 5.3, using E_ALL ^ E_STRICT provides even more detailed messages. Notice and strict messages tell you useful things like if a variable has not been initiated or a function is deprecated.
If notice and strict messages are hard to get rid of, this can indicate underlying issues with the program logic. For example if you had the code:
the singleton design pattern allows you to have one and only one instance of a class/object. it is good for large, shared resources like databases. <?php
/**
@author Brian Danchilla
@brief PDO singleton class
*/
class PDO_DBConnect {
static $db ;
private $dbh ;
private function PDO_DBConnect () {
$db_type = 'pgsql'; //ex) mysql, postgresql, oracle
$db_name = 'myDatabase';
$user = 'brian' ;
$password = 'myP@ssw0rd' ;
$host = 'localhost' ;
try {
Here is a very basic example. You have two tables TableA, TableB and you want to update a field, 'late', in TableA whenever TableA.student_number = TableB.student_number.
UPDATE TableA as a
SET `late` = 't' WHERE a.student_id IN
( SELECT student_id
FROM TableB
)
Code without tests is bad code. It doesn't matter how well written it is; it doesn't matter how pretty or object-oriented or well-encapsulated it is. With tests, we can change the behavior of our code quickly and verifiably. Without them, we really don't know if our code is getting better or worse.
-- Michael C. Feathers 'Working Effectively with Legacy Code'
(not using tests) I like to call Edit and Pray
-- Michael C. Feathers 'Working Effectively with Legacy Code'


All Articles