Hi, I feel that I'm going crazy. I'm trying to write everything as a class and I don't care for simplicity! I know that it's bad, but it seems that I hate functions and I can't work with them anymore! You know, I think we can not do much things with classes in a php script, coz the glue part of the program (the part that should take the $_POST vars and do the header stuff and ...) makes a great part of the whole script, but I'm still trying to write as OO as possible.
For example for a simple image upload program, that needs to get an image and a description from the client and put it in a db, I wrote 2 classes, that one of them inherits from the another. Here are my 2 classes:
class status_admin_utils
{
$returned_messages = array(
"method_1" => array(
"success" => array(),
"failure" => array()
),
"method_2" => array(
"success" => array(),
"failure" => array()
)
.
.
.
.
function status_admin_utils( $some_arguments )
{
# Some assignments
}
function read_db()
{
# reads the db
# put all the success/failure messages inside the $returned_messages["read_db"] array,
# that I can output output to the user (if I want) after running the method
}
function write_db()
{
# write into db
# put all the success/failure messages inside the $returned_messages["write_db"] array,
# that I can output to the user (if I want) after running the method
}
function manage_uploaded_images( $images, $descriptions, $dir_to_upload )
{
# copy the images into $dir_to_upload and then write their address/description
# into the db.
# put all the success/failure messages inside the $returned_messages["manage_uploaded_images"] array,
# that I can output to the user (if I want) after running the method
}
function delete_records( $records_ids )
{
# Perform the same thing, and puts the relative messages in $returned_messages array.
}
}
Ok, you might say that it's ok till now, but the problem is that I need to show the contents of the db to the users too. I t means that I have to write all the records to the browser and put a check box for each record. After the user checked some of the checkboxes and pressed the delete button for example, I have to get the id of those records ( that are the values of the checkboxes) and then delete them. So, I'm trying to do this with classes! I mean I show the contents of my db to the client by means of a class and I make all those checkboxes and their values by a class, then when the page is loaded, I check if $_POST vars is full, if yes, it means I should parse it and call the appropriate method. How do I do that? See:
class status_admin_manage extends status_admin_utils
{
function status_admin_manage($tableName)
{
# This is the name of my database table.
$this->tableName = $tableName;
}
function show_selectable_records($action_page)
{
?>
<form action="<?=$action_page?>" method="POST">
<input name="to_be_used_by" value="<?=$this->tableName?>">
.
.
.
.
<input type="submit" name="to_be_used_for" value="delete"><br />
<input type="submit" name="to_be_used_for" value="edit">
</form>
<?
}
}
So, now in the action page (where my form sends its data, it might be the current page too), I check for $POST["to_be_used_by"], if it has the name of my database table, I instantiate an object of the previous class, then I check for $POST["to_be_used_for"], so, for example if it is "delete", I call the delete_records method of my class. Writing like this, allows me to have multiple classes in one page, and I can always call the right class->method. So, what is your idea? Is it good or bad? I'm almost doing everything inside of my class, and I think I reached a kind of style for myself! If I write all my classes like this, then I'll be able to work with them even if 100 years passes! I don't need to read the glue part of the program to find out what variable I should use or what method I should call!
Thanks for your comments,
bijan