Should I begin learning PHP Framework? [closed]
I have known PHP basics, functional programming for a long time before. But this year, in my 2nd semester I have learned Object-Oriented Programming with Java. So I want to apply my knowledge of OOP to PHP. But I find it very difficult to think of any decent utilization. It's not like in Java everything is an object. I see it easier to connect to database an old way, to write some simple functions for logging or registration, etc.
All those examples http://php.net/manual/en/language.oop5.php just gives me knowledge about syntax, not methodology of using it.
So I began to think. Maybe it's because I wanted to apply OOP without any Framework, and it's no use without it? If so - any Framework recommendations that are easy to learn?
OOP is a methodology used to organise your code into meaningful, reusable entities and it doesn't alone answer to the question of "how to best organize your code to create web applications".
Most of the web frameworks use Model-View-Controller architecture (MVC) along with OOP. In order to understand how web frameworks work, I suggest you to familiarize yourself with MVC as well (for a starting point, see "Coding Horror: Understanding Model-View-Controller")
Also, regardless of any frameworks, it is crucial to understand some basic concepts when creating web applications. At the minimum, familiarize yourself with how sessions work (http://www.php.net/manual/en/book.session.php), as well as how you can access databases (PDO for generic database connectivity, MySQL for MySQL-specific binding, etc.).
As for what web framework to choose, the options are plenty. Therefore consider the following pointers as informative:
- Yii Framework has a pretty clean OOP-based design (http://www.yiiframework.com/)
- Another popular object-oriented PHP framework is CakePHP (http://cakephp.org/)
- For a more light-weight framework with less overhead, consider CodeIgniter (http://codeigniter.com/)
- The de facto web framework for PHP is Zend (http://framework.zend.com/). Its huge, comprehensive, well-supported, but not always the perfect companion when travelling light.
- Also note that if you organize your code according to MVC, you may find yourself most comfortable when not using any frameworks at all
Also note that unlike in Java, in PHP object-oriented features have been slowly added during its versions 4 and 5. Therefore, many web frameworks designed for earlier versions of PHP may not provide you as cleanly object-oriented design as you might want. Therefore when choosing a framework, consider whether you opt-in for backward-compatibility (PHP4 support) or are you seeking for a clean, object-oriented design in the framework (mostly PHP5 required).
OOP has its usage outside of a framework, you cannot compare apples with oranges.
The most respectable frameworks out there are written in OOP. So try to use one such framework in order to see use cases of OOP, then learn from that framework (step through with a debugger and learn how the framework works from the inside).
I would recommend you two things, if you really want to become a pro:
- Zend Framework - not easy to learn, but it will show you lost of things from the next point:
- a book about design patterns - VERY VERY IMPORTANT
Regarding the databases: modern PHP code should use PDO.
The framework will only show you usages of design patterns and how to organize your code, so you'll develop a "common sense" for it.
After you've passed that point, you will be able to invent your own OOP-world, without a framework (if this really needs be, although a framework gives you the time to actually be innovative).
You don't need to learn any PHP framework to start applying OOP principles. You can check out this fine answer to see some application of OOP without any framework.
Learning a framework is definitely a good choice, because then you can use lots of ready-made component. There are a lots of PHP frameworks nowadays. You can check out CakePHP, Zend (component-based), YII, Symphony (Yahoo uses it), CodeIgniter etc.
Hope this helps.
First of all, you don't need a framework to utilize OO style of programming in PHP; in matter of fact PHP has many core classes that use object oriented approach. For example take a look at PDO or the SPL.
Anyways concerning frameworks, there are various OOP-based frameworks out there, most which follow the MVC pattern. Some popular frameworks you might want to look into:
- Zend Framework
- CodeIgniter
- Symfony
- CakePHP
Zend Framework is very extensive, but the learning curve can be quite steep, but it's definitely worth the effort. If you want a good introductory MVC framework, I can recommend CodeIgniter; it's very lightweight and easy-to-use. Unfortunately I have no first-hand experience of Symfony or CakePHP (or other MVC frameworks).
You should use framework. Your code will be more easily edited by other developers if you do so.
Code Igniter is quite easy and basic, but you can check out Zend, Yii, Symphony and so on.
I recommend you Codeigniter framework to start with
精彩评论