<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>code-diesel &#187; package</title>
	<atom:link href="http://www.codediesel.com/tag/package/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.codediesel.com</link>
	<description>/* PHP &#38; MySQL Journal */</description>
	<lastBuildDate>Thu, 02 Feb 2012 13:19:04 +0000</lastBuildDate>
	<language>en</language>
	<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>Simulating Packages in PHP</title>
		<link>http://www.codediesel.com/php/simulating-packages-in-php/</link>
		<comments>http://www.codediesel.com/php/simulating-packages-in-php/#comments</comments>
		<pubDate>Thu, 05 Nov 2009 06:36:15 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[software]]></category>
		<category><![CDATA[tip]]></category>
		<category><![CDATA[oop]]></category>
		<category><![CDATA[package]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=1842</guid>
		<description><![CDATA[how to simulate java like import packages in php]]></description>
			<content:encoded><![CDATA[<p>Most modern languages have a concept of packages, wherein related classes are stored together. PHP sadly doesn&#8217;t have a similar concept. For example in Java we can use the following line to imports all classes from the java.awt.event package.</p>

<div class="wp_codebox"><table><tr id="p18421"><td class="code" id="p1842code1"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.awt.event.*</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p><span id="more-1842"></span><br />
But in the spirit of &#8216;programming into the language&#8217;, what we can do is try to simulate a &#8216;package-like&#8217; concept in PHP. Take the following directory structure.<br />
<a href="http://www.codediesel.com/wp-content/uploads/2009/11/package_dir_struct1.gif"><img src="http://www.codediesel.com/wp-content/uploads/2009/11/package_dir_struct1.gif" alt="package_dir_struct1" title="package_dir_struct1" width="273" height="180" class="aligncenter size-full wp-image-1849" /></a></p>
<p>So now if we want to import the &#8216;login.php&#8217; file in our code we must be able to do the following with a newly created &#8216;import&#8217; function:</p>

<div class="wp_codebox"><table><tr id="p18422"><td class="code" id="p1842code2"><pre class="php" style="font-family:monospace;">import<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;com.codediesel.Security.login&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>This will include the &#8216;login.php&#8217; file from the &#8216;com/codediesel/Security&#8217; directory. To include all the files from the &#8216;Security&#8217; directory we can do the following:</p>

<div class="wp_codebox"><table><tr id="p18423"><td class="code" id="p1842code3"><pre class="php" style="font-family:monospace;">import<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;com.codediesel.Security.*);</span></pre></td></tr></table></div>

<p> As you can see that this is actually quite different from what the actual package concept implies, which is to import <em>related classes</em>. </p>
<p>Anyways, taking it as a conceptual idea only the code for the import function is shown below:</p>

<div class="wp_codebox"><table><tr id="p18424"><td class="code" id="p1842code4"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000088;">$basePath</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;./&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> import<span style="color: #009900;">&#40;</span><span style="color: #000088;">$classPath</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$basePath</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/* If the path ends with a '.*' then include
       all the files in the last given directory.
    */</span>
    <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/(\.\*)$/'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$classPath</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$importFilePath</span> <span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$classPath</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$classPath</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$importFilePath</span> <span style="color: #339933;">=</span> <span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;.&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;/&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$importFilePath</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$d</span><span style="color: #339933;">=</span> <span style="color: #990000;">dir</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$basePath</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$importFilePath</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">while</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">false</span> <span style="color: #339933;">!==</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$d</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">read</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> 
        <span style="color: #009900;">&#123;</span>
            <span style="color: #009933; font-style: italic;">/* Reject parent, current directories and sub directories */</span>
            <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'.'</span><span style="color: #009900;">&#41;</span>  <span style="color: #339933;">||</span>
               <span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'..'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span>
               <span style="color: #009900;">&#40;</span><span style="color: #990000;">is_dir</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$d</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">path</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;/&quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$file</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
            <span style="color: #009900;">&#123;</span>
                <span style="color: #000000; font-weight: bold;">continue</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
&nbsp;
            <span style="color: #000000; font-weight: bold;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$basePath</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$importFilePath</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;/&quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$file</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #009933; font-style: italic;">/* If a single file is specified */</span>
        <span style="color: #000088;">$importFile</span> <span style="color: #339933;">=</span> <span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;.&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;/&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$classPath</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;.php&quot;</span><span style="color: #339933;">;</span>
    	<span style="color: #000000; font-weight: bold;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$basePath</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$importFile</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Please note that this is just an idea. Detailed error handling is not included to keep the code conceptually simple to understand. It would be fun if readers could bounce around some thoughts.</p>
<h4>Now the Problems</h4>
<p>The first main problem is that this is not exactly what the package concept implies. What we are essentially doing is dressing-up the &#8216;include&#8217; concept in new clothes.</p>
<p>The second is that for multiple file inclusion as shown above, we cannot predict in which sequence the files will be included, which can raise some dependency issues.</p>
<p>Including a large number of files can incur a small performance hit. This can be better handled if developed as a PECL extension.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/php/simulating-packages-in-php/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>

