开发者

Object oriented PHP

How often do people use objects in PHP?

I have noticed it has not been used in any of the open source PHP projects I have looked into. It wasn't used in a web programming course at my Uni. I am just curious about how 开发者_如何学Pythonmuch objects are used by professional PHP developers.


That's a very vague question, but OOP is used in the same way in PHP as it would be in any other scenario: as a way of modelling the interactions between discrete entities in your business logic.

A classic application for OOP in a web context would be a blog, where each post on the blog would be represented by an object, which in turn might have references to other objects representing the comments on that post.

As Jon said in his comment, though, OOP should be used only where it makes sense and logically fits into the system you're designing. Don't try to shoe-horn something into it just for the sake of it. Many PHP applications are simple enough not to require an OO approach, and are best implemented with a clean, straightforward procedural approach.

One caveat of OOP is that it can be quite difficult for beginners to learn, and is very easy to misunderstand. I started programming with PHP, and it took me a fair bit of trial and error to understand exactly how OOP should properly be used.


Support for OO in PHP hasn't been around forever so older open source projects might not be using it because it's based on older code.

The reason schools often teach PHP without using the OO-approach is because PHP is often the entry-language for many programmers at school. By that I mean many schools will use PHP as a language they start teaching to people with little experience with programming because PHP is easy to learn. Because object orientation seems more complex to a new programmer many courses will stay away from that and rather focus on the basics of the language. Then they will later focus on OO as part of other languages like Java. Once you learn the basics of OO from another language it's easy to combine with your PHP-knowledge and start using OO-PHP.

As for using OO-PHP it will give you a more flexible code so I'd really look into it and you'll discover it's powers in notime:)


Usually Objects are not used much in small web applications but are much more useful in larger ones. The MVC pattern is generally used. Objects are not used very frequently in php but are much more so in larger languages such as java, python and C.

Hope that helps, RayQuang


I would suggest that most recent Open Source libraries in PHP do use OO principles; although older libraries may vary... and libraries written using OO are likely to survive better and be more widely used than procedural libraries.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜