Ok, it might be a banal question but i'm a little bit confused.
I'm going to develop a project by my own. This means i'll build the HTML template, the PHP scripts, MYSQL query, AJAX calls and CSS styles.
This means no other people will touch any part of the code.
I know templates are good ways to make the entire application easy to be modified. But is that so necessary to do it? Since i'm the only one who got to know what that files and what that page do?
Using common patterns in the development of software products is not only about making it more readable/maintainable but also about making it extensible. The longer your software will be in use, the more features will be requested/desired. Extending one big mess of a software will not work out in this case.
Try a php framework (e.g. Zend Framework / Symfony(1/2)), there are lots of tutorials which allow you an easy start with or without templating engine (I use twig atm, which does a great job!).
If i were you, i wouldn't use templates (do you mean PHP frameworks?) but thats just me.
You don't have to use a template engine - PHP itself can do that just fine.
In general, it's a good idea to "separate concerns" (eg. code which outputs HTML is separate from code which loads data from the database) - it improves the readability and organization of code, making it easier to maintain and faster to develop further.
You should have a look at a framework, like symfony. Most frameworks make use of MVC and hence provide a good separation of your code.
But is that so necessary to do it? Since i'm the only one who got to know what that files and what that page do?
Yes, if you want to keep it maintainable in the long run. If you are not constantly working on the code, you will forget how some things work eventually and then you are happy if you have a proper separation and organization.
Related
I used to develop websites using php . I like to learn some framework using php.
So I like to know how effective MVC is ?
Is this easy to learn ?
MVC is a way of organizing code that seems uniquely well suited for web applications. You'll have to organize your code in some particular way, try MVC and see if you like it. It's also the de-facto standard in web app design, so it makes your code easier to understand for other programmers.
It helps me minimize mixing languages -- views contain all the HTML, models all the SQL, and controllers describe and handle the API and support code (like authentication).
I have to say that when I first tried to get my head around MVC I had a great deal of difficulty (especially being someone who is self-taught and started with PHP). Put most simply, and most valuably for me, MVC is a good way to organize your code. It provides a template for separating the different layers of your application, which again sounds confusing, but actually isn't. (Again, this is meant to be an explanation of why MVC is useful and how to get started with it, not how it functions ... which I don't totally understand.)
First off, one really simple way to think about it is to compare it Wordpress (this is assuming you've played with Wordpress). Views function in much the same way as "themes": They are a simple way to combine presentation markup with whatever data is being pushed out to the page.
Models describe (and can interact with) the database.
Controllers do everything in between. (Calling functions in models, loading data into views.)
But it's also important to note that you can use MVC in any way you want to. While the idea is to get you to use a specific organizing pattern, no one is going to stop you from doing it in whatever way works best. I know quite a few folks who forgo models altogether and just use Controllers/Views to do everything they need. I found CodeIgniter to be pretty easy to get up to speed on, and now that I've got the hang of it I find it incredibly useful for both the functions the framework provides, but more importantly, the organizing that MVC forces me in to.
Hope that's helpful.
MVC is a very good design pattern for developing applications. It allows you clear separation between the views, the data access and processing logic which will result into more maintainable code.
Obviously learning MVC is going to be different for each person. My recommendations would be to read everything you can on MVC/PHP-MVC/PHP OOP that you can and then try to write your own framework. Then rewrite it using the things you learned the first time. Repeat.
Few of the major PHP frameworks implement MVC. Most use some variant of "Passive-MVC" or "Model-View-Presenter". They're following the concept, not the actual MVC pattern (which is a runtime organization for graphical apps, not for generating page output).
That's not to say the PHP interpretation of the concept doesn't bring any benefits. It's commonly believed to provide a better structure for large or growing web apps. It's less useful for implementing simple tasks.
I guess everything that was to be said about MVC was said already... so I'd like to point you to a good PHP framework to start with, should you decide to go with MVC - CodeIgniter. It's probably the easiest one to start with if you never used MVC before.
Just to add to all the wonderful answers given about MVCs, an MVC framework will do all the basic work for you and it helps save time. You'd be working on things custom to a particular project like its business logic intead of the basic Create, Read, Update and Delete functions.
#gowri
You can try with any php frame work. You are at initial stage so start learning codignator or cakephp. Both have good documentation and support. I recommend you codignator. Easy and good mvc.
I have been writing a lot of code for work in PHP/MySQL. So far it has all been procedural making use of functions for functionality occuring multiple times/places. Starting to find some of the site hard to manage - time to go OO.
I want to learn about MVC with object oriented PHP & MySQL. I have some experience in Java and MVC but never anything to do with web technologies, i.e. HTML/CSS/JS etc. I don't really understand how the dynamically generated HTML fits in with the classes etc.
I am after some recommndations about where I can start. Ideally some sites with great examples from the ground up. I don't really want to use a framework at this point because I find that it does too much for you. Once I understand the OO approach with MVC I'll probably use a framework to managei easier.
Cheers,
Evan
You have a lot of code, and despite being hard to change, it probably works. Making an overhaul is going to set you back a while, so the best approach is incremental. Find some ONE thing that would seriously benefit from using objects, and use objects there. Refactor as you are able to run tests. You can work this in with the ordinary flow of events, and things work out OK.
Frankly, if you're serious about removing redundancy and duplication, you'll often find places where just writing a small function can make a dent. If you do this often enough, you'll find groups of functions that work on the same data. That will suggest where to look for objects.
In other words, if you listen with the right kind of ears, the code will tell you.
If you just want a good online course, maybe you should have a look at
http://www.lynda.com/home/DisplayCourse.aspx?lpk2=653&srchtrk=index%3A1%0Alinktypeid%3A2%0Aq%3APHP%20Object%20Oriented%0Apage%3A1%0As%3Arelevance%0Asa%3Atrue%0Aproducttypeid%3A2%0Acategory_facet%3APHP
This course was really useful for a friend of me. The progress he booked when he finished this tutorial was really great. It takes you through the basics of building a CMS in PHP and object oriented.
I think you should start with small pieces to get into OOP step by step. I mean that you should write simple classes for things you use often like handling forms, image uploads, site messages, session handling.
When you get used to programming classes and working on objects it will be time to jump for something bigger like refactor whole "engine" to OOP using mvc and other stuff.
Not really an answer but too big to be a comment.
Actually HTML has nothing to so with classes and JS and CSS has nothing to do with PHP, MVC and OOP at all.
Your code should produce some data which is going to be displayed one or another way. You can use some class to render this data, but it's not that important class.
While JS and CSS are totally separate files usually, called by browser directly from the server, avoiding your application code (unless being generated dynamically). Anyway you should have not too much concern in it.
Although it's still hard and non-trivial task to tie classes hierarchy with such a discrete way of execution of a typical PHP application. Once wise man have said once, "If Windows were have to restart after each user's click, it were written completely different".
welcome to SO.
I am by no means a PHP expert, here is my thought comes on top of my head:
OO programming with PHP does help, but not very likely as the rescue to removing all the redundancies or making your code "neater". The traditional way of coding in PHP can produce nice code. OO helps to encapsulate your code to hide the implementation and reuse others' code by direct composition and/or via inheritance.
I would say the real concern here is how to decouple the "static" html from the "dynamic" PHP.
For instance, there shouldn't be many :
echo "<h1>A title</h1";
echo "<p>blah blah blah...";
Even in conditional printing.
I would say separate the whole business logic on one page/script into several functions, and at least hide the nitty-gritty inside a function.
For your questions on popular OO frameworks of PHP, CakePHP is a good one, got its inspiration from Ruby on Rails.
Read MVC Tutorial in PHP to understand what MVC is then start using a MVC Framework like Zend, CakePHP, Symphony or CodeIgniter...
This question already has answers here:
Closed 13 years ago.
Possible Duplicate:
Why should I use templating system in PHP?
I was just curious as to how many developers actually do this?
Up to this time I haven't and I was just curious to whether it really helps make things look cleaner and easier to follow. I've heard using template engines like Smarty help out, but I've also heard the opposite. That they just create unnecessary overhead and it's essentially like learning a new language.
Does anyone here have experience with templates? What are your feelings on them? Are the helpful on big projects or just a waste of time?
On a side note: The company I work for doesn't have a designer, there are just two developers working on this project charged with the re-design/upgrade. I also use a bit of AJAX, would this have issues with a template engine?
Not only does this practice make the code look cleaner, it also has many long term and short term benefits.
You can never go wrong with organizing code. First off it makes it much easier to maintain and easier to read if someone else has to pick up after you. I have worked with Smarty before and it is nice, it keeps the designers work from interfering with the program code.
Using template systems and frameworks would make it much easier to accomplish tasks. There is a rule of thumb you can follow which is DRY (Don't Repeat Yourself). Frameworks help you achieve this goal.
You may want to look into MVC, this is the model that these frameworks are based off of. But you could implement this design structure without necessarily using framework. Avoiding the learning curve. For frameworks like Zend, the learning curve is much greater than some other ones.
I have found that Code Igniter is fairly easy to use and they have some VERY helpful video tutorials on their website.
Best of Luck!!
Actually it's the business logic that needs to be separated from the views. You can use php as a "template language" inside the views.
You can use ajax on any template engine i think.
Edit
My original response addressed the question whether to use a template engine or not to generate your html.
I argued that php is good enough for template tasks, as long as you separate business logic from presentation logic.
It's worth doing this even for simple pages, because it enables you to:
isolate the code that is the brain of your application from the code that is the face, and so you can change the face, without messing with the brain, or you can enhance the brain without braking the looks
isolate 80% of bugs in 20% of your code
create reusable components: you could assign different presentation code to the same business code, and vice versa;
separate concerns of the feature requests (business code) from the concerns of the design requests (presentation code), which also usually are related to different people on the client side, and different people on the contractor side
use different people to write the business code and the presentation code; you can have the designer to handle directly the presentation code, with minimal php knoledge;
A simple solution, which mimics MVC and doesn't use objects could be:
use a single controller php file, which receives all requests via a .httpdaccess file;
the controller decides what business and presentation code to use, depending on the request
the controller then uses an include statement to include the business php file
the business code does it's magic, and then includes the presentation php file
PHP is a template engine (or if you prefer, a hypertext preprocessor). When HTML is mixed heavily with PHP logic, it does become very difficult to maintain, which is why you would have functions defined separately to build various parts and simply build the page from short function calls embedded in the HTML. Done like this, I don't see much of a difference between Smarty and raw PHP, other than the choice of delimiters.
Separation of concerns is a very important tenant to any type of software development, even on the web. Too many times I have found that people just throw everything into as few files as possible and call it a day. This is most certainly the wrong way to do it. As has been mentioned, it will help with maintainability of the code for others, but more than that, it helps you be able to read the code. When everything is separated out, you can think about easily.
Code Ignitor, I have found, has been the easiest to learn framework for working with PHP. I pretty much started my current job and was up and running with it within a few days, from never having heard of it, to using it pretty efficiently. I don't see it as another language at all, either. Basically, using the framework forces me to organize things in a manageable way, and the added functionality is anlagous to using plugins and such for jQuery, or importing packages in Java. The thought that it's like learning another language seems almost silly.
So, in short, organize organize organize. Keep in mind, though, that there is a level of abstraction that just becomes absurd. A rule of thumb is that a class (or file in our case) should do one thing very well. This doesn't mean it is a class that wraps around print, but takes a string, formats it using a complex algorithm and then prints it (this is just an example). Each class should do something specific, and you can do that without any framework. What makes MVC great, though, is that it lets you organize things further, not just on the single class level, but on the level of "packages", being Model, View, and Controller (at least in the case of these frameworks; there are other ways to package projects). So, now you have single classes that do things well, and then you have them grouped with similar classes that do other things well. This way, everything is kept very clean an manageable.
The last level to think about once you have things organized into classes, and then packages, is how these classes get accessed between packages. When using MVC, the access usually will go Model<->Controller<->View, thus separating the model (which is usually database stuff and "business" code in the PHP world), from the view (which usually takes information from the user, and passes it along to the controller, who will then get more information from the model, if necessary, or do something else with the input information). The controller kind of works like the switchboard between the two other packages usually. Again, there are other ways to go with packaging and such, but this is a common way.
I hope that helps.
Smarty and other php template frameworks really do nothing more than compile to PHP anyway, and they also cache their results in most cases to allow for faster processing. You can do this all on your own, but if you ever look at the compiled templates that Smarty generates, and compare to the original Smarty template you create, you can see that one is far more readable than the other.
I write mostly mod_perl these days and started using templates (HTML::Template) halfway through our ongoing project. If I had to make the decision again, I would use templates right from the start - rewriting later to use templates is kind of tedious, though rewarding because you get nicer and cleaner code. For anything bigger than 2-3 pages in php, I would also use some template engine.
One big advantage of a templating engine such as Smarty is that non-developers can use it to embed the necessary logic that is used on the front-end (one really can't separate logic and display on all but the simplest sites). However, if the developer is the one maintaining the pages then using PHP would be preferable in my opinion.
If you separate out large logic blocks and maintain a consistent patten for looping and for-each flow control statements (i.e. don't use print statements, or only use print statements for one-liners, etc.) Then that should be okay.
This isn't a question about what framework to use. I've learned both Rails and Django, and I write all of my webapps in PHP. My question is why bother with the frameworks? It's always taken me longer to use a framework than to reuse old MySQL code and build "models" with phpMyAdmin. I also like writing everything myself, because I know what's going on. I can still reuse functions, etc. and do things how I want, and this freedom seems to be missing from most frameworks.
I'm not saying that my way is right; in fact, I'm trying to figure out where my logic fails. The hype can't be just thin air. What am I missing?
The basic idea of a framework is to allow you to work at a higher level of abstruction and write only the code you have to write to implement your specific requirements. All the other repetitive stuff is handled for you by the framework, and probably with far fewer bugs and security holes than if you did it yourself.
It may feel like it takes longer to learn a framework than to just do it yourself using basic language features and standard APIs, but it's simply not true - not if the framework is good and the app is non-trivial, and especially not once you have learned the framework (using a different one for each new project would of course be idiotic) and factor in the time it would take to find and eliminate all the bugs and correct all the design mistakes that have long since been found, eliminated and corrected in the framework by its developer community.
Almost every developer has cowboy coder instincts that tell him "Doing things yourself is much more fun than using code others have written, and I'm sure I'm good enough to get it right the first time, so it will even be faster and better!". These instincts are almost always wrong.
Frameworks allow you to concentrate on the application itself rather than worrying about the boilerplate code that you'd otherwise have to write for every application. They allow you to structure you site in a much more logical (mostly object-oriented) way, using tried and tested design patters such as model-view-controller. The code in framework is generally more mature and of a higher standard than code you would write yourself for one-off projects as framework have a large community of developers perfecting the code perfecting the code over year. This means that framework-driven sites often perform better and are much more secure.
You also mentioned you like writing things yourself - I know where you're coming from. My solution to this was to write my own framework - I get to reuse and improve my code with every project I do and I know the entire codebase inside out.
Writing it your self may make it easier for you to understand things your self but unfortunately it can make it much harder for other developers to understand what is happening. Frameworks will often be better documented and have a larger community that can support a new developer that is working on the app that you wrote.
I think a big part of it is what you focus on. Frameworks standardize the parts that you shouldn't have to keep revisiting, which helps you focus on the application as a whole. If you reuse your own code all the time you're already using your own makeshift framework.
Your comparing a framework (Rails) to a language (PHP). A framework is going to give you pre-built components so you can spend time on what makes your project unique.
You may already have a code base that helps do this for you. Check out some of the PHP frameworks since that's where you are more comfortable. Take a look at CakePHP, CodeIgnitor and/or Zend Framework.
If you are building many small apps/sites, using a framework may make your life easier.
I think a good step for you is to create your own framework with the code you've programmed so far. ;)
Try to make your code parametrizeable, in other words: create components which you can reuse in different parts of a website (for instance: styled containers), or in different websites (form generators/validators).
You can even go further and create base-classes from which you extend new classes to build your websites. (for instance: data objects with generic select/insert/update/delete methods).
I bet this gives you the best view on why frameworks are so damn handy ;)
It'll take you longer to initially use a framework for the same reasons a PHP developer would take longer to initially use Ruby - you're not familiar with it.
Once you're familiar with them, frameworks can offer the ability to skip the mundane and focus on actually writing the important parts of the app.
You should also just use a PHP based framework like Symfony or CakePHP using them should reduce your production time considerably.
One reason to use a frame work is code separation. Take symfony for example. The model is all done with propel or doctrine libraries. Very little SQL needed. You instantiate a new object and user getters and setters, to store your data, and instead of writing SQL in your page code you create functions in the objects related to the query. When you need to access the same kind of data on different pages you are asking the model for it, keeping the business logic with the model where it should be, so there's never any difference. All the work is done in the "action controller function". You get all the data you need, and then put as little php in the display, basically just echoing the variables you got in the action controller, (with the exception of some for loops and if statements for conditionals. I have found this a more efficient way to code, and on my 2nd project saw the production time cut in half.
You don't need to learn a new language python/ruby just to use a great framework, just have to fin one that works for you.
First, PHP has frameworks too, so the question as stated misses the point.
Yes, you can write your own framework, and as Kris said, there's no shame in that. However, part of the leverage of code reuse is the collective value of the efforts of many. It's not just about reusing your own code. Frameworks encapsulate the common tasks and patterns we all share and provide well tested solutions with many iterations of improvements from the community. No individual effort is going to measure up to that, no matter who you are.
If you roll your own, it will only become world class due to the collective effort of world class people, and that will only happen if your idea merits the attention. The top frameworks out there are already proven on those criteria.
DHH is a smart guy, but the Rails we have today never could have been realized by him alone. Not even close.
If you like "writing everything yourself" as you say, then choose a framework with a core philosophy that matches yours, and start making core contributions in the areas where you can see room for improvement.
Depending on the functions of your Web Application, it can be faster to develop without a framework. For example when the Webapp is just some kind of data viewer.
But as soon as you begin to implement more advanced functions, you are much more efficient with a framework.
Try do do this from scratch:
- proper Form validation
- Handling of multiple Language and Date/Time formatting
- Authentication
See a framework as free tools and stable implemented function for you to use.
Sounds to me like you have already written your own framework in php, since you do mention code reuse.
I can imagine it being easier to use your own set of wheels instead of adapting to someone else's. No shame in that.
Frameworks are there mainly to help people who are semi-new to PHP (or the specific language it is built on) to be able to build a website to an extent that it is secure and easy enough to add on extra parts to the site without having to know a lot about the specifics like security, MySQL (or other database types). In my opinion it is a fairly good way to help break coders into a language, allowing for the fact that the framework isn't too complex of course.
EDIT The reason behind me saying they are for beginners is because myself, as a beginner has used frameworks to break myself into languages a lot better.
Ok, have a bunch of questions that I have been thinking about the past few days. Currently I have a site that is just a bunch of PHP files with MySQL statements mixed in with PHP, HTML and CSS, basically a huge mess. I have been tasked with cleaning up the site and have made for myself, the following requirements:
The site needs to be efficient and well laid out (the source code), I would like to be able to write as little code as possible.
There has to be good separation between structure, presentation and logic.
For whatever reason, I can't use a framework and need to keep the code maintainable and "simple" as there will be future developers working with it.
There needs to be an admin section for at least a few pages.
Saying that, this is what I know about the site as it is now:
Consists of 10-12 pages, a few are completely static, most are dynamically driven via a database and there is a huge form for users to fill out (20-30 fields) that need to be validated and checked.
The hierarchy of the site is basically 5-6 main pages and then sub-pages within those.
So, knowing those things I wanted to know if anyone had any tips/suggestions as to how to go about doing this with the least amount of headaches.
Would an OO approach be best in this situation?
Since there are many static pages and the dynamic pages just need the content filled in would it be best to use some kind of basic template?
EDIT: Thanks for the answers, when I said no frameworks I basically meant anything that would require new syntax other than PHP, as whoever gets hired to work on this site after me will probably only know PHP.
Here's an article about how to organize your PHP project, from Rasmus Lerdorf, the architect who created the language:
http://toys.lerdorf.com/archives/38-The-no-framework-PHP-MVC-framework.html
Despite the popularity of OO frameworks for PHP, Rasmus advocates a less OO approach. He knows more than anyone about PHP intended usage, and how to take advantage of its architecture for high-performance websites.
edit: In response to the comment by #theman, I'll concede the article isn't a fine work of writing, but I think the content is important. Using PHP as it was intended to be used is better than struggling against its weaknesses to make it fit an OO mold.
I highly recommend the Smarty templating engine for all PHP projects. It gives you an easy way to separate the logic from the presentation.
Have a look at this SO question and the answer. It's a pretty good, simple MVC design with some tips on how it can be improved. If you are concerned about maintenance, then at the very least you need to seperate presentation from logic (you need a view and controller). Smarty forces that, but it is a type of framework and you'll have additional syntax to learn.
Before you jump on Rasmus' "no framework php mvc framework" bandwagon, read some of the critical comments. Any web application structure is a framework, and Rasmus' approach isn't the best I've seen.