A Single Article
Read it, comment, and share it with your friendsNever forget your semicolons
I’ve been working on a PHP script this weekend and it’s my first time writing PHP with a program that is actually designed for writing code (I’ve been using Wordpad and Notepad entirely until now). It’s also my first time doing serious work with functions and revisiting a lot of the coding that I did in 1st and 2nd level Java programming.
For one thing, half my errors have been forgetting to put semicolons at the end of a statement. It’s such a noobish mistake, worse than forgetting to close html tags or css blocks. It always tends to happen when I use echo… maybe because PHP is so simple that I get careless. At least I’ve learned to recognize when an error is due to forgetting a semicolon. And now that I’m using a nice editor, it doesn’t take me years of hitting the down-arrow to find the line it’s on.
Another thing I’ve found is that PHP doesn’t support re-declaring a method with a new number of arguments. I forgot what this is called, I was always able to do it in Java, but with PHP I just can’t do:
foo () { do something...; }
foo ($arg) { do something different with this argument; }
foo ($arg1, $arg2) { $arg3 = $arg1 + $arg2; foo($arg3); }
That was rather surprising, and it illustrates some of the shortcomings of PHP that I’ve read about. I had to do something along the lines of:
foo () { do something...; }
footwo ($arg) { …; }
foothree ($arg1, $arg2) { …; …; }
Not so cool, since it’s harder to keep track of all those functions. Come to think of it, does anyone remember the name of the technique? Something like “overriding methods.”
Get a Trackback link
4 Comments
Responses to my articleYou’d think having been working as a web developer for over a year now I’d a) not still be making the same silly noobish mistakes and b) know what you’re on about with your overriding methods thing.. but no.
Sorry.
Here’s how to do it in Php:
http://www.php.net/manual/en/functions.arguments.php
There’s actually at least two different ways to accomplish this with Php. With “Default argument values” it goes like this:
function foo ($arg1=NULL, $arg2=NULL)
{
if (is_null($arg1)) …
}
Thanks so much! It will definitely help me with my code.
The other approach is to always pass one variable, namely an array of numerous values. So:
function ($array) { …
$array can obviously contain any parameters and you can act on the size on the array:
switch (count($array)) {
case ‘1′:
break;
case ‘2′:
break;
etc. Note you would probably wany more checking on the array before you start but I’ve never really found the lack of overiding in PHP a problem.
Leave a comment
Share your thoughts with the worldYou can use Markdown, or you can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>Please keep comments respectful and on topic.
This form is guarded by Akismet, so don't waste your time trying to submit spam. It won't work. Ever.