<?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>PHP, CSS, HTML, Web &#38; beyond &#187; PHP</title>
	<atom:link href="http://www.joseairosa.com/tag/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.joseairosa.com</link>
	<description>Programming Tips &#38; Tricks</description>
	<lastBuildDate>Mon, 23 Jan 2012 12:46:03 +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>PHP Cropimage Script (memcache and APC support)</title>
		<link>http://www.joseairosa.com/2011/08/03/php-cropimage-script-memcache-and-apc-support/</link>
		<comments>http://www.joseairosa.com/2011/08/03/php-cropimage-script-memcache-and-apc-support/#comments</comments>
		<pubDate>Wed, 03 Aug 2011 21:35:24 +0000</pubDate>
		<dc:creator>José P. Airosa</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[apc]]></category>
		<category><![CDATA[compression]]></category>
		<category><![CDATA[cropping]]></category>
		<category><![CDATA[images]]></category>
		<category><![CDATA[memcache]]></category>
		<category><![CDATA[optimization]]></category>
		<category><![CDATA[rewrite rules]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[speed]]></category>
		<category><![CDATA[website optimization]]></category>

		<guid isPermaLink="false">http://www.joseairosa.com/?p=305</guid>
		<description><![CDATA[Hello! &#160; This script will enable you to automaticaly resize images without loosing quality. It uses a simple math formulas to calculate the desired final size and then rerenders the image. The usage is very simples. On you HTML code just create an image and replace the src with: cropimage.php?src=img/myimage.jpg&#38;size=320&#215;240 src &#8211; the source of [...]]]></description>
			<content:encoded><![CDATA[<p>Hello!</p>
<p>&nbsp;</p>
<p>This script will enable you to automaticaly resize images without loosing quality.<br />
It uses a simple math formulas to calculate the desired final size and then rerenders the image.</p>
<p>The usage is very simples. On you HTML code just create an image and replace the src with:</p>
<p>cropimage.php?src=img/myimage.jpg&amp;size=320&#215;240</p>
<p>src &#8211; the source of the image itself<br />
size &#8211; the size that the image should have. If the size is not directly compatible with the original size it will display a simple background and the image with the correct size on top.</p>
<p><em>Example: cropimage.php?src=img/myimage.jpg&amp;size=320&#215;240</em></p>
<p>If it&#8217;s a square image you can just use <strong>myimage.jpg&amp;size=320</strong> and it will make a 320&#215;320 image.</p>
<p>If you don&#8217;t want that nasty name (I wouldn&#8217;t <img src='http://www.joseairosa.com/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> ) you can add a rewite rule on your htaccess file:</p>
<blockquote><p>RewriteRule ^images\/crop\/([^/]+)\/([^/]+)\/(.+)$ cropimage.php?src=$3&amp;size=$2&amp;id=$1&amp;rewrite=1 [L,QSA]</p></blockquote>
<p>With this rule you can use something like: http://www.example.com/images/crop/1234/320&#215;180/my-awesome-image.png</p>
<p>Now on the script itself you need to do some adjustments on line &#8220;$_GET['src'] = &#8216;img/&#8217;.$_GET['id'].&#8217;/&#8217;.$_GET['src'];&#8221; where you point the image name to the respective folder.</p>
<p>If you want the original size just call it like this:</p>
<p>http://www.example.com/images/crop/1234/original/my-awesome-image.png</p>
<p>Note: using an ID is very important since it will prevent image overwriting.</p>
<p>&nbsp;</p>
<h2>Using with memcache:</h2>
<p>This script works great with memcache and apc, and it&#8217;s highly recommended!<br />
For that just set &#8220;define(&#8220;USE_MEMCACHE&#8221;, true);&#8221; to true (as it is on this example).</p>
<p>Using with APC:</p>
<p>This script works great with memcache and apc, and it&#8217;s highly recommended!<br />
For that just set &#8220;define(&#8220;USE_APC&#8221;, true);&#8221; to true (as it is on this example).</p>
<p>&nbsp;</p>
<h2>Watermark</h2>
<p>This script also supports a watermark.<br />
If you want to have a go with the watermark just set &#8220;define(&#8220;USE_WATERMARK&#8221;, false);&#8221; to true and set the path to the watermark in &#8220;define(&#8220;WATERMARK&#8221;,&#8221;);&#8221;.</p>
<p>&nbsp;</p>
<h2>Fork me @ <a title="Fork me at github" title='Original Link: http://bit.ly/odG98h'  href="http://www.joseairosa.com/?ZIRorxnx">http://bit.ly/odG98h</a></h2>
<p>&nbsp;</p>
<h2>Examples</h2>
<p>You can see it in action here: http://www.porvocacao.com<br />
In this website we&#8217;re using this script in memcache mode.</p>
<p>Let me know your thoughts <img src='http://www.joseairosa.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.joseairosa.com/2011/08/03/php-cropimage-script-memcache-and-apc-support/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Screencast 1 &#8211; mod_rewrite and PHP</title>
		<link>http://www.joseairosa.com/2009/08/03/screencast-1-mod_rewrite-and-php/</link>
		<comments>http://www.joseairosa.com/2009/08/03/screencast-1-mod_rewrite-and-php/#comments</comments>
		<pubDate>Mon, 03 Aug 2009 15:43:07 +0000</pubDate>
		<dc:creator>José P. Airosa</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Screencast]]></category>
		<category><![CDATA[mod_rewrite]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.joseairosa.com/?p=101</guid>
		<description><![CDATA[<object width="550" height="312"><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id=5910332&#38;server=vimeo.com&#38;show_title=1&#38;show_byline=1&#38;show_portrait=0&#38;color=&#38;fullscreen=1" /><embed src="http://vimeo.com/moogaloop.swf?clip_id=5910332&#38;server=vimeo.com&#38;show_title=1&#38;show_byline=1&#38;show_portrait=0&#38;color=&#38;fullscreen=1" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="550" height="312"></embed></object><p><a href="http://vimeo.com/5910332">Screencast - mod_rewrite and PHP</a> from <a href="http://vimeo.com/user2086629">José Airosa</a> on <a href="http://vimeo.com">Vimeo</a>.</p>]]></description>
			<content:encoded><![CDATA[<p><object width="550" height="312"><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id=5910332&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1" /><embed src="http://vimeo.com/moogaloop.swf?clip_id=5910332&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="550" height="312"></embed></object>
<p><a title='Original Link: http://vimeo.com/5910332'  href="http://www.joseairosa.com/?BQM9HrN0">Screencast &#8211; mod_rewrite and PHP</a> from <a title='Original Link: http://vimeo.com/user2086629'  href="http://www.joseairosa.com/?WpMUt2Gj">José Airosa</a> on <a title='Original Link: http://vimeo.com'  href="http://www.joseairosa.com/?tz2GLZGJ">Vimeo</a>.</p>
<p>Hello folks!</p>
<p>This time, and for the first time, I bring you a screencast! It&#8217;s all about mod_rewrite and PHP. What it is, how can we use it and how can we integrate it to be used with PHP.</p>
<p>Below are the functions that i talked about in the screencast. Use them, or not, as you please.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p101code1'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p1011"><td class="code" id="p101code1"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * Get current page URL
 * 
 * @return string with paeg name
 */</span>
<span style="color: #000000; font-weight: bold;">function</span> curPageURL<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$pageURL</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'http'</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><a href="http://www.php.net/isset"><span style="color: #990000;">isset</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;HTTPS&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;HTTPS&quot;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">&quot;on&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #000088;">$pageURL</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;s&quot;</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span>
		<span style="color: #000088;">$pageURL</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;://&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;SERVER_PORT&quot;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">!=</span> <span style="color: #0000ff;">&quot;80&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$pageURL</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;SERVER_NAME&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;:&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;SERVER_PORT&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;REQUEST_URI&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$pageURL</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;SERVER_NAME&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;REQUEST_URI&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #000088;">$pageURL</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * Get current page script name
 * 
 * @return string with page script name
 */</span>
<span style="color: #000000; font-weight: bold;">function</span> curPageScriptName<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
 <span style="color: #b1b100;">return</span> <a href="http://www.php.net/substr"><span style="color: #990000;">substr</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;SCRIPT_NAME&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><a href="http://www.php.net/strrpos"><span style="color: #990000;">strrpos</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;SCRIPT_NAME&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;/&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * Get current page name
 * 
 * @return string with page script name
 */</span>
<span style="color: #000000; font-weight: bold;">function</span> curPageName<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
 <span style="color: #b1b100;">return</span> <a href="http://www.php.net/end"><span style="color: #990000;">end</span></a><span style="color: #009900;">&#40;</span><a href="http://www.php.net/explode"><span style="color: #990000;">explode</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/&quot;</span><span style="color: #339933;">,</span>curPageURL<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: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Hope you&#8217;ll enjoy as much as I did making it <img src='http://www.joseairosa.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Let me know your feedback.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.joseairosa.com/2009/08/03/screencast-1-mod_rewrite-and-php/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>A good way to create Logs in PHP</title>
		<link>http://www.joseairosa.com/2009/02/10/a-good-way-to-create-logs-in-php/</link>
		<comments>http://www.joseairosa.com/2009/02/10/a-good-way-to-create-logs-in-php/#comments</comments>
		<pubDate>Tue, 10 Feb 2009 14:04:59 +0000</pubDate>
		<dc:creator>José P. Airosa</dc:creator>
				<category><![CDATA[Classes]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[classes]]></category>

		<guid isPermaLink="false">http://localhost:8888/Blog/wordpress/?p=7</guid>
		<description><![CDATA[<img src="http://www.joseairosa.com/wp-content/themes/BlueGrey/images/post2.png" alt="A good way to create Logs in PHP" />

Hello everyone :)

Today we're gonna go over Logging in PHP as nested <a href="http://pt.php.net/zend-engine-2.php">Class</a>.

With the introduction of <a href="http://www.php.net/">PHP5</a> we now get the opportunity to use PHP as an <a href="http://en.wikipedia.org/wiki/Object-oriented_programming_language">Object Oriented Programming Language</a>. What this means is, and in a very short way, that we can now have a more reliable, faster and safer way to program in PHP.

OO programming brings <a href="http://en.wikipedia.org/wiki/Abstraction_(computer_science)">abstraction</a>, <a href="http://en.wikipedia.org/wiki/Information_hiding">encapsulation</a>, <a href="http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)">inheritance</a>, and <a href="http://en.wikipedia.org/wiki/Polymorphism_(computer_science)">polymorphism</a> so, therefore, PHP also does.]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.joseairosa.com/wp-content/themes/BlueGrey/images/post2.png" alt="A good way to create Logs in PHP" /></p>
<p>Hello everyone <img src='http://www.joseairosa.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Today we&#8217;re gonna go over Logging in PHP as nested <a title='Original Link: http://pt.php.net/zend-engine-2.php'  href="http://www.joseairosa.com/?M7h5Abzu">Class</a>.</p>
<p>With the introduction of <a title='Original Link: http://www.php.net/'  href="http://www.joseairosa.com/?F668TupL">PHP5</a> we now get the opportunity to use PHP as an <a title='Original Link: http://en.wikipedia.org/wiki/Object-oriented_programming_language'  href="http://www.joseairosa.com/?bfp5Pm9q">Object Oriented Programming Language</a>. What this means is, and in a very short way, that we can now have a more reliable, faster and safer way to program in PHP.</p>
<p>OO programming brings <a title='Original Link: http://en.wikipedia.org/wiki/Abstraction_(computer_science)'  href="http://www.joseairosa.com/?ACWiS0Fu">abstraction</a>, <a title='Original Link: http://en.wikipedia.org/wiki/Information_hiding'  href="http://www.joseairosa.com/?WMpmOOGi">encapsulation</a>, <a title='Original Link: http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)'  href="http://www.joseairosa.com/?KCgtWv4X">inheritance</a>, and <a title='Original Link: http://en.wikipedia.org/wiki/Polymorphism_(computer_science)'  href="http://www.joseairosa.com/?AiSevQvV">polymorphism</a> so, therefore, PHP also does.</p>
<ul>
<li>Abstraction &#8211; Allows the programmer to declare abstract methods that can be instantiated in a class and declared by an object.</li>
<li>Encapsulation &#8211; Gives us the ability to protect which ever data, inside a class, by using protected methods like private classes ou functions.</li>
<li>Inheritance &#8211; We can now create tree like class structure where the lower levels on the tree are childes of the level above. (in this Logging example we&#8217;re going to be using this methodology)</li>
<li>Polymorphism &#8211; Using this feature we&#8217;re able to declare a given class and use his children methods even tho we have declared parent class without the need to redeclare them.</li>
</ul>
<p>First of all we&#8217;re going to go over the basic structure of our Log class.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p7code2'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p72"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p7code2"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">interface</span> iLogs
<span style="color: #009900;">&#123;</span>
 <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> insereLog<span style="color: #009900;">&#40;</span><span style="color: #000088;">$entrada</span><span style="color: #339933;">,</span><span style="color: #000088;">$tipo</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>We declared an interface. This way the application that will be calling this class is able to read its available content without the need to directly access the class internally.</p>
<blockquote><p><span style="font-weight:bold;">Note:</span> Methods that are declared on the interface need to be equal to the ones declared inside the class itself (either method name or number of arguments required). Otherwise you&#8217;ll get a fatal error from PHP.</p></blockquote>
<p>Next step takes us to declaration of the class itself.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p7code3'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p73"><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code" id="p7code3"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Logs implements iLogs
<span style="color: #009900;">&#123;</span>
 <span style="color: #000000; font-weight: bold;">const</span> VERBOSE <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
 <span style="color: #666666; font-style: italic;">// Max size for log compression -&gt; 1MB</span>
 <span style="color: #000000; font-weight: bold;">private</span> static <span style="color: #000088;">$max_size</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1024000</span><span style="color: #339933;">;</span>
 <span style="color: #666666; font-style: italic;">// Folder in your root where the logs will be stored</span>
 <span style="color: #000000; font-weight: bold;">private</span> static <span style="color: #000088;">$cwd</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'logs/'</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>As you can see, we&#8217;re telling class Logs that it should <span style="font-style:italic;">implement</span> interface iLogs that we previously declared.</p>
<p>Every class needs a constructor and a destructor. Basically what should and should not be done when we initialize the class.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p7code4'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p74"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code" id="p7code4"><pre class="php" style="font-family:monospace;"> <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><a href="http://www.php.net/defined"><span style="color: #990000;">defined</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'LOG_FILE_ADMIN'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
   <a href="http://www.php.net/define"><span style="color: #990000;">define</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'LOG_FILE_ADMIN'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'logAdmin.log'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><a href="http://www.php.net/defined"><span style="color: #990000;">defined</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'LOG_FILE_CLIENTES'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
   <a href="http://www.php.net/define"><span style="color: #990000;">define</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'LOG_FILE_CLIENTES'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'logClientes.log'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><a href="http://www.php.net/defined"><span style="color: #990000;">defined</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'LOG_FILE_WEBSERVICES'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
   <a href="http://www.php.net/define"><span style="color: #990000;">define</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'LOG_FILE_WEBSERVICES'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'logWebServices.log'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #004000;">preparaDirectorias</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #004000;">preparaLogs</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span>
 <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __destruct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
 <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p><a title='Original Link: http://pt2.php.net/manual/en/language.oop5.decon.php'  href="http://www.joseairosa.com/?hh307yjT">__construct</a> and <a title='Original Link: http://pt2.php.net/manual/en/language.oop5.decon.php'  href="http://www.joseairosa.com/?hh307yjT">__destruct</a> use &#8220;__&#8221; since they are native to PHP5 and are automatically called when a class is instantiated.</p>
<p>When the class is built we want to create some constants used throughout the class and we want to check if everything is according to the file structure required.<br />
We also use <span style="font-style:italic;">self</span> to address the actual instantiated class and :: in place of -> because we&#8217;ll be calling them in static mode (no data can be changed).</p>
<p>Our destruct class is empty since there is nothing to be done when the class is destroyed.</p>
<blockquote><p><span style="font-weight:bold;">Note:</span> Normally you can use this function for a DB connection and destruct can close the connection.</p></blockquote>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p7code5'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p75"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code" id="p7code5"><pre class="php" style="font-family:monospace;"> <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> preparaDirectorias<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <a href="http://www.php.net/chdir"><span style="color: #990000;">chdir</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'./'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><a href="http://www.php.net/is_dir"><span style="color: #990000;">is_dir</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;logs&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><a href="http://www.php.net/mkdir"><span style="color: #990000;">mkdir</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'logs'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #b1b100;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> Exception <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;It was no possible to create 'logs' dir&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
  <span style="color: #009900;">&#125;</span>
  <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><a href="http://www.php.net/is_dir"><span style="color: #990000;">is_dir</span></a><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$cwd</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;logs_arquivo&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><a href="http://www.php.net/mkdir"><span style="color: #990000;">mkdir</span></a><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$cwd</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'logs_arquivo'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #b1b100;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> Exception <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;It was no possible to create 'logs_arquivo' dir&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
 <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>We attempt to check if folder structure is properly created, if not we create it.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p7code6'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p76"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
</pre></td><td class="code" id="p7code6"><pre class="php" style="font-family:monospace;"> <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> preparaLogs<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><a href="http://www.php.net/file_exists"><span style="color: #990000;">file_exists</span></a><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$cwd</span><span style="color: #339933;">.</span>LOG_FILE_ADMIN<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
   <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><a href="http://www.php.net/fopen"><span style="color: #990000;">fopen</span></a><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$cwd</span><span style="color: #339933;">.</span>LOG_FILE_ADMIN <span style="color: #339933;">,</span> <span style="color: #0000ff;">'w'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #b1b100;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;It was not possible to create '&quot;</span><span style="color: #339933;">.</span>LOG_FILE_ADMIN <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span> 
  <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><a href="http://www.php.net/filesize"><span style="color: #990000;">filesize</span></a><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$cwd</span><span style="color: #339933;">.</span>LOG_FILE_ADMIN<span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$max_size</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #004000;">compressbz2</span><span style="color: #009900;">&#40;</span>LOG_FILE_ADMIN<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <a href="http://www.php.net/unlink"><span style="color: #990000;">unlink</span></a><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$cwd</span><span style="color: #339933;">.</span>LOG_FILE_ADMIN<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><a href="http://www.php.net/fopen"><span style="color: #990000;">fopen</span></a><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$cwd</span><span style="color: #339933;">.</span>LOG_FILE_ADMIN <span style="color: #339933;">,</span> <span style="color: #0000ff;">'w'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #b1b100;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;It was not possible to create '&quot;</span><span style="color: #339933;">.</span>LOG_FILE_ADMIN <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><a href="http://www.php.net/file_exists"><span style="color: #990000;">file_exists</span></a><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$cwd</span><span style="color: #339933;">.</span>LOG_FILE_CLIENTES <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
   <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><a href="http://www.php.net/fopen"><span style="color: #990000;">fopen</span></a><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$cwd</span><span style="color: #339933;">.</span>LOG_FILE_CLIENTES <span style="color: #339933;">,</span> <span style="color: #0000ff;">'w'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #b1b100;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;It was not possible to create '&quot;</span><span style="color: #339933;">.</span>LOG_FILE_CLIENTES <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><a href="http://www.php.net/filesize"><span style="color: #990000;">filesize</span></a><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$cwd</span><span style="color: #339933;">.</span>LOG_FILE_CLIENTES<span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$max_size</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #004000;">compressbz2</span><span style="color: #009900;">&#40;</span>LOG_FILE_CLIENTES<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <a href="http://www.php.net/unlink"><span style="color: #990000;">unlink</span></a><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$cwd</span><span style="color: #339933;">.</span>LOG_FILE_CLIENTES<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><a href="http://www.php.net/fopen"><span style="color: #990000;">fopen</span></a><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$cwd</span><span style="color: #339933;">.</span>LOG_FILE_CLIENTES <span style="color: #339933;">,</span> <span style="color: #0000ff;">'w'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #b1b100;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;It was not possible to create '&quot;</span><span style="color: #339933;">.</span>LOG_FILE_CLIENTES <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><a href="http://www.php.net/file_exists"><span style="color: #990000;">file_exists</span></a><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$cwd</span><span style="color: #339933;">.</span>LOG_FILE_WEBSERVICES<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
   <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><a href="http://www.php.net/fopen"><span style="color: #990000;">fopen</span></a><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$cwd</span><span style="color: #339933;">.</span>LOG_FILE_WEBSERVICES<span style="color: #339933;">,</span> <span style="color: #0000ff;">'w'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #b1b100;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;It was not possible to create '&quot;</span><span style="color: #339933;">.</span>LOG_FILE_WEBSERVICES<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><a href="http://www.php.net/filesize"><span style="color: #990000;">filesize</span></a><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$cwd</span><span style="color: #339933;">.</span>LOG_FILE_WEBSERVICES<span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$max_size</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #004000;">compressbz2</span><span style="color: #009900;">&#40;</span>LOG_FILE_WEBSERVICES<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <a href="http://www.php.net/unlink"><span style="color: #990000;">unlink</span></a><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$cwd</span><span style="color: #339933;">.</span>LOG_FILE_WEBSERVICES<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><a href="http://www.php.net/fopen"><span style="color: #990000;">fopen</span></a><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$cwd</span><span style="color: #339933;">.</span>LOG_FILE_WEBSERVICES<span style="color: #339933;">,</span> <span style="color: #0000ff;">'w'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #b1b100;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;It was not possible to create '&quot;</span><span style="color: #339933;">.</span>LOG_FILE_WEBSERVICES<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
 <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>We&#8217;re going to check the size of our logs. If they exceed $max_size then we <a title='Original Link: http://en.wikipedia.org/wiki/Data_compression'  href="http://www.joseairosa.com/?_y5NcBWM">compress</a> them and create a new and fresh log file.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p7code7'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p77"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
</pre></td><td class="code" id="p7code7"><pre class="php" style="font-family:monospace;"> <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> compressbz2<span style="color: #009900;">&#40;</span><span style="color: #000088;">$file_name</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$file_pointer</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/fopen"><span style="color: #990000;">fopen</span></a><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$cwd</span><span style="color: #339933;">.</span><span style="color: #000088;">$file_name</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;r&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #666666; font-style: italic;">//Opens the file in readonly,</span>
  <span style="color: #666666; font-style: italic;">//Check for permissions</span>
  <span style="color: #000088;">$file_read</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/fread"><span style="color: #990000;">fread</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file_pointer</span><span style="color: #339933;">,</span> <a href="http://www.php.net/filesize"><span style="color: #990000;">filesize</span></a><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$cwd</span><span style="color: #339933;">.</span><span style="color: #000088;">$file_name</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #666666; font-style: italic;">//Reads the content</span>
  <a href="http://www.php.net/fclose"><span style="color: #990000;">fclose</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file_pointer</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #666666; font-style: italic;">//Closes the file</span>
  <span style="color: #000088;">$actual</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/date"><span style="color: #990000;">date</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;-Ymd-His&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$filename</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$file_name</span><span style="color: #339933;">.</span><span style="color: #000088;">$actual</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;.bz2&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #666666; font-style: italic;">//Opens compression file for writing purpose</span>
  <span style="color: #000088;">$bz</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/bzopen"><span style="color: #990000;">bzopen</span></a><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$cwd</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'logs_arquivo/'</span><span style="color: #339933;">.</span><span style="color: #000088;">$filename</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;w&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #666666; font-style: italic;">//Writes the file</span>
  <a href="http://www.php.net/bzwrite"><span style="color: #990000;">bzwrite</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$bz</span><span style="color: #339933;">,</span> <span style="color: #000088;">$file_read</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #666666; font-style: italic;">//Closes the file</span>
  <a href="http://www.php.net/bzclose"><span style="color: #990000;">bzclose</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$bz</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>A simple function for compressing files. I will not go into a lot of detail on this section. If you find it to be useful please <a title='Original Link: mailto:joseairosa@core-creations.pt'  href="http://www.joseairosa.com/?335OoYAP">contact me</a> and I&#8217;ll make a post about file compression.</p>
<p>And finally we&#8217;re going to create the method that we call when using this class to add a log.</p>
<blockquote><p><span style="font-style:italic;">Note:</span> As you probably noticed (or not <img src='http://www.joseairosa.com/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> ) all other methods (except construct and destruct) are private and therefore can only be called from within the class itself.</p></blockquote>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p7code8'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p78"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
</pre></td><td class="code" id="p7code8"><pre class="php" style="font-family:monospace;"> <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> insereLog<span style="color: #009900;">&#40;</span><span style="color: #000088;">$entrada</span><span style="color: #339933;">,</span><span style="color: #000088;">$tipo</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$tipo</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #000088;">$fp</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/fopen"><span style="color: #990000;">fopen</span></a><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$cwd</span><span style="color: #339933;">.</span>LOG_FILE_ADMIN <span style="color: #339933;">,</span> <span style="color: #0000ff;">'a+'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$fp</span><span style="color: #009900;">&#41;</span>
    <span style="color: #b1b100;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;It was not possible to create '&quot;</span><span style="color: #339933;">.</span>LOG_FILE_ADMIN <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
    <a href="http://www.php.net/fwrite"><span style="color: #990000;">fwrite</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'['</span><span style="color: #339933;">.</span><a href="http://www.php.net/date"><span style="color: #990000;">date</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Y/m/d - H:i:s&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'] '</span><span style="color: #339933;">.</span><span style="color: #000088;">$entrada</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #004000;">DEBUG</span><span style="color: #009900;">&#41;</span>
     <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$entrada</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #b1b100;">elseif</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$tipo</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #000088;">$fp</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/fopen"><span style="color: #990000;">fopen</span></a><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$cwd</span><span style="color: #339933;">.</span>LOG_FILE_CLIENTES <span style="color: #339933;">,</span> <span style="color: #0000ff;">'a+'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$fp</span><span style="color: #009900;">&#41;</span>
    <span style="color: #b1b100;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;It was not possible to create '&quot;</span><span style="color: #339933;">.</span>LOG_FILE_CLIENTES <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
    <a href="http://www.php.net/fwrite"><span style="color: #990000;">fwrite</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'['</span><span style="color: #339933;">.</span><a href="http://www.php.net/date"><span style="color: #990000;">date</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Y/m/d - H:i:s&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'] '</span><span style="color: #339933;">.</span><span style="color: #000088;">$entrada</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #004000;">DEBUG</span><span style="color: #009900;">&#41;</span>
     <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$entrada</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #b1b100;">elseif</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$tipo</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #000088;">$fp</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/fopen"><span style="color: #990000;">fopen</span></a><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$cwd</span><span style="color: #339933;">.</span>LOG_FILE_WEBSERVICES <span style="color: #339933;">,</span> <span style="color: #0000ff;">'a+'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$fp</span><span style="color: #009900;">&#41;</span>
    <span style="color: #b1b100;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;It was not possible to create '&quot;</span><span style="color: #339933;">.</span>LOG_FILE_WEBSERVICES <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
    <a href="http://www.php.net/fwrite"><span style="color: #990000;">fwrite</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'['</span><span style="color: #339933;">.</span><a href="http://www.php.net/date"><span style="color: #990000;">date</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Y/m/d - H:i:s&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'] '</span><span style="color: #339933;">.</span><span style="color: #000088;">$entrada</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #004000;">DEBUG</span><span style="color: #009900;">&#41;</span>
     <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$entrada</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
  <a href="http://www.php.net/fclose"><span style="color: #990000;">fclose</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>This logging class supports logs for 3 different types of systems; frontoffice (clients), backoffice (administrations) and <a title='Original Link: http://pt.wikipedia.org/wiki/Web_service'  href="http://www.joseairosa.com/?7hUrYec5">webservices</a>.</p>
<p>The way you call this function can be done in different ways, in this case, I&#8217;ll go over the Inheritance feature we talked before.</p>
<p>Imagine you have a class, lets say, foo</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p7code9'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p79"><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code" id="p7code9"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> foo <span style="color: #009900;">&#123;</span>
 <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> hello<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">// I bet you never seen this before!!! hehe ^_^</span>
  <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Hello World'</span><span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>What we can do is&#8230;</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p7code10'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p710"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code" id="p7code10"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//Logs.class.php is your Log class we just did</span>
<span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">'Logs.class.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> foo <span style="color: #000000; font-weight: bold;">extends</span> Logs <span style="color: #009900;">&#123;</span>
 <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> hello<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">// I bet you never seen this before!!! hehe ^_^</span>
  <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Hello World'</span><span style="color: #339933;">;</span>
  Logs<span style="color: #339933;">::</span><span style="color: #004000;">insereLog</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Mom, I think i just said Hello World. Log it please, thank you!&quot;</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>And voila, there you go <img src='http://www.joseairosa.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  your very own log system out of the box.</p>
<p>Feel free to report me any mistakes, suggestions or doubts you might have.</p>
<p>Have fun and <span style="font-style:italic;">be != be# {&#8216;run remotly&#8217; &#038;&#038; Get<All>['4_free'];}</span></p>
]]></content:encoded>
			<wfw:commentRss>http://www.joseairosa.com/2009/02/10/a-good-way-to-create-logs-in-php/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

