Classes. What's the point?

后端 未结 12 1876
夕颜
夕颜 2020-12-03 03:09

I\'m fairly new to OOP in PHP, I\'ve made a couple of basic scripts but nothing impressive. All I\'ve really taken from it is that it would probably be easier just make a co

相关标签:
12条回答
  • 2020-12-03 03:39

    Classes are a notion of object-oriented design (and programming and analysis, respectively), where they are used to encapsulate data and methods.

    Other object-oriented programming techniques may include features such as

    • information hiding,
    • data abstraction,
    • encapsulation,
    • modularity,
    • polymorphism and
    • inheritance

    From an article .. top-15-best-practices-for-writing-super-readable-code:

    Object oriented programming can help you create well structured code. But that does not mean you need to abandon procedural programming completely. Actually creating a mix of both styles can be good.

    From http://java.sun.com/docs/books/tutorial/java/concepts/class.html:

    In the real world, you'll often find many individual objects all of the same kind. There may be thousands of other bicycles in existence, all of the same make and model. Each bicycle was built from the same set of blueprints and therefore contains the same components. In object-oriented terms, we say that your bicycle is an instance of the class of objects known as bicycles. A class is the blueprint from which individual objects are created.

    Finally, a short youtube video about the differences between the procedural and object-oriented programming paradigm ...

    0 讨论(0)
  • 2020-12-03 03:39

    A big advantage to OOP is code reuse. An example: Say you design a generic "Socket" class, which communicates with a server at a very low level. Then, I may come along and decide that I want to write an FTP class, which allows a user to download files while connected to an FTP server. I can "subclass" your Socket class and extend its functionality to support FTP, without rewriting your code.

    Now, say John wants to write an HTTP class, to interact with HTTP servers. He, too, can subclass your Socket class to accomplish this, without rewriting the low level socket code.

    Notice how in this example there was no duplication of socket code; both me and John shared it. Code duplication is bad because if there is an error in the duplicated code, the exact same bug may occur somewhere else in your code and you may not realize it.

    More detailed information about classes can be found on Wikipedia.

    0 讨论(0)
  • 2020-12-03 03:43

    To manage complexity.

    Edit: Maybe I shopuld elaborate a bit more.

    Programs are complex entities and many ways have been proposed historically to tame their complexity.

    Procedural programming, functions, structured programming, abstract data types and object-oriented programming are just models to help programmers manage the mental effort that is required to understand a program.

    Object programming will not solve everything, and certainly for a small script it is overkill, but it works great for big applications or systems.

    My original answer tries to summarize the full content of the humongous Code Complete book: do your best to manage complexity and make your programs understandable for you and your successors. It's great if you think that object programming will help you in this task, but it's even better that you question its convenience and try to find better ways.

    0 讨论(0)
  • 2020-12-03 03:45

    I have thought exactly the same thing.

    I have read a few books on OO-PHP, my favorite being PHP In Action.

    Although when I read the books I thought "Hey, this is very useful and interesting information.", I have yet to use a significant amount of OO-PHP.

    Simple needs like getting info from the server and JSON-ing it back really dont need OOP. Im sure it is "Best Practice", but for many people's needs it isn't practical. A few classes, like database connection, and similar data handling can be real time savers, but PHP is sometimes easier and quicker to be used only partially OO.

    0 讨论(0)
  • 2020-12-03 03:47

    All of the above poster have really good points. I would highly recommend the following books:

    • The OO Thought Process: http://www.amazon.com/Object-Oriented-Thought-Process-3rd/dp/0672330164/ref=sr_1_5?ie=UTF8&s=books&qid=1262547120&sr=8-5

    • PHP5 Objects, Patterns & Practice: http://www.amazon.com/PHP-Objects-Patterns-Practice-Second/dp/1590599098/ref=sr_1_3?ie=UTF8&s=books&qid=1262546817&sr=8-3

    • OO Design Heuristics: http://www.amazon.com/Object-Oriented-Design-Heuristics-Arthur-Riel/dp/020163385X/ref=sr_1_1?ie=UTF8&s=books&qid=1262546869&sr=8-1

    0 讨论(0)
  • 2020-12-03 03:49

    Classes are a way to organize programs. Classes can be used to represent entities in the real world, or artifacts in our programming world, reducing the complexity of programs by allowing us to reason about them in terms of a higher granularity.

    It is well-known in psychology that experts organize knowledge into "chunks" to allow them to reason more easily and quickly about their domain. Classes allow us to reason more easily and quickly about our programs.

    In addition, classes allow us to group together the knowledge about an entity together with the methods/procedures/functions/operations that are naturally associated with that entity. Again, this provides a natural way to organize the information in our programs.

    There are other advantages of classes, such as inheritance, but primarily classes are a cognitive way of organizing program structure to reduce the complexity.

    0 讨论(0)
提交回复
热议问题