tags:

views:

126

answers:

3

I'm developing a web system using regular PHP. This was my first experience with PHP sothe code is not legible nor clean. It mixes some many html code with PHP. I'd say I have already done half of the code.

What are the real advantages of the Object-oriented PHP. The website is about books and book authors, using mysql and apache. So it's not a very complicated website.

+1  A: 

Object oriented PHP does not differ with the procedural style in the amount of HTML code you mingle with PHP code. So if your only concern is the mix you should look for other ways to get your code cleaned. For example you can create html template files with placeholders for your dynamic content and use file_get_contents and str_replace to inject the dynamic content at run time.

Majid
However, the amount of PHP code that you intermingle with your HTML will be SIGNIFICANTLY higher using non OO.
thetaiko
@thetaiko: Not necessarily. Prove me wrong with an example and I'd answer your challenge with rewriting the procedural version.
Majid
Whether or not you're using OOP or procedural PHP, if you're mixing in HTML (presentation) with your business logic, you're in for a big sprawling mess. Any web developer can tell you that. Use the presentation side more as a template like how JSP enforces it.
Lotus Notes
A: 

if you really want to use oo programming go to Ruby.

OO PHP for me is a fake. And if you already have half of code done in structural php dont change your mind.

just remember to make code clean with lots of comments so you can easily change sth in the future

Dobiatowski
Someone should tell facebook. And Zend. And PEAR. And the maintainer of SPL.There are millions of lines of quality object oriented PHP out there, regardless of your personal feelings about it.
Frank Farmer
i don't say that there is no good tools in oo php. I just say that PHP was never really OO programming language.
Dobiatowski
@Dobiatowski So what? It is OOP now. Sure, it's not my favorite OOP implementation, but it works just fine.
George Marian
@George Marian: The only way to keep the state of the objects across pages is to serialize them and unserialize them or to store them in a database. In a sense this breaks the OOP, but there is nothing anyone can do about that because this is how the web works... unless you starting using Java (applets/servlets) or some other similar language (I am not familiar with Ruby). ;)
Alerty
@Alerty Sure, that's the way HTTP is supposed to work, in a stateless fashion. Throwing cookies on top of that goes against the REST paradigm to which HTTP ascribes. As does serializing objects to pass between pages. But, that has nothing to do with OOP in PHP. :)
George Marian
@George Marian: Why would it have nothing to do with OOP in PHP? PHP and OOP in PHP is limited to the same paradigm that HTTP follows unless you start using sessions, cookies and databases to keep the data. Not many objects would be of use if they could not keep their state over different pages or over a page refresh unless you have some way of doing it...
Alerty
@Alerty That issue has nothing to do with PHP or OOP, that's just what HTTP is. You'll find that every other server side technology like JSP and ASP are also stateless. And why does serializing an object break OOP? It's an object before, and still an object afterwards.
Lotus Notes
@Alerty What @Byron said. Maintaining state is in no way inherent to OOP. The reason that we've slapped sessions on top of HTTP is that we're so used to that type of state handling. Granted, for some thing it's necessary, but I believe it's overused. Using HTTP the way it was intended is gaining in popularity, with RESTful approaches to webservices for example.
George Marian
+7  A: 

The real advantage of object-orientation: your code is better organized, easier to maintain, more modular (and thus easier to reuse), and potentially less brittle (because of encapsulation of state and implementation, and hopefully better security). (The cynic in me also says that if you learn object-oriented PHP, you take the first important step to leaving the PHP ghetto. Heh. Worked for me!)

There's already a lot of questions from PHPers moving into OO on Stack Overflow:

Not to mention that there are zillions of PHP object-oriented tutorials out there. My take: basically, yes, if you are writing PHP, you should probably be writing object-oriented PHP for anything beyond the most trivial of applications. There are lots of Rails-like frameworks for PHP that will make your life easier, and may help you become a better programmer.

Tom Morris
+1: I agree with you! I would only add that PHP does keep the state of your objects to one page to another so you will perhaps want to keep the data in the $_SESSION array.
Alerty