<?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: Simulating Packages in PHP</title>
	<atom:link href="http://www.codediesel.com/php/simulating-packages-in-php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.codediesel.com/php/simulating-packages-in-php/</link>
	<description>/* PHP &#38; MySQL Journal */</description>
	<lastBuildDate>Fri, 27 Jan 2012 16:39:42 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
	<item>
		<title>By: Catalin</title>
		<link>http://www.codediesel.com/php/simulating-packages-in-php/comment-page-1/#comment-2045</link>
		<dc:creator>Catalin</dc:creator>
		<pubDate>Tue, 08 Jun 2010 10:35:51 +0000</pubDate>
		<guid isPermaLink="false">http://www.codediesel.com/?p=1842#comment-2045</guid>
		<description>I have a similar function and I am happy that I am not the only weirdo that uses such functionality.

I hate puting the namespece in a class names so it is OK to do have import() instrument. I also have a core folder where I put basic classes which are loaded default bu autoload :)</description>
		<content:encoded><![CDATA[<p>I have a similar function and I am happy that I am not the only weirdo that uses such functionality.</p>
<p>I hate puting the namespece in a class names so it is OK to do have import() instrument. I also have a core folder where I put basic classes which are loaded default bu autoload <img src='http://www.codediesel.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nicolas BUI</title>
		<link>http://www.codediesel.com/php/simulating-packages-in-php/comment-page-1/#comment-1499</link>
		<dc:creator>Nicolas BUI</dc:creator>
		<pubDate>Thu, 05 Nov 2009 23:45:33 +0000</pubDate>
		<guid isPermaLink="false">http://www.codediesel.com/?p=1842#comment-1499</guid>
		<description>Hi,

I had my own library that has lots of features from Java. And &quot;import&quot; is one of primary features.

In real life use, I&#039;ve split to different method as follow :

class Tine {
  static function importClass( $class )
    {
        $class_path = trim( strtr( $class, &#039;.*&#039;, &#039;/ &#039; ) );
        $class_name = strtolower( basename( $class_path ) );
        $class_path = TINE_CLASS_PATH . &#039;/&#039; . $class_path . &#039;.php&#039;;
        if ( file_exists( $class_path ) &amp;&amp; is_file( $class_path ) &amp;&amp; !class_exists( $class_name ) )
        {
            require( $class_path );
        }
    }
    
    static function importPackage( $package )
    {
        $class_path = TINE_CLASS_PATH . &#039;/&#039;.trim( strtr( $package, &#039;.*&#039;, &#039;/ &#039;) );
        self::importFiles( $class_path );
    }
    
   static function importFiles( $folder, $create_instance = false )
    {
        if ( file_exists( $folder ) &amp;&amp; is_dir( $folder ) ) 
        {
        	$append = substr( $folder, -1, 1 ) == &#039;/&#039; ? &#039;&#039; : &#039;/&#039;;
            $handle = opendir( $folder );
            if ( is_resource( $handle ) )
            {
                while ( true == ( $file = readdir( $handle ) ) ) 
                {
                	$full_file = $folder . $append . $file;
                	if ( is_file( $full_file ) )
                	{
	                    $res        = null;
	                    $ext        = substr( $file, -4, 4 );
	                    $class_name = substr( $file, 0, -4 );
	                    if ( 0 == strcasecmp( $ext, &#039;.php&#039; ) ) 
	                    {
	                        $class_exists = class_exists( $class_name );
	                        if ( !$class_exists )
	                        {
	                        	require( $folder . $append . $file );
	                        }
	                        if ( $create_instance &amp;&amp; $class_exists )
	                        {
	                    		new $res[1];
	                    	}
	                    } 
                	}
                }
                closedir( $handle );
            }
            return;
        }
    }
    
    static function importFolder( $folder, $create_instance = false )
    {
        self::importFiles( $folder, $create_instance );
    }
    
    static function importFile( $file, $once = true )
    {
    	if(  $once )
    		require_once( $file );
    	else 
    		require( $file );
    }
    
    static function import($class)
    {
        $class = str_replace( &#039; &#039;, &#039;&#039;, $class );
        $pos = strpos( $class, &#039;/&#039; );
        if ( $pos === false )
        {
            if ( $class{ strlen( $class ) - 1 } == &#039;*&#039; ) 
                self::importPackage( $class );
            else
                self::importClass( $class );
        }
        else
        {
            if ( is_dir( $class ) ) 
                self::importFiles( $class );
            else
                self::importFile( $class );
        }
    } 
}
&lt;/code&gt;

Most of application developped for my clients, use more than 100 classes. So to gain performance, I&#039;ve found a little way to load faster : As the main goal is to load fewer files, i use a import tracker to generate a file containing most imported files, something like:
&lt;code&gt;
static function trackFile( $path ) {
    	if ( defined(&#039;TINE_CLASS_TRACKER&#039;) &amp;&amp; file_exists( $path ) ) { 
    		global $trackers; $trackers[ $path ] = md5_file( $path ); 
    	}
    }
&lt;/code&gt;</description>
		<content:encoded><![CDATA[<p>Hi,</p>
<p>I had my own library that has lots of features from Java. And &#8220;import&#8221; is one of primary features.</p>
<p>In real life use, I&#8217;ve split to different method as follow :</p>
<p>class Tine {<br />
  static function importClass( $class )<br />
    {<br />
        $class_path = trim( strtr( $class, &#8216;.*&#8217;, &#8216;/ &#8216; ) );<br />
        $class_name = strtolower( basename( $class_path ) );<br />
        $class_path = TINE_CLASS_PATH . &#8216;/&#8217; . $class_path . &#8216;.php&#8217;;<br />
        if ( file_exists( $class_path ) &amp;&amp; is_file( $class_path ) &amp;&amp; !class_exists( $class_name ) )<br />
        {<br />
            require( $class_path );<br />
        }<br />
    }</p>
<p>    static function importPackage( $package )<br />
    {<br />
        $class_path = TINE_CLASS_PATH . &#8216;/&#8217;.trim( strtr( $package, &#8216;.*&#8217;, &#8216;/ &#8216;) );<br />
        self::importFiles( $class_path );<br />
    }</p>
<p>   static function importFiles( $folder, $create_instance = false )<br />
    {<br />
        if ( file_exists( $folder ) &amp;&amp; is_dir( $folder ) )<br />
        {<br />
        	$append = substr( $folder, -1, 1 ) == &#8216;/&#8217; ? &#8221; : &#8216;/&#8217;;<br />
            $handle = opendir( $folder );<br />
            if ( is_resource( $handle ) )<br />
            {<br />
                while ( true == ( $file = readdir( $handle ) ) )<br />
                {<br />
                	$full_file = $folder . $append . $file;<br />
                	if ( is_file( $full_file ) )<br />
                	{<br />
	                    $res        = null;<br />
	                    $ext        = substr( $file, -4, 4 );<br />
	                    $class_name = substr( $file, 0, -4 );<br />
	                    if ( 0 == strcasecmp( $ext, &#8216;.php&#8217; ) )<br />
	                    {<br />
	                        $class_exists = class_exists( $class_name );<br />
	                        if ( !$class_exists )<br />
	                        {<br />
	                        	require( $folder . $append . $file );<br />
	                        }<br />
	                        if ( $create_instance &amp;&amp; $class_exists )<br />
	                        {<br />
	                    		new $res[1];<br />
	                    	}<br />
	                    }<br />
                	}<br />
                }<br />
                closedir( $handle );<br />
            }<br />
            return;<br />
        }<br />
    }</p>
<p>    static function importFolder( $folder, $create_instance = false )<br />
    {<br />
        self::importFiles( $folder, $create_instance );<br />
    }</p>
<p>    static function importFile( $file, $once = true )<br />
    {<br />
    	if(  $once )<br />
    		require_once( $file );<br />
    	else<br />
    		require( $file );<br />
    }</p>
<p>    static function import($class)<br />
    {<br />
        $class = str_replace( &#8216; &#8216;, &#8221;, $class );<br />
        $pos = strpos( $class, &#8216;/&#8217; );<br />
        if ( $pos === false )<br />
        {<br />
            if ( $class{ strlen( $class ) &#8211; 1 } == &#8216;*&#8217; )<br />
                self::importPackage( $class );<br />
            else<br />
                self::importClass( $class );<br />
        }<br />
        else<br />
        {<br />
            if ( is_dir( $class ) )<br />
                self::importFiles( $class );<br />
            else<br />
                self::importFile( $class );<br />
        }<br />
    }<br />
}</p>
<p>Most of application developped for my clients, use more than 100 classes. So to gain performance, I've found a little way to load faster : As the main goal is to load fewer files, i use a import tracker to generate a file containing most imported files, something like:<br />
<code><br />
static function trackFile( $path ) {<br />
    	if ( defined('TINE_CLASS_TRACKER') &amp;&amp; file_exists( $path ) ) {<br />
    		global $trackers; $trackers[ $path ] = md5_file( $path );<br />
    	}<br />
    }<br />
</code></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ian</title>
		<link>http://www.codediesel.com/php/simulating-packages-in-php/comment-page-1/#comment-1498</link>
		<dc:creator>Ian</dc:creator>
		<pubDate>Thu, 05 Nov 2009 21:31:22 +0000</pubDate>
		<guid isPermaLink="false">http://www.codediesel.com/?p=1842#comment-1498</guid>
		<description>You may also want to look int PHAR packages, which have been around for a while as a PECL extension, but the functionality is baked in to PHP 5.3 by default now.  Granted the concept is more or less jars for PHP, but you can use them to bundle similar classes together and load them in one fell swoop if you&#039;re clever about it.

Also, it might also be worth looking into SPL autoloaders rather than the vanilla __autoload magic if you&#039;re not familiar with them.  You could write specific functions that load classes from well known file system hierarchies, thus eliminating the need for an &quot;import&quot; kinda thing, but still being able to maintain the java-like package structure you enjoy.  The nice thing about SPL auto loaders is that you can define as many as you want, and PHP will execute all the autoloaders you&#039;ve defined and registered before throwing up a class doesn&#039;t exist error (to put it crudely).  They&#039;re also faster than regular ol&#039; __autoload, so there&#039;s no huge penalty (outside filesystem access) for using them :)

That&#039;s my 2 cents</description>
		<content:encoded><![CDATA[<p>You may also want to look int PHAR packages, which have been around for a while as a PECL extension, but the functionality is baked in to PHP 5.3 by default now.  Granted the concept is more or less jars for PHP, but you can use them to bundle similar classes together and load them in one fell swoop if you&#8217;re clever about it.</p>
<p>Also, it might also be worth looking into SPL autoloaders rather than the vanilla __autoload magic if you&#8217;re not familiar with them.  You could write specific functions that load classes from well known file system hierarchies, thus eliminating the need for an &#8220;import&#8221; kinda thing, but still being able to maintain the java-like package structure you enjoy.  The nice thing about SPL auto loaders is that you can define as many as you want, and PHP will execute all the autoloaders you&#8217;ve defined and registered before throwing up a class doesn&#8217;t exist error (to put it crudely).  They&#8217;re also faster than regular ol&#8217; __autoload, so there&#8217;s no huge penalty (outside filesystem access) for using them <img src='http://www.codediesel.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>That&#8217;s my 2 cents</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Giorgio Sironi</title>
		<link>http://www.codediesel.com/php/simulating-packages-in-php/comment-page-1/#comment-1496</link>
		<dc:creator>Giorgio Sironi</dc:creator>
		<pubDate>Thu, 05 Nov 2009 18:29:41 +0000</pubDate>
		<guid isPermaLink="false">http://www.codediesel.com/?p=1842#comment-1496</guid>
		<description>I also think now you can implement the package structure with namespaces (Doctrine\Entity\Manager). Though, it was already present as a convention for class names (Zend_Auth_Adapter for instance).</description>
		<content:encoded><![CDATA[<p>I also think now you can implement the package structure with namespaces (Doctrine\Entity\Manager). Though, it was already present as a convention for class names (Zend_Auth_Adapter for instance).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: jozef</title>
		<link>http://www.codediesel.com/php/simulating-packages-in-php/comment-page-1/#comment-1493</link>
		<dc:creator>jozef</dc:creator>
		<pubDate>Thu, 05 Nov 2009 15:23:18 +0000</pubDate>
		<guid isPermaLink="false">http://www.codediesel.com/?p=1842#comment-1493</guid>
		<description>Doctrine 2.0 has class loader for PHP 5.3 namespace separator &quot;\&quot; . It is also compatible with Zend/Pear naming conventions.
http://www.doctrine-project.org
look at file Doctrine\Common\ClassLoader.php</description>
		<content:encoded><![CDATA[<p>Doctrine 2.0 has class loader for PHP 5.3 namespace separator &#8220;\&#8221; . It is also compatible with Zend/Pear naming conventions.<br />
<a href="http://www.doctrine-project.org" rel="nofollow">http://www.doctrine-project.org</a><br />
look at file Doctrine\Common\ClassLoader.php</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: sameer</title>
		<link>http://www.codediesel.com/php/simulating-packages-in-php/comment-page-1/#comment-1492</link>
		<dc:creator>sameer</dc:creator>
		<pubDate>Thu, 05 Nov 2009 15:20:40 +0000</pubDate>
		<guid isPermaLink="false">http://www.codediesel.com/?p=1842#comment-1492</guid>
		<description>To put it plainly, I just needed to bring the Java package concept as-it-is to php, it doesn&#039;t have to have any immediate utility in the daily practices of a programmer. Autoload is a nice thing, albeit somewhat retrofitted into php. Anyways its just an idea, if it doesn&#039;t work than so be it.</description>
		<content:encoded><![CDATA[<p>To put it plainly, I just needed to bring the Java package concept as-it-is to php, it doesn&#8217;t have to have any immediate utility in the daily practices of a programmer. Autoload is a nice thing, albeit somewhat retrofitted into php. Anyways its just an idea, if it doesn&#8217;t work than so be it.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Chris Renner</title>
		<link>http://www.codediesel.com/php/simulating-packages-in-php/comment-page-1/#comment-1490</link>
		<dc:creator>Chris Renner</dc:creator>
		<pubDate>Thu, 05 Nov 2009 15:03:51 +0000</pubDate>
		<guid isPermaLink="false">http://www.codediesel.com/?p=1842#comment-1490</guid>
		<description>Sameer, 

I think the question Topbit raises (though it was not stated this plainly), is why is this necessary or even desirable, given that we already have Autoload.

I&#039;m not saying it is or isn&#039;t, I&#039;d just like to see if there&#039;s an answer.  I&#039;m curious, but I need to understand the utility of your concept.</description>
		<content:encoded><![CDATA[<p>Sameer, </p>
<p>I think the question Topbit raises (though it was not stated this plainly), is why is this necessary or even desirable, given that we already have Autoload.</p>
<p>I&#8217;m not saying it is or isn&#8217;t, I&#8217;d just like to see if there&#8217;s an answer.  I&#8217;m curious, but I need to understand the utility of your concept.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: sameer</title>
		<link>http://www.codediesel.com/php/simulating-packages-in-php/comment-page-1/#comment-1486</link>
		<dc:creator>sameer</dc:creator>
		<pubDate>Thu, 05 Nov 2009 12:13:56 +0000</pubDate>
		<guid isPermaLink="false">http://www.codediesel.com/?p=1842#comment-1486</guid>
		<description>I&#039;m aware of the Autoload functionality in PHP5; what I wanted to do was to somehow replicate the syntax offered by other languages.</description>
		<content:encoded><![CDATA[<p>I&#8217;m aware of the Autoload functionality in PHP5; what I wanted to do was to somehow replicate the syntax offered by other languages.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Topbit</title>
		<link>http://www.codediesel.com/php/simulating-packages-in-php/comment-page-1/#comment-1485</link>
		<dc:creator>Topbit</dc:creator>
		<pubDate>Thu, 05 Nov 2009 11:51:21 +0000</pubDate>
		<guid isPermaLink="false">http://www.codediesel.com/?p=1842#comment-1485</guid>
		<description>This problem was solved some time ago in PHP, and in a manner that can speed up the use of a potential hierarchy of classes, rather than slow it down by including files that are not being used.

It&#039;s AutoLoading, as demonstrated with such tools as Zend_Loader, part of Zend Framework (though it can be used separately to the rest of it)</description>
		<content:encoded><![CDATA[<p>This problem was solved some time ago in PHP, and in a manner that can speed up the use of a potential hierarchy of classes, rather than slow it down by including files that are not being used.</p>
<p>It&#8217;s AutoLoading, as demonstrated with such tools as Zend_Loader, part of Zend Framework (though it can be used separately to the rest of it)</p>
]]></content:encoded>
	</item>
</channel>
</rss>

