Using PHP Packages without Composer

Composer http://getcomposer.org/ is a “Dependency Manager for PHP”.

If you do not have Apache command line access, like most people using a shared hosting account, instructions for installing a PHP package with Composer won’t work (at least not using the instructions given):

The preferred method of installation is [composer](http://getcomposer.org/). In your project root (not web root), create a minimum [composer.json](http://packagist.org/) file:
{
"require": {
"VerticalTab/Pillow": "x.x.x"
}
}
Replace "x.x.x" above with the tag number you want to use.

Next, get composer and use it to install (again, in your project root)
$ wget http://getcomposer.org/composer.phar
$ php composer.phar install
This will put the library into your vendors directory.

Then you would use this line of code in your program to access the package:
require 'vendor/autoload.php';

Well, you don’t need Composer to install this package, you can insert some standard PHP code that works, into your program. (But you won’t have all the version checking that Composer does.)

This PHP code replaces the autoload.php:
function __autoload($class_name) {
require_once("./$class_name.php");
}

The package I am using as an example is a Zillow API package, in the VerticalTab folder, inside the current folder, and contains these files:
/VerticalTab/Pillow/Chart.php
/VerticalTab/Pillow/Comps.php
/VerticalTab/Pillow/Date.php
/VerticalTab/Pillow/HttpClient.php
/VerticalTab/Pillow/Links.php
/VerticalTab/Pillow/Property.php
/VerticalTab/Pillow/Proxy.php
/VerticalTab/Pillow/Range.php
/VerticalTab/Pillow/ResultSet.php
/VerticalTab/Pillow/SearchResults.php
/VerticalTab/Pillow/Service.php
/VerticalTab/Pillow/Xml.php
/VerticalTab/Pillow/Zestimate.php

List all the package’s .PHP files using a special syntax. Convert the names like below. The backslashes are required; forward slashes are incorrect syntax. Drop the .php extension

use VerticalTab\Pillow\Chart;
use VerticalTab\Pillow\Comps;
use VerticalTab\Pillow\Date;
use VerticalTab\Pillow\HttpClient;
use VerticalTab\Pillow\Links;
use VerticalTab\Pillow\Property;
use VerticalTab\Pillow\Proxy;
use VerticalTab\Pillow\Range;
use VerticalTab\Pillow\ResultSet;
use VerticalTab\Pillow\SearchResults;
use VerticalTab\Pillow\Service;
use VerticalTab\Pillow\Xml;
use VerticalTab\Pillow\Zestimate;

Now all the package code is accessible to your program.

Inside the package are namespace commands, e.g. VerticalTab/Pillow/SearchResults.php has namespace VerticalTab\Pillow;


Posted

in

by

Comments

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.