在PHP中组织/使用可重用的代码库[关闭]
My goal is to write some reusable (OO) code for communicating with an external API, but as I'm not well versed in this kind of stuff in PHP, I'm not sure what is the best way to organize and load my code.
Stuff like limiting the amount of knowledge the client code needs to have about the libraries classes and making sure there are no naming collisions has led me to experiments with namespacing and avoiding having to inlude all the library files have led to experiments with autoloading, but I have so many questions on the subject that in stead of a lengthy process of trial and error, I thought I'd see if I anyone here possessed some wisdom on the subject.
So I guess my main questions are:
- How to best encapsulate the library code, making it stand on it's own as much as possible.
- How to best load/call this library code from the client code.
... but feel free to expand on whatever you think should be considered. :)
我的目标是编写一些可重用的(OO)代码,用于与外部API通信,但我不是 我非常熟悉PHP中的这种东西,我不确定组织和加载代码的最佳方法是什么。 p>
就像限制客户端代码所需的知识量一样 关于库类并确保没有命名冲突导致我使用命名空间进行实验并且避免必须包含所有库文件导致自动加载实验,但是我对这个主题有很多问题而不是 漫长的反复试验过程中,我想我会看到我这里的任何人都对这个问题有一些了解。 p>
所以我猜我的主要问题是: p> \ n
- 如何最好地封装库代码,使其尽可能独立。 li>
- 如何从客户端代码中最好地加载/调用此库代码 。 li>
ul>
...但随意扩展什么 你认为应该考虑。 :) p> div>
The best way to go about it is simply to check how others do it Both Zend Framework and Symfony for instance will have a clear separation between these two. May not be a bad idea to borrow some concepts and make them your own.
I tend to simply put my library code and 'client code' in separate namespaces.
What I can say that use OOP in php code to organize your code and reusable.
You can easily make a standalone class.