PHP Classes

Strider’s example is pretty valid. I think the largest appreciation of OOP comes in the frameworks and module creation.

One of the projects I worked on, for a company that just wanted me to catch them up in development, is a file upload script. The module existed in the resource directory and could literally be called by any other script. You just passed in the parameters through its constructor.

So, if it was for the helpdesk, you pass in where the files needed to reside for helpdesk. Need it for something else, pass that location in. You could also pass in a constant that said, I only want to allow images, or documents, ect.

It was extremely versatile. Now, you could do that with procedural style, but it would have made the function break development rules, a function should only do one thing and do it well.

Kevin, I am pretty sure you have a number of pre-built function written? How do you orginize them? By what they do? That is what classes do. Think of the PHPMailer library. It has the classes needed that create and send the mail, nothing more. Or PDO, it doesn’t do string manipulation, or math calculations, it only deals with the database.

I have all the functions I use in functions.php and include that file in the global config. Since you mentioned an uploader, that rang with me. The current project I am doing currently has two different upload files, one just uploads images, and path to DB, other inserts csv data. I could see how one upload class would be better for me that can handle either situation. Much of the code in both files is exactly the same. It would start getting messy to separate the duplicate code and then include it back. Excellent point on a function doing one thing well.

I will check out some upload classes and see what I can come up with. Dont know enough OOP to know if I make it right even if it works.

  • Edit: As I think about it, uploading images and uploading/importing CSV data really are not the same thing. Wouldn’t I want a seperate class for each one or would I do that Extends thing?
Sponsor our Newsletter | Privacy Policy | Terms of Service