PHP Object Oriented or not? [closed]

2019-06-15 20:04发布

问题:

I have a start of a webapp that I wrote without using the Object Oriented features of PHP.

I don't really know if it is worth it to go back and rewrite the parts I have finished. Is object oriented PHP worth rewriting all or part of a decent working app?

回答1:

Given that you have an incomplete app I would say that reworking it into an Object based app will probably be helpful.

One thing to consider is the expected size of the end application. Below a certain complexity Object based may be overkill except for the learning experience.

I started out avoiding Objects like the plague because my initial introduction to them in university classes was terrible. I somewhat recently had to work on a project which was implemented in php objects. making the required changes was much easier than other projects. I have since then worked in the object model frequently and find it very handy for quick creation and easier upkeep.



回答2:

Just to disagree with the consensus... I would say no in most cases. Not as an academic exercise on commercial code anyway. If it's working don't re-write it. If you have to go in to change / add bits, then refactor towards OO practices (there are lots of posts on SO about refactoring when you are changing code anyway, and not just for the sake of it).

In practise if you haven't done a lot of OOP, then you'll want to start small and get a feel for it.

Once you get a handle on the basics, a good beginners guide to Design Patterns (I like the Head First book) is very useful. Most PHP books would teach you OOP fairly poorly. They teach you about inheritance, but usually don't teach you about loose coupling and favouring composition over inheritance. A design patterns book will give you a better insight into this.

PHP still has a reputation for not "doing" OO right. I don't think this is fair, but is a reflection of the fact that it's so easy for people to get started without really grokking OOP. I would go out on a limb and say the majority (ever so slightly - call it 51%) of PHP programmers aren't comfortable with OOP. I think it's possible to do good OO in PHP, and if you're already comfortable with the language it's a great way to grow your skills.

EDIT:

Just to add a couple of disclaimers...

  1. My comment about most PHP programmers not being comfortable with OOP wouldn't apply to the current SO audience!
  2. Not suggesting you aren't comfortable with OOP, this applies if you're not


回答3:

Typical answer: "It depends."

I tend to write the display page as a start-to-finish, < html > to < /html > scripted page. But the things that happen on that page were objects. Kinda like a poor man's ASP. While you can have OOP-base output, I alwasy thought it too cumbersome for a task as tedious as dumping data to a browser.

So, business rules and data access were OOP. Presentation was script.

If you have business rules that are not OOP, I would seriously consider writing those as objects on two conditions: (1) is "Do you have time/effort/money to do so?" and (2) is "Do you have a good PHP IDE that will make your life easier?" If it works, and changing it means writing in Notepad++, then I would call it done. :-)



回答4:

I wouldn't say it is critical, but if you are going to go much further with the app, I would recommend doing it now while it is not as much of a monumental task. I would say the maintainability of a well written OOP program could far outweigh the up front costs. Especially when you consider that you will be able to refactor much of the code as you go along.



回答5:

Learning object oriented techinques will be really useful, especially for programming in other languages in the future.

Since you have only just started the application, you could rewrite and improve the parts you have written. It depends on your deadline.



回答6:

There are two possibilities: either your app is a one-off that just has to work right now and will never be touched, adapted, expanded or modified, or else your app is the beginning of something that you will keep working with and using over a long time.

If the former, don't break perfectly usable code. You have better things to do with your time.

If the latter, you have to bear in mind an important fact about PHP, which is this: poorly written PHP is a nightmare to maintain. Not as bad as poorly written Perl -- because what is? -- but bad enough that sooner or later you will feel a strong urge to steal a time machine, travel back to the moment you wrote the code you now find yourself maintaining, and stab yourself in the eye socket with an icepick.

So if you're going to be maintaining this code over time, take the time to do it right. That means: some kind of templating system, no PHP tags embedded inside HTML, separate files for separate functionality, and classes classes classes!

Your eye sockets will thank you.



回答7:

I would say try and go OO just because what you have can be reused much easier than procedural if done right

I will also say that OO is much more organized then procedural. When your at a small scale it's easy to get away with sloppy code OO or not. But when you get to larger projects your procedural must be much more organized and thought out. Where as on some larger projects OO tends to force you to be more organized making things a little easier.



回答8:

No, i think if the app is working like it should there's no need to rewrite it. PHP isn't really OOP at all. They try hard but sometimes i think even the PHP-Developers dont really understand the sense of OOP. If you wish to learn OOP (which is surely a good idea) try a real OOP-language like Smalltalk to learn the basic concepts. Java is also nice 2 learn the basic, although it isnt fully OOP as well



回答9:

I'd like to reiterate the other answers here. It depends upon size of application and how much you'd like to learn about OOP. I'd be careful of learning OOP by using PHP though.

As for how much PHP is object oriented... PHP4 had some OOP elements slapped onto it, PHP5 is better but it's not baked into the language. PHP works both ways, and personally I like that you can choose.



回答10:

In my mind, we phper can thorouly 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 it's 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 is data-centric programming, we need separate the data(array) and the activity(class method).

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