Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making a php library, how to make it independent?

Tags:

php

Hi I'm developing a library for guitar chords. In some files on the library I have something like this:

require_once "lib/GuitarChord.php";
require_once "lib/Chord.php";
require_once "lib/Key.php";
require_once "lib/Tuning.php";

How can I make this library so it doesn't need to know where the other library files are?

This way a person could have the library files in any directory?

Edit: Removed second question.

like image 593
carlosdubusm Avatar asked Sep 15 '11 22:09

carlosdubusm


People also ask

Which library is used in PHP?

The mysqlnd library is part of the PHP distribution. It offers features like lazy connections and query caching, features that are not available with libmysqlclient, so using the built-in mysqlnd library is highly recommended.

Does PHP have library?

Yes, Like every other Language PHP uses libraries. PHP Libraries are collections of prewritten code that users can use to optimize tasks. The Standard PHP Library (SPL) is a collection of interfaces and classes that are meant to solve common problems.


2 Answers

Create autoloaders for all locations where these classes might be.

For Example:

//AUTOLOADER
function class_autoloader($class) {

   // presumes classes are in './classes'
   $folders = array(
     './', './../', './../../'  
   );
   $directories = array(
     'classes','lib',  ''
   );
   $dir = dirname(__FILE__);
   $theClass = '/' . $folderedClass . '.php';


   foreach($folders as $folder){
       foreach($directories as $directory){
           $theInclude = $dir.$folder.$directory.$theClass;

           if (file_exists($theInclude) && include_once($theInclude)) {
              return TRUE;
           } 
       }
   }

  trigger_error("The class '$class' or the file '$theClass' failed to spl_autoload ", E_USER_WARNING);

  return FALSE;
}

spl_autoload_register('class_autoloader');

Then if you want to load the Chord Class and you know it is in your lib folder, the autoloader will do the work for you when you do:

new Chord();

You can attach many different autoloader callbacks with spl_autoload_register

like image 190
Naftali Avatar answered Oct 15 '22 21:10

Naftali


Somewhere you're going to have to include the folder name, if you don't want to include all your classes in one file. You can make it somewhere that the user only has to set once, though:

$lib_folder = "lib";

require_once $lib_folder . "/GuitarChord.php";
require_once $lib_folder . "/Chord.php";
require_once $lib_folder . "/Key.php";
require_once $lib_folder . "/Tuning.php";

As for handling different types of databases, you can make another setting like $db_type that the user either sets to "MYSQL", "FILE", "XML", etc. And where you make calls to the db, use a switch statement to see what database method they're using and change your call accordingly.

Edit: It's also worth looking into PHP's __autoload feature

like image 23
rekamoyka Avatar answered Oct 15 '22 21:10

rekamoyka