开发者

Why class files are required in PHP?

开发者 https://www.devze.com 2023-02-17 10:14 出处:网络
I have seen that almost all PHP script includes开发者_如何学运维 a class file. as a newbie I don\'t understand why this is required and instead of writing scripts on a single page. What is the concept

I have seen that almost all PHP script includes开发者_如何学运维 a class file. as a newbie I don't understand why this is required and instead of writing scripts on a single page. What is the concept behind this. can any one clearly explain this?


Management. For a big project, would you rather have 500 small files, each containing a class, and which can be edited and changed individually, or one giant 50 meg script containing everything?


firstly, it's more clean approach, you won't need to search huge files for a class you need. secondly if you need a class in other places you'll have to rewrite it, but if you have it in separate file you can use it everywhere you need it.


you seriously think 1file containing 100MB of data is funny ? imagine a script with one file,how much time it will take to load on browser ? you think your server can handle reading 100MB while you are not permitted to read more than 16MB (as default and it can be raised)


It's not required, it is just often a better solution for many reasons.

Code re-use A class should be a self-contained, well-defined piece of code which can be re-used in many different places and circumstances.

Black-box coding In this case the user of the class doesn't need to know how it works, only how to use it. For example, while I am writing the code for my Robotic Monkey, I might write a Banana class. I could then give this to you and you would know how to use $banana->peel(); without having to understand the complex server<->monkey interaction that I had to deal with in order to write the class.

You could go away and write your own Robotic Orang-utan software and re-use my Banana class without editing so much as a semi-colon.

Extendability Given my Banana class above, an experienced programmer could extend that class to add in a throw() method. They could even do this without understanding all of the inner workings of the class.

There are other reasons. It's hard to explain why classes are useful without explaining classes themselves.

In short, once you understand classes you will understand why you should be using them!

0

精彩评论

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