9

I'm developing a web system using regular PHP. This was my first experience with PHP so the code is not legible nor clean. It mixes some 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.

Jocelyn
  • 11,209
  • 10
  • 43
  • 60
rlc
  • 5,809
  • 5
  • 38
  • 46
  • 3
    It really is more the difference between being organized in your code and not. You WANT organization. The headache of sifting through disorganized code is a HUGE time sink that you don't want to have to deal with.. OO increases longevity and reusability of code. Make a book class, slap the attributes as vars in it, and enjoy the benefits of having to deal with one, consolidated object that holds all the data you need (versus a bunch of disconnected arrays or whatever other way one might go about keeping track of books). This website is your friend: http://php.net/manual/en/book.classobj.php – abelito Jul 12 '10 at 22:35
  • I removed the 'comparsion' and 'language-comparison' tags as you aren't really asking for a comparison between PHP and other languages: you are asking about PHP and whether to use the object system. – Tom Morris Jul 12 '10 at 22:46

4 Answers4

16

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.

Community
  • 1
  • 1
Tom Morris
  • 3,979
  • 2
  • 25
  • 43
  • +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 Jul 13 '10 at 00:26
2

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 Fouladpour
  • 29,356
  • 21
  • 76
  • 127
  • However, the amount of PHP code that you intermingle with your HTML will be SIGNIFICANTLY higher using non OO. – thetaiko Jul 12 '10 at 22:40
  • 1
    @thetaiko: Not necessarily. Prove me wrong with an example and I'd answer your challenge with rewriting the procedural version. – Majid Fouladpour Jul 12 '10 at 22:52
  • 1
    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 Jul 13 '10 at 00:50
0

In my mind, we PHPers can thoroughly throw away the concept of Object (class instance), we only need Array and Mode Class:

All arrays in initial mode support any array function as its method:

<?php
$array1->array_flip(this);
?>

Use "->mode()" to validate the minimal data set, and then switch mode class:

<?php
$array1->mode('class1', $success);
?>

Any mode class has no "->construct()" in it, but has "->validate()" to validate the minimal data set.

The array in a mode still could use array function as its method, but after using any of them the array will be switched back into basic array mode, and we need to use "->mode('class1', $success);" to switch mode back.

The radical thought here is data-centric programming; we need to separate the data (array) and the activity (class method).

We could modify the PHP engine, to get rid of parts of OO (object oriented), and support Mode Class. We could call it MyPHP.

For example: $array_man1 could be set into two modes: cls_normal_man and cls_crazy_man:

<?php
$array_man1->mode('cls_normal_man')->normal_method1()->mode('cls_crazy_man')->crazy_method1();
?>
Serp C
  • 864
  • 1
  • 10
  • 24
diyism
  • 12,477
  • 5
  • 46
  • 46
-5

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

Adam Lukaszczyk
  • 4,898
  • 3
  • 22
  • 22
  • 6
    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 Jul 12 '10 at 22:43
  • 3
    i don't say that there is no good tools in oo php. I just say that PHP was never really OO programming language. – Adam Lukaszczyk Jul 12 '10 at 22:48
  • @Dobiatowski So what? It is OOP now. Sure, it's not my favorite OOP implementation, but it works just fine. – George Marian Jul 12 '10 at 23:00
  • @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 Jul 13 '10 at 00:57
  • @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 Jul 13 '10 at 17:03
  • 1
    @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 Jul 13 '10 at 20:16
  • @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 Jul 14 '10 at 00:33