Classes. What's the point?

后端 未结 12 1916
夕颜
夕颜 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:58

    I always thought the same thing until I started using classes--realizing that it is so much simpler and better. You should create those functions that you're mentioning, but gather them all within a class. The functions can then within a class share the same variables, and therefore making it simpler when having to use the same functions multiple times.

    The best example I can come up with now is connecting to a database.

    Instead of having individual functions that connect, sends a query, fetching the results and closing the connection you could wrap all these functions within a class. This would allow you to communicate to the database multiple times using the same functions, without having to make loads of variables tailored for the specific use on the page.

提交回复
热议问题