
My journey with PHP began almost two-and-a-half years ago. After programming mainly in Java for three years, I decided to switch over to PHP. I did this both to make myself more marketable and to do something interesting. During my time with this language, I have come to favor it more and more over Java, which was the first programming language I ever learned.
One of the main aspects I love about PHP is the how “loose” it is. Coming from Java, where Strong typing reigned supreme, typing
“$arr = array()” instead of “<type>[]array = new <type>[<length>]”
was a welcome change for me. Though I understand there are some pitfalls with loose typing, I still prefer it.
Going back to the “loose” aspect of PHP I love so much, we have default parameters and variable functions. These two features have augmented my programming skills substantially. They’ve completely changed the way I approach coding a feature or task.
For example, before being introduced to default parameters, my noobish self would write code like this:
function fooA($arg1, $arg2) { //Logic dealing with $arg1, $arg2 }
Me: Wait, there’s also a third argument that might be needed! I know! I’ll just copy the logic from my first function and add in logic for the third argument! GENIUS!
function fooB($arg1, $arg2, $arg3) { //Logic dealing with $arg1, $arg2 //New new logic for $arg3 }
Me: *pats self on back*
I would then proceed to call fooA when I only had two arguments and then fooB when I had the third argument.
Needless to say, the DRY principle was a foreign concept to me at the time. After being introduced to default parameters, my code now looks like this:
function fooA($arg1, $arg2) { //Logic dealing with $arg1, $arg2 }
Me: Wait, there’s also a third argument that might be needed! I know! I’ll set the the thirdargument with a default parameter and add to my function logic for the thirdargument should a value be passed! GENIUS!
function fooA($arg1, $arg2, $arg3 = <default value here>) { //Logic dealing with $arg1, $arg2 If(//Condition for $arg3) { // Logic for $arg3 } } Me: *slaps self on back*
Using default parameters has opened me to a new way of thinking when I write my code. However, I’ve also learned not to go too crazy with default parameters, or else I’d end up with functions like this:
Function foo($arg1, $arg2, $arg3 = <default value> , $arg4 = <default value>, $arg5 = <default value>) and so forth.
One thing I’ve thought about doing to circumvent this is to make one default parameter an array, which would contain any extra arguments I may need.
I’ve recently begun to use variable functions and cannot imagine how I coded without them. Before using variable functions, I would write code like this (written in code igniter framework):
function addData($data) { $this->db->trans_start(); $this->db->insert(data); $this->db->trans_complete(); } function updateData($data) { $this->db->trans_start(); $this->db->where(<key>,<value>); $this->db->update(data, “table”); $this->db->trans_complete();
Now using variable functions I’m able to condense these two functions into one:
function processData($data, $func) { $this->db->trans_start(); If($func = = “update”) { $this->db->where(<key>,<value>); } $this->$func ($data); $this->trans_complete(); }
By passing in the function to be used, I was able to condense the code use considerably.
As I continue to work more and more with PHP, I am excited for the opportunity to learn new tricks like the ones I’ve discussed here.
Questions or comments? Share them below, or tweet us @Pixafy!