tags:

views:

90

answers:

5

i have studied design patterns and want to use them to code an open source library (not an application).

but i have never coded a library before and don't know where should i include files, should i have a bootstrap file that loads everything or should every class load their own classes they are dependent on etc.

are there any tutorials for writing libraries in php from start to finnish?

thanks

+3  A: 

I can't point you to a tutorial, but the easiest way to have a plug and play library is to have one class per class file, so that the user has the option to use __autoload and simply instantiate your classes without having to change anything in their existing code. This way is most accessible to most developers.

This method still allows you to create one 'standalone' include file that includes all your other class files.

Other options include making it into a PEAR package or asking people to place your files in the include path. However this is not optimal in my opinion for people who don't have access to anything other than their own public_html folder (on shared hosting, for example).

Lotus Notes
I assume you mean PEAR, not PERL.
zerocrates
+1  A: 

I'd point you to Zend Framework. You can use it as a whole framework, but can also use selected components as you like. yes, i think to a library, autoloading is the key.

Jim Li
+1  A: 

If your library does any examination or manipulation of strings, think about whether they're meant to be strings of bytes or strings of text characters, and if the latter, use only the multibyte string functions with them, not the normal PHP string functions. Otherwise your library won't be usable to people who need to work with Unicode text.

Wyzard
+2  A: 

You should have a look at the Solarphp Framework. It solves like every other frameworks some standard issues like autoloading, dependeny injection etc... I learned a lot about writing good php code from it. If you have questions join #solar-talk at freenode

kalkin
A: 

There are much of PHP frameworks available and which may helps you to build Library files. But, i always prefer MVC like models with more hook functions with globally accepted formats. Few of the examples below...

# o     = Object
# r     = Return
# a     = Array
# e     = Element
# g = Global declaration
# hook  = a function which can be used for hooking with other functions
# call  = a function which can be used for making call from client to server system
# sync  = a function which can be used for SYNC
VAC-Prabhu