# Python-esque package management in PHP

**URL:** <https://forum.kirupa.com/t/python-esque-package-management-in-php/227059>\
**Category:** open source\
**Created:** [May 26, 2007, 3:53pm UTC](https://forum.kirupa.com/t/python-esque-package-management-in-php/227059 "2007-05-26T15:53:34Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sammo](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/sammo/32/779_2.png) [@Sammo](https://forum.kirupa.com/u/Sammo)\
**Post date:** [May 26, 2007, 3:53pm UTC](https://forum.kirupa.com/t/python-esque-package-management-in-php/227059/1 "2007-05-26T15:53:34Z")

</div>

Simple PHP class that allows for a more structured manner of importing classes.

PackageManager.php:

```php
<?php

class PackageManager {
	const PACKAGE_BASE = "./";	

	private $path;
	
	function __construct($path) {
		$path = str_replace(".", "/", $path);
		$this->path = PackageManager::PACKAGE_BASE . (substr($path, -1, 1) == "/" ? $path : $path . "/");
	}
	
	function import($file) {
		if ($file == "*") {
			foreach(glob($this->path . "*") as $class) {
				require_once $this->path . $class;
			}
		} else {
			$classes = explode(",", $file);
			foreach($classes as $class) {
				$class = trim($class);
				$class = (substr($class, -4, 4) == ".php"_ ? $class : $class . ".php";
				require_once $this->path . $class or
					throw new PackageManagerException("Class: {$class} was not found in {$this->path}");
			}
		}
	}
}
class PackageManagerException extends Exception { }

function from($path) {
	return new PackageManager($path);
}

?>

```

if you had the following file structure:

```auto
 PackageManager.php
 index.php
 app/
    blog/
       Posts.php
       Feeds.php
       comments/
          Comments.php
 core/
    lib/
       DB.php
       Template.php
       Urls.php

```

You could include any of the following (or every) in index.php:

```php
from("app.blog")->import("Posts, Feeds");

from("app.blog.comments")->import("Comments.php");

from("core.lib")->import("*");

from("core.lib")->import("DB, Urls");

try {
   from("core.lib")->import("Unknown");
} catch(PackageManagerException $e) {
   echo $e->getMessage();
}

```
