FishSword Posted July 2, 2011 Share Posted July 2, 2011 Hiya! , I've been thinking long and hard about the best way to structure the code for a project I'm currently working on, to allow for easy development and management. Upon carrying out a search on the internet relating to this subject, it has been mentioned more than once, that it is a good idea to separate presentation code from logic (e.g. HTML from PHP) I am keen to obtain a better understanding on how you guys would code a simple page that displays information from a database and requires some sort of user input, whilst allowing for error messages to be displayed, if the user incorrectly completes what was asked of them. Also, I'm currently looking into the best way to link different pages together within my application. I've checked out some forum software (phpBB, myBB and smf) for inspiration and they all tend to link to different pages of the website via a main index.php file, containing a switch statement. Is this the best way, or are there other techniques that could be used? - What are the advantages/disadvantages of this approach? Of course, choosing the best structure to use at the beginning of a project is highly important, as this will then need to be adopted throughout the application. If you could provide examples of code or point me in the right direction, it would be much appreciated. Many Thanks, FishSword Quote Link to comment https://forums.phpfreaks.com/topic/240941-best-code-structure-technique/ Share on other sites More sharing options...
.josh Posted July 2, 2011 Share Posted July 2, 2011 Most people use a framework like Zend or something, and follow the MVC pattern. Here is a suggested place to start: http://framework.zend.com/manual/en/learning.quickstart.intro.html Quote Link to comment https://forums.phpfreaks.com/topic/240941-best-code-structure-technique/#findComment-1237599 Share on other sites More sharing options...
FishSword Posted July 3, 2011 Author Share Posted July 3, 2011 Hi Crayon Violent, Thanks for your reply. What happens when you create a pagination script for example? You will then be mixing HTML with the PHP? I'm not that keen on using a framework, as I'm afraid this would drastically increase the development time of my project, due to the learning curve involved. After all, am I correct in saying that using a framework is not a requirement, and not all frameworks may suite the project. Due to this, I rather keep things as simple as possible, but I would still like to know the best way of linking files together, as well as outputting error messages to the user if a user incorrectly completes fields that the page requires, whilst separating the presentation code from the logic. Cheers for you help! Many Thanks, FishSword Quote Link to comment https://forums.phpfreaks.com/topic/240941-best-code-structure-technique/#findComment-1237848 Share on other sites More sharing options...
vbconz Posted July 4, 2011 Share Posted July 4, 2011 Hiya! , I've been thinking long and hard about the best way to structure the code for a project I'm currently working on, to allow for easy development and management. Upon carrying out a search on the internet relating to this subject, it has been mentioned more than once, that it is a good idea to separate presentation code from logic (e.g. HTML from PHP) My $0.02 One of the basic ideas about coding database applications is to make it n-teir (where n is a number). Basically what that means is that you split the application development into distinct sections and code them that way. Normally a db application is coded as a 3-teir application. http://en.wikipedia.org/wiki/Multitier_architecture The three layers of coding are: 1 - Data access 2 - Logic 3 - Display By coding this way you protect yourself from several things: 1 - Data corruption (e.g. if changes to the display change some rules they wont hurt your data as the data and logic teirs wont let them through). 2 - Tie in to one architecture - coding display, logic and data seperately means you can change one of the layers without duisrupting other layers One example would be - I was tasked to work on a Microsfot Access DB project which had turned to custard (for a large govt organisation). When I got there I found they had forms that you entered data into. The forms had code behind each field which triggered workflow processes and also did checking to see busienss logic was adhered to. However, changes to the data and form layout meant they had to change the forms regularly and because the busienss logic was tied in with the form and layout that got messed up as well introducing bugs all the time. One example of the hassles was - field A contained data needed by field B. However field A got moved so it was after field B. On exiting field B logic would be triggered which required input from field A (which the user had not yet entered). Thus a logic error. My first move was to remove all the business / workflow logic from the forms and just let the forms act as a dumb display. Once all the data was entered into the fields, an action was triggered which involked the seprerate logic module and this performed its roles. This business logic then sent the information to the data teir which checked it was suitable and met database constraints before putting it into the system . Coming back to your question: Where possible seperate data, business / logic / workflow and display from each other. In general logic / busieness rules / workflow belong on the server after the data is posted back. this helps protect you from hacking (ie someone cirumventing your logic on the client side) and means you only need to change one file to enforce busienss logic. Where possible put validation / display message / display logic as close to the user (ie on the browser) as possible .This will save you server load by removing round trips to the server to check display and validation, as well and speed up the application by havng error messages etc display instanateneously in the broswer instead of wating for a posted form to receive a response. As for mixing html / php etc. Be pragmatic. The PHP is only going to runon the server side and the html will be needed to help layout forms etc. There are a lot of rules aobut what people SHOULD do but the reality is that Ive never seen a pure implementation of any system in any application. Do what is best for your application For me the golden rules are 1 - NTeir. Protect yourself against change and simplify your job. Think about either data, logic or dispay - noty all three at the same time. Seperating them out also forces pre-planning and focuses you on ther needs of each area forcing better practice. 2 - Security - code against the prats who want to screw you up, code against human kind who will find different ways to break your apps. Keep your data safe. 3 - Pragmatism - How do I get this application working as fast as possible for the user, with the least load on CPU, network, client PC etc. Apply coding rules and processes and then break them to make an application the best it can be. An example - Cods laws of databasse normalisation are aplpied at planning stage until a Db is normalised. Then the Db is de-normalised to make it faster and less complex. Best practice involves application of best standards and ideals and then breaking those rules to meet the real world. 4 - Dont get religious. People who say they wont code in xxx language as it is wrong / not pure / not the best way - often discard the opportunity to use a better tool. No one tools or system is the ebst. Use the best of everything available. The question is not should you mix php and html, the question is when should you and shuoldn't you mix php and html etc. Shane Quote Link to comment https://forums.phpfreaks.com/topic/240941-best-code-structure-technique/#findComment-1238049 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.