<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: PHP Package Management and Autoloading</title>
	<atom:link href="http://jacwright.com/blog/40/php-package-management-and-autoloading/feed/" rel="self" type="application/rss+xml" />
	<link>http://jacwright.com/blog/40/php-package-management-and-autoloading/</link>
	<description>Flex, AIR, PHP, etc.</description>
	<lastBuildDate>Thu, 26 Aug 2010 22:38:43 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
	<item>
		<title>By: ryan teixeira</title>
		<link>http://jacwright.com/blog/40/php-package-management-and-autoloading/comment-page-1/#comment-9448</link>
		<dc:creator>ryan teixeira</dc:creator>
		<pubDate>Wed, 09 Jun 2010 17:38:14 +0000</pubDate>
		<guid isPermaLink="false">http://www.jacwright.com/blog/?p=40#comment-9448</guid>
		<description>I prefer Andrey&#039;s solution.  I see no need to explicitly list directories.  Manipulate the include_path to include all the directories where files would be located.

Consider whether a solution like this is really needed before implementing it.

Lastly, I think it would be a mistake to try to make PHP look like Java.</description>
		<content:encoded><![CDATA[<p>I prefer Andrey&#8217;s solution.  I see no need to explicitly list directories.  Manipulate the include_path to include all the directories where files would be located.</p>
<p>Consider whether a solution like this is really needed before implementing it.</p>
<p>Lastly, I think it would be a mistake to try to make PHP look like Java.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Andrey Skorikov</title>
		<link>http://jacwright.com/blog/40/php-package-management-and-autoloading/comment-page-1/#comment-9341</link>
		<dc:creator>Andrey Skorikov</dc:creator>
		<pubDate>Mon, 03 May 2010 08:48:52 +0000</pubDate>
		<guid isPermaLink="false">http://www.jacwright.com/blog/?p=40#comment-9341</guid>
		<description>Jacob&#039;s solution can be misleading in two ways.

Firstly, it creates the illusion that there are some kind of packages in PHP, which is not the case, so the problem of name collisions remains. Howie have already pointed that out.

Secondly, declaring &quot;imported&quot; classes at the top of the file does not imply that those are the only dependencies required by the script. Some classes might already have loaded other classes too. It&#039;s kind of a hint, though.

Personally, since there is no notion of a package in PHP (though in PHP 5.3 namespaces are introduced), i find there is no need to specify the exact location of the &quot;imported class&quot;. There is no way for explicitly declaring the dependencies either. So, basically the only things i want to (and can) achieve are:

1. Get rid of the require or include statements
2. Organize files in some kind of hierarchy

The idea is just to build the include_path to contain all the subdirectories. That way i can easily move files around without worrying about any class loading issues.

init();

function init() {
	$path = implode(dirs(&quot;classes&quot;), PATH_SEPARATOR);
	set_include_path($path);
	main();
}

function dirs($path) {
	$dirs = array($path);
	$contents = scandir($path);
	foreach ($contents as $filename) {
		if (ignored($filename)) {
			continue;
		}
		$item = &quot;{$path}/{$filename}&quot;;
		if (is_dir($item)) {
			$dirs = array_merge($dirs, dirs($item));
		}
	}
	return $dirs;
}

function ignored($filename) {
	return preg_match(&quot;/^\./&quot;, $filename);
}

function __autoload($class) {
	require_once(&quot;{$class}.php&quot;);
}</description>
		<content:encoded><![CDATA[<p>Jacob&#8217;s solution can be misleading in two ways.</p>
<p>Firstly, it creates the illusion that there are some kind of packages in PHP, which is not the case, so the problem of name collisions remains. Howie have already pointed that out.</p>
<p>Secondly, declaring &#8220;imported&#8221; classes at the top of the file does not imply that those are the only dependencies required by the script. Some classes might already have loaded other classes too. It&#8217;s kind of a hint, though.</p>
<p>Personally, since there is no notion of a package in PHP (though in PHP 5.3 namespaces are introduced), i find there is no need to specify the exact location of the &#8220;imported class&#8221;. There is no way for explicitly declaring the dependencies either. So, basically the only things i want to (and can) achieve are:</p>
<p>1. Get rid of the require or include statements<br />
2. Organize files in some kind of hierarchy</p>
<p>The idea is just to build the include_path to contain all the subdirectories. That way i can easily move files around without worrying about any class loading issues.</p>
<p>init();</p>
<p>function init() {<br />
	$path = implode(dirs(&#8220;classes&#8221;), PATH_SEPARATOR);<br />
	set_include_path($path);<br />
	main();<br />
}</p>
<p>function dirs($path) {<br />
	$dirs = array($path);<br />
	$contents = scandir($path);<br />
	foreach ($contents as $filename) {<br />
		if (ignored($filename)) {<br />
			continue;<br />
		}<br />
		$item = &#8220;{$path}/{$filename}&#8221;;<br />
		if (is_dir($item)) {<br />
			$dirs = array_merge($dirs, dirs($item));<br />
		}<br />
	}<br />
	return $dirs;<br />
}</p>
<p>function ignored($filename) {<br />
	return preg_match(&#8220;/^\./&#8221;, $filename);<br />
}</p>
<p>function __autoload($class) {<br />
	require_once(&#8220;{$class}.php&#8221;);<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sven Neumann</title>
		<link>http://jacwright.com/blog/40/php-package-management-and-autoloading/comment-page-1/#comment-9195</link>
		<dc:creator>Sven Neumann</dc:creator>
		<pubDate>Sun, 11 Apr 2010 13:44:05 +0000</pubDate>
		<guid isPermaLink="false">http://www.jacwright.com/blog/?p=40#comment-9195</guid>
		<description>My own derivation from your code:

The following is placed in classes/loader.inc.php and included via require_once(&#039;classes/loader.inc.php&#039;);

It will turn this
new TagStorageData()
into
require_once(&#039;classes/tag/storage/tag_storage_data.class.php&#039;);

and
new ITagStorageData()
into
require_once(&#039;classes/tag/storage/tag_storage_data.interface.php&#039;);

define(&#039;CLASS_EXT&#039;, &#039;.class.php&#039;);
define(&#039;INTERFACE_EXT&#039;, &#039;.interface.php&#039;);

function __autoload ($className) {
	loadClass($className);
}

function loadClass ($className) {
	$classesRootFolder = dirname(__FILE__);
	$classFileExtension = &#039;&#039;;
	if(preg_match(&#039;/^I[A-Z]/&#039;, $className)) { // Interface
		$classFileExtension = INTERFACE_EXT;
		$className = preg_replace(&#039;/^I([A-Z])/&#039;, &#039;$1&#039;, $className);
	} else { // Class
		$classFileExtension = CLASS_EXT;
	}
	$parts = preg_split(&#039;/(?&lt;!^)(?=[A-Z])/&#039;, $className);
	$classFileName = strtolower(implode(&#039;_&#039;, $parts).$classFileExtension);
	array_pop($parts); // Drop final element
	$classFileName = strtolower(implode(&#039;/&#039;, $parts)).&#039;/&#039;.$classFileName;
	echo &quot;Auto-loading Class: $className in $classFileName\n&quot;;
	$classFileName = $classesRootFolder.&#039;/&#039;.$classFileName;
	include_once($classFileName);
}</description>
		<content:encoded><![CDATA[<p>My own derivation from your code:</p>
<p>The following is placed in classes/loader.inc.php and included via require_once(&#8216;classes/loader.inc.php&#8217;);</p>
<p>It will turn this<br />
new TagStorageData()<br />
into<br />
require_once(&#8216;classes/tag/storage/tag_storage_data.class.php&#8217;);</p>
<p>and<br />
new ITagStorageData()<br />
into<br />
require_once(&#8216;classes/tag/storage/tag_storage_data.interface.php&#8217;);</p>
<p>define(&#8216;CLASS_EXT&#8217;, &#8216;.class.php&#8217;);<br />
define(&#8216;INTERFACE_EXT&#8217;, &#8216;.interface.php&#8217;);</p>
<p>function __autoload ($className) {<br />
	loadClass($className);<br />
}</p>
<p>function loadClass ($className) {<br />
	$classesRootFolder = dirname(__FILE__);<br />
	$classFileExtension = &#8221;;<br />
	if(preg_match(&#8216;/^I[A-Z]/&#8217;, $className)) { // Interface<br />
		$classFileExtension = INTERFACE_EXT;<br />
		$className = preg_replace(&#8216;/^I([A-Z])/&#8217;, &#8216;$1&#8242;, $className);<br />
	} else { // Class<br />
		$classFileExtension = CLASS_EXT;<br />
	}<br />
	$parts = preg_split(&#8216;/(?&lt;!^)(?=[A-Z])/&#039;, $className);<br />
	$classFileName = strtolower(implode(&#039;_&#039;, $parts).$classFileExtension);<br />
	array_pop($parts); // Drop final element<br />
	$classFileName = strtolower(implode(&#039;/&#039;, $parts)).&#039;/&#039;.$classFileName;<br />
	echo &quot;Auto-loading Class: $className in $classFileName\n&quot;;<br />
	$classFileName = $classesRootFolder.&#039;/&#039;.$classFileName;<br />
	include_once($classFileName);<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jacob Wright</title>
		<link>http://jacwright.com/blog/40/php-package-management-and-autoloading/comment-page-1/#comment-8983</link>
		<dc:creator>Jacob Wright</dc:creator>
		<pubDate>Thu, 07 Jan 2010 05:16:29 +0000</pubDate>
		<guid isPermaLink="false">http://www.jacwright.com/blog/?p=40#comment-8983</guid>
		<description>anisa: copy/paste into a file.</description>
		<content:encoded><![CDATA[<p>anisa: copy/paste into a file.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: anisa</title>
		<link>http://jacwright.com/blog/40/php-package-management-and-autoloading/comment-page-1/#comment-8976</link>
		<dc:creator>anisa</dc:creator>
		<pubDate>Sun, 03 Jan 2010 11:53:38 +0000</pubDate>
		<guid isPermaLink="false">http://www.jacwright.com/blog/?p=40#comment-8976</guid>
		<description>how i can get the php package dawnlode</description>
		<content:encoded><![CDATA[<p>how i can get the php package dawnlode</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jacob Wright</title>
		<link>http://jacwright.com/blog/40/php-package-management-and-autoloading/comment-page-1/#comment-8972</link>
		<dc:creator>Jacob Wright</dc:creator>
		<pubDate>Fri, 01 Jan 2010 00:06:24 +0000</pubDate>
		<guid isPermaLink="false">http://www.jacwright.com/blog/?p=40#comment-8972</guid>
		<description>This is under the creative commons Attribution license (http://creativecommons.org/licenses/by/3.0/).</description>
		<content:encoded><![CDATA[<p>This is under the creative commons Attribution license (<a href="http://creativecommons.org/licenses/by/3.0/" rel="nofollow">http://creativecommons.org/licenses/by/3.0/</a>).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jon Dean</title>
		<link>http://jacwright.com/blog/40/php-package-management-and-autoloading/comment-page-1/#comment-8971</link>
		<dc:creator>Jon Dean</dc:creator>
		<pubDate>Thu, 31 Dec 2009 22:22:46 +0000</pubDate>
		<guid isPermaLink="false">http://www.jacwright.com/blog/?p=40#comment-8971</guid>
		<description>Thanks, this is very cool. Is it available for use via a public license? Without one explicitly given it&#039;s technically not legal to reuse since you can claim copyright at any time (an implicit copyright.)

A few example licenses:

MIT License allows for most use but you must include the copyright notice every time: http://www.opensource.org/licenses/mit-license.php

Public domain has no restrictions at all: http://creativecommons.org/licenses/publicdomain/

Anyway, thanks a lot!</description>
		<content:encoded><![CDATA[<p>Thanks, this is very cool. Is it available for use via a public license? Without one explicitly given it&#8217;s technically not legal to reuse since you can claim copyright at any time (an implicit copyright.)</p>
<p>A few example licenses:</p>
<p>MIT License allows for most use but you must include the copyright notice every time: <a href="http://www.opensource.org/licenses/mit-license.php" rel="nofollow">http://www.opensource.org/licenses/mit-license.php</a></p>
<p>Public domain has no restrictions at all: <a href="http://creativecommons.org/licenses/publicdomain/" rel="nofollow">http://creativecommons.org/licenses/publicdomain/</a></p>
<p>Anyway, thanks a lot!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Benux</title>
		<link>http://jacwright.com/blog/40/php-package-management-and-autoloading/comment-page-1/#comment-8657</link>
		<dc:creator>Benux</dc:creator>
		<pubDate>Sat, 17 Oct 2009 17:55:06 +0000</pubDate>
		<guid isPermaLink="false">http://www.jacwright.com/blog/?p=40#comment-8657</guid>
		<description>well i repeat the code in another for cause the system erase the name tag after the &quot;?&quot; haha, hopes this comes useful

if (preg_match(’/^[A-Z](.*).php$/’, $file,$class)) {
$imports[$class[1]] = “$folder/$file”;
}

thanks again.</description>
		<content:encoded><![CDATA[<p>well i repeat the code in another for cause the system erase the name tag after the &#8220;?&#8221; haha, hopes this comes useful</p>
<p>if (preg_match(’/^[A-Z](.*).php$/’, $file,$class)) {<br />
$imports[$class[1]] = “$folder/$file”;<br />
}</p>
<p>thanks again.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Benux</title>
		<link>http://jacwright.com/blog/40/php-package-management-and-autoloading/comment-page-1/#comment-8656</link>
		<dc:creator>Benux</dc:creator>
		<pubDate>Sat, 17 Oct 2009 17:48:10 +0000</pubDate>
		<guid isPermaLink="false">http://www.jacwright.com/blog/?p=40#comment-8656</guid>
		<description>thanks, great job, this really gave me some good ideas for my projects, its very interesting.

for that thing of &quot;*&quot; you could use some regular expressions instead of strrpos to make it more restrictive with the uppercase convention and the &quot;.php&quot; ending extension, something like this:

if (preg_match(&#039;/^[A-Z](?.*).php$/&#039;, $file,$class)) {
   $imports[$class[&#039;name&#039;]] = &quot;$folder/$file&quot;;
}</description>
		<content:encoded><![CDATA[<p>thanks, great job, this really gave me some good ideas for my projects, its very interesting.</p>
<p>for that thing of &#8220;*&#8221; you could use some regular expressions instead of strrpos to make it more restrictive with the uppercase convention and the &#8220;.php&#8221; ending extension, something like this:</p>
<p>if (preg_match(&#8216;/^[A-Z](?.*).php$/&#8217;, $file,$class)) {<br />
   $imports[$class['name']] = &#8220;$folder/$file&#8221;;<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jacob Wright</title>
		<link>http://jacwright.com/blog/40/php-package-management-and-autoloading/comment-page-1/#comment-8655</link>
		<dc:creator>Jacob Wright</dc:creator>
		<pubDate>Mon, 12 Oct 2009 21:44:30 +0000</pubDate>
		<guid isPermaLink="false">http://www.jacwright.com/blog/?p=40#comment-8655</guid>
		<description>if you do an import(&#039;mypackage.*&#039;); it will &quot;register&quot; all the filenames so that when a class is referenced it can know where to include() the PHP from. include() only happens when actually using the class you need. However, using &quot;*&quot; in the import will scan the directory for the filenames, so use sparingly.</description>
		<content:encoded><![CDATA[<p>if you do an import(&#8216;mypackage.*&#8217;); it will &#8220;register&#8221; all the filenames so that when a class is referenced it can know where to include() the PHP from. include() only happens when actually using the class you need. However, using &#8220;*&#8221; in the import will scan the directory for the filenames, so use sparingly.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
