Tuesday, July 28, 2009
Saturday, July 25, 2009
php method overloading
Saturday, July 25, 2009
with every revision of php the php developer's lives get a tad easier, and our work a bit more interesting.
php 5 has a ton of new features that help keep php as a relevant web development tool. one of the most notable new development in the language is the ability to overload a method. this provides a measure of flexibility that can reduce the chore of coding the main application.
an application i was working on for a recent employer used a concatenated key for the job ID; (simplified) it was comprised of project ID and location ID. if the project ID was 1101, and the location number was 5054, the job ID would be 1101-5054. Sometimes when calling the method you had the job ID, but other times you had the project ID and location ID as separate pieces of information. Now, to concatenate the project ID and location ID into a job ID is no big deal, nor is exploding out the job ID into a project ID and location ID. but it is an extra step. and the more complicated the key, the more extra steps are required. by overloading the method, you need not worry with concatenating or exploding to get the correct variable to pass to the method, you just pass whatever you have! here's how:
this simple approach can be used to handle many complicated issues and can ease the burden of calling specific methods.
php 5 has a ton of new features that help keep php as a relevant web development tool. one of the most notable new development in the language is the ability to overload a method. this provides a measure of flexibility that can reduce the chore of coding the main application.
an application i was working on for a recent employer used a concatenated key for the job ID; (simplified) it was comprised of project ID and location ID. if the project ID was 1101, and the location number was 5054, the job ID would be 1101-5054. Sometimes when calling the method you had the job ID, but other times you had the project ID and location ID as separate pieces of information. Now, to concatenate the project ID and location ID into a job ID is no big deal, nor is exploding out the job ID into a project ID and location ID. but it is an extra step. and the more complicated the key, the more extra steps are required. by overloading the method, you need not worry with concatenating or exploding to get the correct variable to pass to the method, you just pass whatever you have! here's how:
function activateJob (); {
$a = func_get_args();
if(count($a) == 1 {
$jobID = $a[0];
} else {
$jobID = $a[0] . '-' . $a[1];
}
// now $jobID holds the actual job ID
}now you can call job ID two different ways. if you have the full job ID, just call it:activateJob($jobID);but if you only have a separate project ID and location ID, you simply call it:
activatejob($projectID, $locationID)and the method figures out what to do with it!
this simple approach can be used to handle many complicated issues and can ease the burden of calling specific methods.
Wednesday, July 15, 2009
Subscribe to:
Posts (Atom)


