<?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>Magento Design and Development &#187; class</title>
	<atom:link href="http://inchoo.net/tag/class/feed/" rel="self" type="application/rss+xml" />
	<link>http://inchoo.net</link>
	<description>Magento Design and Magento Development Professionals - Inchoo</description>
	<lastBuildDate>Tue, 07 Sep 2010 06:36:42 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Making use of Magento getSingleton method</title>
		<link>http://inchoo.net/ecommerce/magento/making-use-of-magento-getsingleton-method/</link>
		<comments>http://inchoo.net/ecommerce/magento/making-use-of-magento-getsingleton-method/#comments</comments>
		<pubDate>Thu, 09 Oct 2008 21:26:04 +0000</pubDate>
		<dc:creator>Branko Ajzele</dc:creator>
				<category><![CDATA[Magento]]></category>
		<category><![CDATA[class]]></category>
		<category><![CDATA[method]]></category>
		<category><![CDATA[object]]></category>

		<guid isPermaLink="false">http://inchoo.net/?p=170</guid>
		<description><![CDATA[In one of my previous articles I showed you how to use getModel and getData methods in Magento. Although we should not put those to methods at the same level, since I&#8217;d say the getModel is a bit more higher. Higher in sense, you first use the geModel to create the instance of object then [...]]]></description>
			<content:encoded><![CDATA[<p>In one of my previous <a href="http://activecodeline.com/2008/10/05/getting-things-in-magento-by-getmodel-and-getdata-methods/">articles</a> I showed you how to use getModel and getData methods in Magento. Although we should not put those to methods at the same level, since I&#8217;d say the getModel is a bit more higher. Higher in sense, you first use the geModel to create the instance of object then you use getData to retrieve the data from that instance. I have said it before, and I&#8217;m saying it again; Magento is a great peace of full OOP power of PHP. It&#8217;s architecture is something not yet widely seen in CMS solutions.</p>
<p>One of the architectural goodies of Magento is it&#8217;s Singleton design pattern. In short, Singleton design pattern ensures a class has only one instance. Therefore one should provide a global point of access to that single instance of a class.<span id="more-170"></span></p>
<p>So why would you want to limit yourself to only one instance? Let&#8217;s say you have an application that utilizes database connection. Why would you create multiple instance of the same database connection object? What if you had references to an object in multiple places in your application? Wouldn&#8217;t you like to avoid the overhead of creating a new instance of that object for each reference? Then there is the case where you might want to pass the object&#8217;s state from one reference to another rather than always starting from an initial state.</p>
<p>Inside the Mage.php file of Magento system there is a <strong>getSingleton</strong> method (function if you prefer). Since it&#8217;s footprint is rather small, I&#8217;ll copy paste the code for you to see it.</p>
<p><strong>public static function getSingleton</strong>($modelClass=&#8221;, array $arguments=array())<br />
{<br />
$registryKey = &#8216;_singleton/&#8217;.$modelClass;<br />
if (!Mage::registry($registryKey)) {<br />
Mage::register($registryKey, Mage::getModel($modelClass, $arguments));<br />
}<br />
return Mage::registry($registryKey);<br />
}</p>
<p>First, notice the word static. In PHP and in other OOP languages keyword static stands for something like &#8220;this can be called on non objects, directly from class&#8221;. Now let me show you the footprint of the getModel method.</p>
<p><strong>public static function getModel</strong>($modelClass=&#8221;, $arguments=array())<br />
{<br />
return Mage::getConfig()-&gt;getModelInstance($modelClass, $arguments);<br />
}</p>
<p>Do you notice the <strong>parameters inside the brackets</strong>? They are the same for both of theme. Where am I getting with this? Well, I already showed you how to figure out the list of all of the available parameters in one of my previous <a href="http://activecodeline.com/2008/10/05/getting-things-in-magento-by-getmodel-and-getdata-methods/">articles</a> on my <a href="http://activecodeline.com">site</a>. So all we need to do at this point is, play with those parameters using <strong>getSingleton()</strong> method and observe the results.</p>
<p>Most important thing you need to remember is that using <strong>getSingleton</strong> you are calling already instantiated object. So if you get the empty array as a result, it means the object is empty. Only the blueprint is there, but nothing is loaded in it. We had the similar case using <strong>getModel(&#8216;<em>catalog/product</em>&#8216;)</strong> on some pages. If we done <strong>var_dump</strong> or <strong>print_r</strong> we could saw the return <strong>array</strong> was <strong>empty</strong>. Then we had to use the load method to load some data into our newly instantiated object.</p>
<p>What I&#8217;m trying to say, you need to play with it to get the feeling. Some models will return data rich objects some will return empty arrays. If we were to do <strong>Mage::getSingleton(&#8216;catalog/session)</strong> on <strong>view.phtml</strong> file we would retrieve an array with some useful data in it. Do not get confused with me mentioning the view.phmtl file, it&#8217;s just he file i like to use to test the code. Using <strong>Mage::getSingleton(&#8216;core/session)</strong> would retrieve us some more data. You get the picture. <strong>Test, test, test&#8230;</strong> What&#8217;s great about the whole thing is that naming in Magento is perfectly logical so most of the time you will probably find stuff you need in few minutes or so.</p>
<p>For the end here are some screenshots for you to see the results of calling getSingleton with <strong>catalog/product</strong> and <strong>catalog/session</strong> parametars.</p>
<p><a href="http://inchoo.net/wp-content/uploads/2008/10/getsingletoncatalogproduct.png" rel="lightbox[170]"><img class="alignnone size-thumbnail wp-image-172" title="getsingletoncatalogproduct" src="http://inchoo.net/wp-content/uploads/2008/10/getsingletoncatalogproduct-150x85.png" alt="" width="150" height="85" /></a> <a href="http://inchoo.net/wp-content/uploads/2008/10/getsingletoncatalogsession.png" rel="lightbox[170]"><img class="alignnone size-thumbnail wp-image-174" title="getsingletoncatalogsession" src="http://inchoo.net/wp-content/uploads/2008/10/getsingletoncatalogsession-150x85.png" alt="" width="150" height="85" /></a></p>
<p>Hope this will be useful for some of you.</p>
]]></content:encoded>
			<wfw:commentRss>http://inchoo.net/ecommerce/magento/making-use-of-magento-getsingleton-method/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>Figuring out Magento object context</title>
		<link>http://inchoo.net/tools/finding-out-magento-object-context/</link>
		<comments>http://inchoo.net/tools/finding-out-magento-object-context/#comments</comments>
		<pubDate>Wed, 10 Sep 2008 20:01:30 +0000</pubDate>
		<dc:creator>Branko Ajzele</dc:creator>
				<category><![CDATA[Magento]]></category>
		<category><![CDATA[Tools]]></category>
		<category><![CDATA[class]]></category>
		<category><![CDATA[method]]></category>
		<category><![CDATA[object]]></category>
		<category><![CDATA[view]]></category>

		<guid isPermaLink="false">http://inchoo.net/?p=94</guid>
		<description><![CDATA[One of the problems working under the hood of the Magento CMS is determining the context of $this. If you are about to do any advanced stuff with your template, besides layout changes, you need to get familiar with Magento&#8217;s objects (classes). Let&#8217;s have a look at the /app/design/frontend/default/default/template/catalog/product/view.phtml file. If you open this file [...]]]></description>
			<content:encoded><![CDATA[<p>One of the problems working under the hood of the Magento CMS is determining the context of <strong>$this</strong>. If you are about to do any advanced stuff with your template, besides layout changes, you need to get familiar with Magento&#8217;s objects (classes).</p>
<p>Let&#8217;s have a look at the <strong>/app/design/frontend/default/default/template/catalog/product/view.phtml</strong> file. If you open this file and execute <strong>var_dump($this)</strong> your browser will return empty page after a short period of delay. By page I mean on the product view page; the one you see when you click on Magetno product. Experienced users will open <strong>PHP error log</strong> and notice the error message caused by <strong>var_dump()</strong>. <span id="more-94"></span><strong>Error message</strong>:<br />
<em>PHP Fatal error:  Allowed memory size of 134217728 bytes exhausted (tried to allocate 121374721 bytes)</em></p>
<p>I like using <strong>print_r()</strong> and <strong>var_dump()</strong>, mostly because so far I had no positive experience using fancy debug or any other debugger with systems like Magento.</p>
<p><em><strong>Why is PHP throwing errors then</strong></em>? If you google out the error PHP has thrown at you, you&#8217;ll see that PHP 5.2 has memory limit. Follow the link <a title="PHP Memory Limit" href="http://www.php.net/manual/en/ini.core.php#ini.memory-limit" target="_blank">http://www.php.net/manual/en/ini.core.php#ini.memory-limit</a> to see how and what exactly.</p>
<p>Google search gave me some useful results trying to solve my problems. I found <strong>Krumo</strong> at <a title="Krumo" href="http://krumo.sourceforge.net/" target="_blank">http://krumo.sourceforge.net/</a>. It&#8217;s a PHP debugging tool, replacement for print_r() and var_dump(). After setting up Krumo and running it on Magento it gave me exactly what I wanted. It gave me the object type of the dumped file; in this case it gave me object type of $this.</p>
<p>If you&#8217;re using an IDE studio with code completion support like <strong>NuSphere PhpED</strong>, <strong>ZendStudio</strong> or <strong>NetBeans</strong> and you decide to do something like $this-&gt; you won&#8217;t get any methods listed. I haven&#8217;t yet seen the IDE that can perform this kind of smart logic and figure out the context of $this by it self.</p>
<p>What you can do is use the information obtained by krumo::dump($this). Performing <strong>krumo::dump($this)</strong> on <strong>/app/design/frontend/default/default/template/catalog/product/view.phtml</strong> file will return object type, <strong>Mage_Catalog_Block_Product_View</strong>.</p>
<p>Now if you do</p>
<p><em>Mage_Catalog_Block_Product_View::</em></p>
<p>your IDE supporting code completion will give you a drop down of all the available methods, let&#8217;s say canEmailToFriend();</p>
<p><em>Mage_Catalog_Block_Product_View::canEmailToFriend();</em></p>
<p>Now all you need to do is to replace  <strong><em>Mage_Catalog_Block_Product_View</em></strong> with <em><strong>$this</strong></em> like</p>
<p><em>$this-&gt;canEmailToFriend();</em></p>
<p>And you&#8217;re done. All of this may look like &#8220;why do I need this&#8221;. What you need it a smart IDE, one that can figure out the context of $this by it self and call the methods accordingly. No IDE currently does that, if I&#8217;m not missing on something. For now I see no better solution to retrieve the object context of $this across all those Magento files.</p>
<p>If you need some help setting up Krumo with Magento you can read my other, somewhat detailed <a title="ActiveCodeline debugging magento using krumo" href="http://activecodeline.com/2008/09/10/debugging-magento-using-krumo/" target="_blank">article</a> on <a title="ActiveCodeline" href="http://activecodeline.com" target="_blank">activecodeline.com</a>.</p>
<p>Hope this was useful for you.</p>
]]></content:encoded>
			<wfw:commentRss>http://inchoo.net/tools/finding-out-magento-object-context/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
