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?

Comments

Regarding databases: Have a look at PDO

Written by KingCrunch

Accepted Answer

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).

Written by jsalonen
This page was build to provide you fast access to the question and the direct accepted answer.
The content is written by members of the stackoverflow.com community.
It is licensed under cc-wiki