Hi,
I'm sort of new to PHP and I had a question about how exactly to structure PHP code. I am building a site to assist in a manufacturing process. The website has multiple different aspect like packaging, shipping, view test results etc... Right now I have created multiple files within each group. For example in the packaging section I have 7 different scripts. Within the packaging section I have been using the action field in a form as well as location.href to jump around the multiple scripts in packaging, so depending on what step of a process the user is in the URL link would be different. However I was recently told that my method is incorrect and I should have one page that controls the access to all other scripts in packaging. As an example I would then have a page called packagingContorl.php and within that file I would have a case statement for each step and then I'd still have the multiple files but packagingControl.php would decide which script to call using include statements. As I'm not familiar with the best coding practices in PHP any help would be appreciated.
Also on a side note I have been putting try-catch statements in all my database access classes and if an error pops up I send the error message to the catch block which will print the message out. However, I was told that having too many try-catch statements is also inefficient. Is this correct?
PHP code file structures
Moderator: General Moderators
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: PHP code file structures
It sounds like they way you are organizing things is fine. You may want to look into using one of the many frameworks around. They will provide some good practices for organization. If you want to code everything youself, then look into the Front Controller pattern.okhokhar wrote:Hi,
I'm sort of new to PHP and I had a question about how exactly to structure PHP code. I am building a site to assist in a manufacturing process. The website has multiple different aspect like packaging, shipping, view test results etc... Right now I have created multiple files within each group. For example in the packaging section I have 7 different scripts. Within the packaging section I have been using the action field in a form as well as location.href to jump around the multiple scripts in packaging, so depending on what step of a process the user is in the URL link would be different. However I was recently told that my method is incorrect and I should have one page that controls the access to all other scripts in packaging. As an example I would then have a page called packagingContorl.php and within that file I would have a case statement for each step and then I'd still have the multiple files but packagingControl.php would decide which script to call using include statements. As I'm not familiar with the best coding practices in PHP any help would be appreciated.
Program it how you think is right first. Don't try to fix theoretical performance problems. You only need to fix inefficiencies when they actually occur.okhokhar wrote:Also on a side note I have been putting try-catch statements in all my database access classes and if an error pops up I send the error message to the catch block which will print the message out. However, I was told that having too many try-catch statements is also inefficient. Is this correct?
(#10850)
Re: PHP code file structures
I agree, switch statements are hardly ever called for in PHP.
I would rather see a bunch of php files like
http://localhost/addUser.php
http://localhost/delUser.php
At least I can find the code I need
when you have
http://localhost/index.php
with 1,000 line switch statement in it, you are basically 1 step short of going back to the times of GOTOs and GLOBALs
Although maybe using an OOP framework like Zend would achieve what your friend was recommending with the switch statement, in a better way.
I would rather see a bunch of php files like
http://localhost/addUser.php
http://localhost/delUser.php
At least I can find the code I need
when you have
http://localhost/index.php
with 1,000 line switch statement in it, you are basically 1 step short of going back to the times of GOTOs and GLOBALs
Although maybe using an OOP framework like Zend would achieve what your friend was recommending with the switch statement, in a better way.