Coding Critique is the place to post source code for peer review by other members of DevNetwork. Any kind of code can be posted. Code posted does not have to be limited to PHP. All members are invited to contribute constructive criticism with the goal of improving the code. Posted code should include some background information about it and what areas you specifically would like help with.
Popular code excerpts may be moved to "Code Snippets" by the moderators.
It doesn't strictly require that you move away from procedural. You certainly need to learn how to keep your presentation separate from your business logic though.
OOP helps in that respect, but it's not a requirement.
That simply means that you should separate your presentation elements like the HTML output from any behavioral elements--the code logic. Retrieve the information using a separate PHP file called something.inc.php or similar.
Don't take that too literally, however. If something needs to be displayed, it needs to be displayed, but you shouldn't have any particularly long statements or any need for deep nested if-statements in your display file. Clean code means less clutter and more organization, and the cleaner your code, the easier it is to modify.
So the code below really does not need to be displayed / could easily be an include. Is that what you're saying? Also, would this be a good piece of code to start learning oop with? I feel it's pretty straight forward.
That would be an.. okay place to start. But then there's issues of what class(es) it would become. One for handling your images, one for handling your files, or one of each working together?
psurrena wrote:Ideally it would be one of each working together.
Any idea how much control you'd give to either? The issue I was addressing was that either of them could be structured to be able to handle that entire function, so either you choose one, or somehow try to split it up.
psurrena wrote:If I understand correctly, almost total control. The only variable would be the path.
I mean that particular function if broken up into being performed by two classes would need each class to perform certain parts of it. Which one would do more? Would the image class only define the image path and filename and the file class handle the rest, or would the file class only handle retrieving the file and the image class do the rest?
For my purposes now, naming of the file is specific as to control the order. So the images are 1.jpg, 1t.jpg, 2.jpg 2t.jpg, etc. What makes sense to me is, the file class would only to the retrieving and image would do the rest.
psurrena wrote:For my purposes now, naming of the file is specific as to control the order. So the images are 1.jpg, 1t.jpg, 2.jpg 2t.jpg, etc. What makes sense to me is, the file class would only to the retrieving and image would do the rest.
Makes sense. I'd get started on it. And try to leave some elbow room for later on, as you'll likely re-factor as you discover new things that you'll want to be able to do.
psurrena wrote:One last question, and I don't mean to drag this on. Why would I use two classes over one class with two functions?
No one said you have to use two classes, you said you'd use two.
The whole purpose of classes is a separation of duties, so to speak. Every object is a worker on an assembly line to serve your data, and each of them has a task to perform and the section to handle.