<?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>sammela.com &#187; opencart</title>
	<atom:link href="http://sammela.com/category/opencart/feed/" rel="self" type="application/rss+xml" />
	<link>http://sammela.com</link>
	<description>Stunning web design articles</description>
	<lastBuildDate>Tue, 17 Nov 2009 04:43:11 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>The OpenCart Controller base class</title>
		<link>http://sammela.com/2009/11/15/the-opencart-controller-base-class/</link>
		<comments>http://sammela.com/2009/11/15/the-opencart-controller-base-class/#comments</comments>
		<pubDate>Sun, 15 Nov 2009 05:00:30 +0000</pubDate>
		<dc:creator>Sam Mela</dc:creator>
				<category><![CDATA[article]]></category>
		<category><![CDATA[news]]></category>
		<category><![CDATA[opencart]]></category>

		<guid isPermaLink="false">http://sammela.com/?p=496</guid>
		<description><![CDATA[All controllers in OpenCart share a common ancestor, the Controller class, located in the  DIR_APPLICATION/system/engine/controller.php file.  Note that DIR_APPLICATION is the path to your application directory, and is defined in the OpenCart config.php file.
In object oriented terms, the Controller class is called a base class.  It contains common methods and data definitions [...]]]></description>
			<content:encoded><![CDATA[<p>All controllers in OpenCart share a common ancestor, the <strong>Controller</strong> class, located in the  DIR_APPLICATION/system/engine/controller.php file.  Note that DIR_APPLICATION is the path to your application directory, and is defined in the OpenCart config.php file.</p>
<p>In object oriented terms, the <strong>Controller</strong> class is called a base class.  It contains common methods and data definitions that all its descendant classes will find useful and can use.  </p>
<p>Data definitions for the <strong>Controller</strong> class follow:</p>
<table cellspacing="0" cellpadding="0" border="0">
<tr>
<td style="padding-right:10px; padding-bottom:10px; font-weight:bold;" align="left" valign="top">$id;</td>
<td style="padding-bottom:10px;" align="left" valign="top"><strong>$id</strong> is used to identify the controller.  Typically it is set by the controller. For example, the ControllerCommonColumnLeft controller sets its <strong>$id</strong> to &#8220;column_left&#8221;. Controller id&#8217;s are the means by which a parent controller relates child controller output to a template. The name used in the template must match the controller name.</td>
</tr>
<tr>
<td style="padding-right:10px; font-weight:bold;" align="left" valign="top">$template</td>
<td style="padding-bottom:10px;" align="left" valign="top"><strong>$template</strong> is the file that defines the output of the controller. It is basically a PHP/HTML file that is populated with variables from the controller <strong>$data</strong> array.</td>
</tr>
<tr>
<td style="padding-right:10px; font-weight:bold;" align="left" valign="top">$children</td>
<td style="padding-bottom:10px;" align="left" valign="top"><strong>$children</strong> is an array of the controllers that a top level controller is composed of. It is very important to understand that these are not children in the object oriented inheritance sense of the term. They are just pieces of the top level controller, the way tires and the engine are pieces of a car. For example, <strong>ControllerCommonHome</strong> contains the following childres:</p>
<ul>
<li> ControllerCommonHeader</li>
<li>ControllerCommonFooter</li>
<li>ControllerCommonLeft</li>
<li>ControllerCommonRight</li>
</ul>
</td>
</tr>
<tr>
<td style="padding-right:10px; font-weight:bold;" align="left" valign="top">$data</td>
<td style="padding-bottom:10px;" align="left" valign="top">All information that will be used to populate the <strong>$template</strong> is stored in the <strong>$data</strong> array. In the controller <strong>fetch</strong> method, the PHP <strong>extract</strong> function is used to create local variables from the  <strong>$data</strong> array and make them available to populate the <strong>$template</strong>.</td>
</tr>
<tr>
<td style="padding-right:10px; font-weight:bold;" align="left" valign="top">$output</td>
<td style="padding-bottom:10px;" align="left" valign="top">The HTML output of the  <strong>$template</strong> is ultimately stored in the <strong>$output</strong> variable, where it is available to be echoed to the client browser.</td>
</tr>
</table>
<p>In addition to the above class data, the base<strong>Controller</strong> class makes a few methods available.  </p>
<table cellspacing="0" cellpadding="0" border="0">
<tr>
<td style="padding-right:10px; padding-bottom:10px; font-weight:bold;" align="left" valign="top">__get</td>
<td style="padding-bottom:10px;" align="left" valign="top"><strong>__get</strong> is a magic method that gets a class scope variable from the <strong>Registry</strong> class. See the online PHP documentation on magic methods for more detail. Also see  <a href="2009/11/13/the-open-cart-load-class-variable/" target="_blank">The Open Cart “load” Class Variable and Magic Methods</a>.</td>
</tr>
<tr>
<td style="padding-right:10px; padding-bottom:10px; font-weight:bold;" align="left" valign="top">__set</td>
<td style="padding-bottom:10px;" align="left" valign="top"><strong>__set</strong> is a magic method that puts a class scope variable into the <strong>Registry</strong> class. See the online PHP documentation on magic methods for more detail. Also see <a href="2009/11/13/the-open-cart-load-class-variable/" target="_blank">The Open Cart “load” Class Variable and Magic Methods</a>.</td>
</tr>
<tr>
<td style="padding-right:10px; font-weight:bold;" align="left" valign="top">forward</td>
<td style="padding-bottom:10px;" align="left" valign="top">The <strong>forward</strong> method creates a new instance of the Router class.</td>
</tr>
<tr>
<td style="padding-right:10px; font-weight:bold;" align="left" valign="top">redirect</td>
<td style="padding-bottom:10px;" align="left" valign="top">The <strong>redirect</strong> method immediately redirects to a new location.</td>
</tr>
<tr>
<td style="padding-right:10px; font-weight:bold;" align="left" valign="top">fetch</td>
<td style="padding-bottom:10px;" align="left" valign="top">See <a href="2009/11/14/how-opencart-renders-pages/" target="_blank">How OpenCart Renders Pages</a>.</td>
</tr>
<tr>
<td style="padding-right:10px; font-weight:bold;" align="left" valign="top">render</td>
<td style="padding-bottom:10px;" align="left" valign="top">See <a href="2009/11/14/how-opencart-renders-pages/" target="_blank">How OpenCart Renders Pages</a>.</td>
</tr>
</table>
<p>© 2009, Sam Mela. All rights reserved.</p>
]]></content:encoded>
			<wfw:commentRss>http://sammela.com/2009/11/15/the-opencart-controller-base-class/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>OpenCart Top Level Controller Architecture</title>
		<link>http://sammela.com/2009/11/14/opencart-top-level-controller-architecture/</link>
		<comments>http://sammela.com/2009/11/14/opencart-top-level-controller-architecture/#comments</comments>
		<pubDate>Sat, 14 Nov 2009 22:54:52 +0000</pubDate>
		<dc:creator>Sam Mela</dc:creator>
				<category><![CDATA[article]]></category>
		<category><![CDATA[news]]></category>
		<category><![CDATA[opencart]]></category>

		<guid isPermaLink="false">http://sammela.com/?p=478</guid>
		<description><![CDATA[An OpenCart top level controller is responsible for orchestrating the complete construction of an OpenCart page.
As an example, the ControllerCheckoutCart controller class is responsible for coordinating the model data and the template view necessary to create a checkout cart page for the client.
Another example is the ControllerCommonHome controller class, which is responsible for the main [...]]]></description>
			<content:encoded><![CDATA[<p>An OpenCart top level controller is responsible for orchestrating the complete construction of an OpenCart page.</p>
<p>As an example, the <strong>ControllerCheckoutCart</strong> controller class is responsible for coordinating the model data and the template view necessary to create a checkout cart page for the client.</p>
<p>Another example is the <strong>ControllerCommonHome</strong> controller class, which is responsible for the main store view that customers see when they first visit.</p>
<h3>Common Base Class</h3>
<p>All controllers in OpenCart share a common ancestor, the <strong>Controller</strong> class, located in the  DIR_APPLICATION/system/engine/controller.php file.  This class is described in <a href="http://sammela.com/2009/11/15/the-opencart-controller-base-class/">The OpenCart Controller Base Class</a></p>
<h3>The Index Method</h3>
<p>© 2009, Sam Mela. All rights reserved.</p>
]]></content:encoded>
			<wfw:commentRss>http://sammela.com/2009/11/14/opencart-top-level-controller-architecture/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How OpenCart Renders Pages</title>
		<link>http://sammela.com/2009/11/14/how-opencart-renders-pages/</link>
		<comments>http://sammela.com/2009/11/14/how-opencart-renders-pages/#comments</comments>
		<pubDate>Sat, 14 Nov 2009 06:01:07 +0000</pubDate>
		<dc:creator>Sam Mela</dc:creator>
				<category><![CDATA[article]]></category>
		<category><![CDATA[news]]></category>
		<category><![CDATA[opencart]]></category>

		<guid isPermaLink="false">http://sammela.com/?p=458</guid>
		<description><![CDATA[The OpenCart Controller class contains a render method that is used to produce HTML pages output by all OpenCart classes that inherit from Controller class.
Here is how it works.
Each time a page is rendered, it is done with one top level controller.  The top level controller may contain zero or more child controllers and [...]]]></description>
			<content:encoded><![CDATA[<p>The OpenCart <strong>Controller</strong> class contains a <strong>render</strong> method that is used to produce HTML pages output by all OpenCart classes that inherit from <strong>Controller</strong> class.</p>
<p>Here is how it works.</p>
<p>Each time a page is rendered, it is done with one top level controller.  The top level controller may contain zero or more child controllers and it contains a template.  Rendering proceed in the following order:</p>
<ol>
<li>Each child controller is rendered</li>
<li>The top level controller is rendered, based on its own data and the output of the child controllers</li>
</ol>
<p>That&#8217;s it.  The rest is details which are described in the remainder of this article.</p>
<h3>How the Template is Rendered</h3>
<p>As stated previously, the top level controller <strong>render</strong> method produces output using a template.  To accomplish this, it passes the template to the <strong>fetch</strong> method, which does the actual work.</p>
<h3>The fetch Method</h3>
<p>As stated previously, the top level controller <strong>render</strong> method produces output using a template.  To accomplish this, it passes the template to the <strong>fetch</strong> method, which does the actual work.</p>
<p>The <strong>fetch</strong> method is not very long, but it does some clever things and it takes advantage of some very powerful features of PHP.  Here is the operative code:</p>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;">// Create local variables from the controller data array</span><br />
<a href="http://www.php.net/extract"><span style="color: #990000;">extract</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #666666; font-style: italic;">// Initialize PHP output buffering</span><br />
<a href="http://www.php.net/ob_start"><span style="color: #990000;">ob_start</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp;<br />
<br />
<span style="color: #666666; font-style: italic;">// Load the template file, which is automatically </span><br />
<span style="color: #666666; font-style: italic;">// populated with the local variables created above &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><br />
<span style="color: #b1b100;">require</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #666666; font-style: italic;">// Get the output of the template file</span><br />
<span style="color: #666666; font-style: italic;">// Which has been echoed to the PHP output buffer</span><br />
<span style="color: #000088;">$content</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/ob_get_contents"><span style="color: #990000;">ob_get_contents</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #666666; font-style: italic;">// Empty the PHP output buffer</span><br />
<a href="http://www.php.net/ob_end_clean"><span style="color: #990000;">ob_end_clean</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>Basically, all information necessary to populate the template must be stored in the controller&#8217;s $data array.  The native PHP extract function pulls the information out the the $data array and populates the local symbol table with it, so it is available to the template file.</p>
<p>Now here is the trick.  The template file echoes its output; but since PHP buffering is turned on, the echoed HTML is not output to the client browser, but rather to the PHP output buffer.  The <strong>fetch</strong> method stores the PHP output buffer contents in the <strong>$content</strong> variable and then returns it to the calling function.  Cool beans.</p>
<p>But there&#8217;s more.  Not only is this method used for the top level controller, but each of the controllers contained by the top level controller (the children) is evaluated the same way; and the children can contain their own children, theoretically without limit, although in practical terms it is unlikely they will go more than two or three levels deep.</p>
<p>© 2009, Sam Mela. All rights reserved.</p>
]]></content:encoded>
			<wfw:commentRss>http://sammela.com/2009/11/14/how-opencart-renders-pages/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The OpenCart Router Class</title>
		<link>http://sammela.com/2009/11/13/the-opencart-router-class/</link>
		<comments>http://sammela.com/2009/11/13/the-opencart-router-class/#comments</comments>
		<pubDate>Fri, 13 Nov 2009 22:04:26 +0000</pubDate>
		<dc:creator>Sam Mela</dc:creator>
				<category><![CDATA[news]]></category>
		<category><![CDATA[opencart]]></category>

		<guid isPermaLink="false">http://sammela.com/?p=443</guid>
		<description><![CDATA[The OpenCart Router class takes a route string and converts it to an internal representation of a class  and a method, typically for invoking a controller.
The Router class is specialized to the directory structure of OpenCart and the naming conventions for OpenCart controller files and class; therefore, before discussing the Router class in detail, it [...]]]></description>
			<content:encoded><![CDATA[<p>The OpenCart <strong>Router</strong> class takes a <strong>route</strong> string and converts it to an internal representation of a class  and a method, typically for invoking a controller.</p>
<p>The <strong>Router</strong> class is specialized to the directory structure of OpenCart and the naming conventions for OpenCart controller files and class; therefore, before discussing the <strong>Router</strong> class in detail, it is important to understand the directory structure and naming conventions that OpenCart uses for controllers, controller directories, and controller files.</p>
<h3>Controller Directory Structure</h3>
<p>In OpenCart, catalog controllers are all located under DIR_APPLICATION/catalog/controller, where DIR_APPLICATION is the path to  your application directory, and is defined in the OpenCart config.php file.</p>
<p>More specifically, files containing definitions of controller classes are contained in the following directories:</p>
<ul>
<li>DIR_APPLICATION/catalog/controller/account</li>
<li>DIR_APPLICATION/catalog/controller/checkout</li>
<li>DIR_APPLICATION/catalog/controller/common</li>
<li>DIR_APPLICATION/catalog/controller/error</li>
<li>DIR_APPLICATION/catalog/controller/feed</li>
<li>DIR_APPLICATION/catalog/controller/information</li>
<li>DIR_APPLICATION/catalog/controller/module</li>
<li>DIR_APPLICATION/catalog/controller/payment</li>
<li>DIR_APPLICATION/catalog/controller/product</li>
</ul>
<p>As an example, the following files are contained in DIR_APPLICATION/catalog/controller/account:</p>
<ul>
<li>DIR_APPLICATION/catalog/controller/checkout/address.php</li>
<li>DIR_APPLICATION/catalog/controller/checkout/cart.php</li>
<li>DIR_APPLICATION/catalog/controller/checkout/confirm.php</li>
<li>DIR_APPLICATION/catalog/controller/checkout/guest.php</li>
<li>DIR_APPLICATION/catalog/controller/checkout/payment.php</li>
<li>DIR_APPLICATION/catalog/controller/checkout/shipping.php</li>
<li>DIR_APPLICATION/catalog/controller/checkout/success.php</li>
</ul>
<h3>Controller Class Naming Conventions</h3>
<p>We see from the previous section how the controllers directories are laid out, but how are the controller classes named?</p>
<p>It is very straightforward.  Each controller class name is built from the last two directory names and the file name of its path name.</p>
<p>For example, the file <strong>DIR_APPLICATION/catalog/controller/checkout/address.php</strong> contains the class <strong>ControllerCheckoutAddress</strong>.</p>
<p>Similarly, the file <strong>DIR_APPLICATION/catalog/controller/account/address.php</strong> contains the class <strong>ControllerAccountAddress</strong>.</p>
<h2>How the Router class works</h2>
<p>Now that we understand the naming conventions for controller classes and files, let&#8217;s look at how the <strong>Router</strong> class works.</p>
<p>The <strong>Router</strong> constructor accepts a required string parameter, route, and an optional array parameter, args.</p>
<p>The constructor creates three class scope variables, <strong>$class</strong>, <strong>$method</strong>, <strong>$args</strong>; which are made available through the following class methods:</p>
<ul>
<li>getClass</li>
<li>getMethod</li>
<li>getArgs</li>
</ul>
<p>Here is what the Router does.   If takes the route string</p>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000088;">$router</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Router<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;account/address/somemethod&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>The <strong>class</strong>variable will be set to &#8220;account/address&#8221; and the <strong>method</strong> variable will be set to &#8220;somemethod&#8221;.</p>
<p>It may seem odd that the class maintains the slash (&#8221;/&#8221;), but that&#8217;s how it works, and later when the class name is actually used to instantiate an object, the slashes are removed first.</p>
<p>This may seem like much ado about nothing, but that&#8217;s how it works.  Later, after the $route variable is created in the Registry, it can be fished out and used instantiate the correct controller; in this case, the <strong>ControllerAccountAddress</strong> class  located in the DIR_APPLICATION/catalog/controller/account/address.php file.</p>
<p>© 2009, Sam Mela. All rights reserved.</p>
]]></content:encoded>
			<wfw:commentRss>http://sammela.com/2009/11/13/the-opencart-router-class/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>The OpenCart Response Class</title>
		<link>http://sammela.com/2009/11/13/the-opencart-response-class/</link>
		<comments>http://sammela.com/2009/11/13/the-opencart-response-class/#comments</comments>
		<pubDate>Fri, 13 Nov 2009 06:19:23 +0000</pubDate>
		<dc:creator>Sam Mela</dc:creator>
				<category><![CDATA[news]]></category>
		<category><![CDATA[opencart]]></category>

		<guid isPermaLink="false">http://sammela.com/?p=428</guid>
		<description><![CDATA[The OpenCart Response class has a simple job. It outputs a page of HTML to a client browser.
But it does a few other things related to that function as well, including the following:

It compresses output data in accordance with how the OpenCart installation is configured and the client browser capabilitiles.
It maintains an array of header [...]]]></description>
			<content:encoded><![CDATA[<p>The OpenCart <strong>Response</strong> class has a simple job. It outputs a page of HTML to a client browser.</p>
<p>But it does a few other things related to that function as well, including the following:</p>
<ul>
<li>It compresses output data in accordance with how the OpenCart installation is configured and the client browser capabilitiles.</li>
<li>It maintains an array of header data and sends the header data, prior to outputting the page HTML</li>
</ul>
<p><!--p final class Response { 	private $headers = array();  	private $output; 	private $level = 0; 	 	public function addHeader($key, $value) { 		$thi-->Here is a complete list of the public methods of the <strong>Response</strong> class:</p>
<ul>
<li><strong>addHeader($key, $value)</strong></li>
<p>Adds a header to an internal keyed list of headers. When the output is constructed the  headers are all transmitted first.</p>
<li><strong> removeHeader($key) </strong></li>
<p>Since the internal header list is keyed, a particular header can be removed using this method.</p>
<li><strong>redirect($url) </strong></li>
<p>Immediately redirects to $url.  For debugging and maintenance purposes, it is much better to redirect using this method, rather than sprinkling redirects throughout code.</p>
<li><strong>setOutput($output, $level = 0) </strong></li>
<p>Sets the HTML to be output by the Response class.  If the optional $level parameter is not false (i.e. non-zero and not set to FALSE), the Response class will attempt to compress the output sent to the browser.</p>
<li><strong>output()</strong></li>
<p>Send headers if not already sent. Compress the output if $level is true. Echo the output.</ul>
<h3>Try It</h3>
<p>It is simple to test the <strong>Response</strong>class, as shown below:</p>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000088;">$rsp</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> response<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000088;">$rsp</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>setOutput<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;This is a test of the Response class with compression enabled&quot;</span><span style="color: #339933;">,</span>1<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000088;">$rsp</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>output<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<h3>Note On Compression</h3>
<p>I had not done any PHP output compression before I looked at the OpenCart <strong>Response</strong> class.  The <strong>compress</strong> method accomplishes this by first determining the ability of the browser to uncompress.</p>
<p>The value of $_SERVER['HTTP_ACCEPT_ENCODING'] will be set to &#8216;gzip&#8217; if the browser is capable of accepting gzip encoding.</p>
<p>The value of $_SERVER['HTTP_ACCEPT_ENCODING'] will be set to &#8216;x-gzip&#8217; if the browser is capable of accepting x-gzip encoding.</p>
<p>If the browser is not capable of accepting either type of compression, the <strong>compress</strong> method simply returns uncompressed data.</p>
<p>If the browser is capable of accepting either type of compression, the <strong>compress</strong> method then checks to see if the server has the proper library installed to do the compression and further it checks that no headers have already been set.  If both these conditions are true, the <strong>compress</strong> method compresses the output data and returns it.</p>
]]></content:encoded>
			<wfw:commentRss>http://sammela.com/2009/11/13/the-opencart-response-class/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The Open Cart &#8220;load&#8221; Class Variable and Magic Methods</title>
		<link>http://sammela.com/2009/11/13/the-open-cart-load-class-variable/</link>
		<comments>http://sammela.com/2009/11/13/the-open-cart-load-class-variable/#comments</comments>
		<pubDate>Fri, 13 Nov 2009 04:54:52 +0000</pubDate>
		<dc:creator>Sam Mela</dc:creator>
				<category><![CDATA[news]]></category>
		<category><![CDATA[opencart]]></category>

		<guid isPermaLink="false">http://sammela.com/?p=416</guid>
		<description><![CDATA[All controllers in the OpenCart ecommerce package inherit from the Controller class.
The load variable in those controllers was a bit of a mystery to me, because I could not figure out where it was assigned.  Then I realized it was being accessed through the Registry class using a PHP language construct called a &#8220;magic [...]]]></description>
			<content:encoded><![CDATA[<p>All controllers in the OpenCart ecommerce package inherit from the <strong>Controller</strong> class.</p>
<p>The <strong>load</strong> variable in those controllers was a bit of a mystery to me, because I could not figure out where it was assigned.  Then I realized it was being accessed through the <strong>Registry</strong> class using a PHP language construct called a &#8220;magic method&#8221;, which is documented in the online PHP Manual. </p>
<p>In particular the <strong>__get</strong> magic method is used.  This method is quite interesting.  It is called whenever a class method attempts to retrieve an undefined attribute.</p>
<p>In a nutshell, since the <strong>load</strong> variable is not defined in the <strong>Controller</strong> class or any of its child classes, the <strong>__get</strong> method will be called.</p>
<p>Let&#8217;s see exactly how this works.  Below is the <strong>__get</strong> method as it is defined in the <strong>Controller</strong> class:</p>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __get<span style="color: #009900;">&#40;</span><span style="color: #000088;">$key</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #b1b100;">return</span> Registry<span style="color: #339933;">::</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$key</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>Here is the code that attempts to access the <strong>load</strong> variable:</p>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">load</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">model</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'catalog/category'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>When PHP encounters the above code, to get the <strong>$this->load</strong> variable, it calls <strong>__get</strong> with the string &#8220;load&#8221;, and the implementation of <strong>__get</strong> looks it up in the Registry class, where it has been previously stored.</p>
<p>Here&#8217;s the final piece of the puzzle.  You may be wondering how the value of the <strong>Registry</strong> variable &#8220;load&#8221; is set.</p>
<p>Simple &#8212; at initialization, in the index.php file,  a loader object is created and stored in the registry, as follows:</p>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000088;">$loader</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Loader<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
Registry<span style="color: #339933;">::</span><span style="color: #004000;">set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'load'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$loader</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>© 2009, Sam Mela. All rights reserved.</p>
]]></content:encoded>
			<wfw:commentRss>http://sammela.com/2009/11/13/the-open-cart-load-class-variable/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
