Post by Bobby Hensley on Apr 10, 2009 10:14:01 GMT -5
Everybody has their own style of programming. Details from variable naming convention to function naming to OOP vs. Imperative, etc. One could say it's what makes each of us unique from one another.
So here's the trial. Write your own version of a simple hit counter. This isn't a test to see who's works better or anything remotely close to that. It's just a simple thirty second script that provokes various areas that show off your programming style.
Here's mine:
Now note that if I was actually to use the above, I wouldn't comment it so heavily unless I was releasing it. But on actual projects that's exactly what I'll do in terms of commenting. Though for larger classes I tend to make just a single, very large comment at the top defining everything in the file.
So here's the trial. Write your own version of a simple hit counter. This isn't a test to see who's works better or anything remotely close to that. It's just a simple thirty second script that provokes various areas that show off your programming style.
Here's mine:
<?php
class Counter
{
protected $num_hits;
protected $hits_file;
/**
* Assign the properties and increment the count
*
* @param string File to store hit count
**/
public function __construct ($file_name)
{
if (!empty ($file_name))
$this -> hits_file = $file_name;
$this -> num_hits = $this -> get_current_count () + 1;
$this -> update_count ();
}
/**
* Grab the current hit count
*
* @return function call Hit file doesn't exist
* @return integer Count property is already set
* @return integer Count isn't set, get it from the file
**/
public function get_current_count ()
{
if (!file_exists ($this -> hits_file))
return $this -> update_count;
if (isset ($this -> num_hits))
return $this -> num_hits;
return file_get_contents ($this -> hits_file);
}
/**
* Update the hit count file
*
* @return integer Boot back 1 incase get_current_count () called us
**/
private function update_count ()
{
$count_handle = fopen ($this -> hits_file, "w+");
fwrite ($count_handle, $this -> num_hits);
fclose ($count_handle);
return 1;
}
}
$count = new Counter ("counter.dat");
echo $count -> get_current_count ();
?>
Now note that if I was actually to use the above, I wouldn't comment it so heavily unless I was releasing it. But on actual projects that's exactly what I'll do in terms of commenting. Though for larger classes I tend to make just a single, very large comment at the top defining everything in the file.