silow
silow

Reputation: 4306

How to create a library

I have some code which I want to organize into a class or library for other developers to use more easily on their website.

The way I use the class now is to instantiate an instance of it and start calling the functions I need as I need them.

$class_object = new MyClass();
$class_object->myFunction();

Is this basically all I need to make a library, or do I need to make further changes to turn this into a library?

Upvotes: 2

Views: 259

Answers (4)

Matteo Riva
Matteo Riva

Reputation: 25060

It depends on what exactly your library is. A class is a good solution if you are describing an object which can have properties and methods, and all the actions are related to that object.

If you only have a collection of functions and you can assume users will have PHP 5.3+, then you might just define them under a namespace.

Upvotes: 1

Jonah
Jonah

Reputation: 10091

As far as I know, yes. But you do need to make your library compatible with the accepted autoloading standards. That way developers can simply drop it into their system and start using it.

http://groups.google.com/group/php-standards/web/psr-0-final-proposal?pli=1

Upvotes: 1

Jamie Rumbelow
Jamie Rumbelow

Reputation: 5095

PHP doesn't have any concept of a library other than what you're already doing. If you want to abstract it (make sure it works outside of your application) and move it into a separate file/directory, you can, but that doesn't qualify it as a library any more than usual!

I will note though, there are some frameworks that use the term library as something specific, but you'll have to look into the individual framework's documentation for that, I'm afraid.

Upvotes: 1

Tim Dorr
Tim Dorr

Reputation: 4921

It depends a lot on the functionality that you're providing. If the library is just a group of functions, then it might be best to have them static:

class MyLib 
{
    public static function convertData( $data )
    {
        // Do something
    }
}

But if it depends on the object being instantiated and stored information within that object, then I'd leave it as-is. It all depends on how you use it.

Upvotes: 1

Related Questions