firepages, that poses an interesting issue, don't mind me, I am naturally biased towards OOP, but let's look at it this way.
$file = new File("/www/$topic.page");
$oldcontents = $file->contents;
$file->contents = 'lala';
$file->save;
$file->close;
vs.
include("functions.inc");
dofile($file,$contents,1);
Like you said it qwill be hard to compare without seeing the code, and I am probably a complete when it comes to the theory of OOP, but I will give it a shot.
Let's start with the first example, it appears a new instance of the class File is instantiated and a private class var is created holding the path and name of the file to work with. So right there we cut out having to globalize any vars. So the method save() can use this variable and so can the rest of the methods without having to use global or haiving to pass like $file->save($file) because the class var is already present, it is about creating objects and then working with the objects. So in this case we create a new File object with the first property being the name and path of the file now we can use any of the classes methods to perfom tasks on this object like saving it or deleting it. This method seems to be a bit cleaner and easier to understand at least for me.
Second example:
The function dofile seems to handle everything and it seems to me that why even use a function at all, and in order to perfom multiple tasks on a singhle item, our function would need to have a lot of if...else... statements or a huge switch statement, so where does the overhead from the class surpass the overload of a crazy function with ten cases in a switch statement and a lot of extranaeous code.
PHP Code:
class File {
var file;
function File($file) {
$this->file = $file;
}
function save()
$fp = fopen($this->file, "w");
fputs($fp, $this->content);
fclose($fp);
}
function delete()
unlink($this->file);
}
as opposed to
PHP Code:
function dofile($file, $content,$action) {
switch($action) {
case "save":
$fp = fopen($file, "w");
fputs($fp, $content);
fclose($fp);
break;
case "delete":
unlink($file);
break;
case "showfile":
fpassthru($file);
break;
}
}
See where I am going with this?
Bookmarks