开发者

PHP vs OO PHP - Which one to use? [closed]

开发者 https://www.devze.com 2023-01-07 13:15 出处:网络
Closed. This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing
Closed. This question is opinion-based. It is not currently accepting answers.

Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.

Closed 7 years ago.

开发者_JAVA百科 Improve this question

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.


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:

  • PHP Object Oriented or Not?
  • Is my PHP code object oriented?
  • Learning PHP Class

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.


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.


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();
?>


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号