<?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>Juozas devBlog &#187; reflection</title>
	<atom:link href="http://dev.juokaz.com/tag/reflection/feed" rel="self" type="application/rss+xml" />
	<link>http://dev.juokaz.com</link>
	<description>Random ideas, scripts and facts</description>
	<lastBuildDate>Mon, 22 Mar 2010 10:48:42 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Dynamic Assemblies loading using Reflection</title>
		<link>http://dev.juokaz.com/winphp-2009/dynamic-assemblies-loading-using-reflection</link>
		<comments>http://dev.juokaz.com/winphp-2009/dynamic-assemblies-loading-using-reflection#comments</comments>
		<pubDate>Fri, 24 Apr 2009 14:04:28 +0000</pubDate>
		<dc:creator>Juozas</dc:creator>
				<category><![CDATA[WinPhp 2009]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[assembly]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[competition]]></category>
		<category><![CDATA[dll]]></category>
		<category><![CDATA[gac]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[reflection]]></category>
		<category><![CDATA[system.reflection]]></category>
		<category><![CDATA[winphp]]></category>

		<guid isPermaLink="false">http://dev.juokaz.com/?p=544</guid>
		<description><![CDATA[Tuesday I&#8217;ve posted post how to use .Net assemblies in PHP, however, as Teal&#8217;c correctly pointed out:
Making your library COM visible requires that you register the DLL in the GAC and registering something in the GAC should only be done when multiple applications need to access the same version of the same library. Even then, [...]]]></description>
			<content:encoded><![CDATA[<p>Tuesday I&#8217;ve posted post how to use <a href="http://dev.juokaz.com/winphp-2009/using-php-with-c-written-libraries">.Net assemblies in PHP</a>, however, as Teal&#8217;c correctly pointed out:</p>
<blockquote><p><img src="http://dev.juokaz.com/wp-content/uploads/2009/04/picture.png" alt=".NET" title=".NET" width="48" height="48" class="alignnone size-full wp-image-566" style="float: right;" />Making your library COM visible requires that you register the DLL in the GAC and registering something in the GAC should only be done when multiple applications need to access the same version of the same library. Even then, most software houses will deploy each app with it’s own copy of the DLL instead of using the GAC.</p></blockquote>
<p>Nevertheless, C# (and most of other languages) support <a href="http://en.wikipedia.org/wiki/Reflection_(computer_science)">Reflection</a>. Basically using reflection, programs can &#8220;observe and modify its own structure and behavior&#8221; (wiki). .NET has <a href="http://msdn.microsoft.com/en-us/library/system.reflection.aspx">System.Reflection</a> namespace with all needed methods. Purpose of this post is to find a way how to load DLL&#8217;s dynamically &#8211; without worrying about registering, re-registering, unregistering them in running system/production servers. </p>
<p>DLL&#8217;s are library assemblies, which &#8220;contain code in CIL, which is usually generated from .NET languages, and then compiled into machine language at runtime by the CLR just-in-time compiler.&#8221; (<a href="http://en.wikipedia.org/wiki/.NET_assembly">from</a>). For all not familiar with assemblies, assembly &#8220;is a reusable, versionable, and self-describing building block of a common language runtime application.&#8221; (<a href="http://msdn.microsoft.com/en-us/library/system.reflection.assembly.aspx">from</a>). Consider this code:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Reflection</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF;">namespace</span> Php
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> Reflection
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">Object</span> get<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> assembly, <span style="color: #FF0000;">string</span> className<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            Assembly asm <span style="color: #008000;">=</span> Assembly.<span style="color: #0000FF;">LoadFrom</span><span style="color: #000000;">&#40;</span>assembly<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #0600FF;">return</span> asm.<span style="color: #0000FF;">CreateInstance</span><span style="color: #000000;">&#40;</span>className, <span style="color: #0600FF;">true</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>This is very basic class, which can be used to do such things as:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">Php.<span style="color: #0000FF;">Reflection</span> obj <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Php.<span style="color: #0000FF;">Reflection</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #FF0000;">Object</span> my <span style="color: #008000;">=</span> obj.<span style="color: #0000FF;">get</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;my.dll&quot;</span>, <span style="color: #666666;">&quot;namespace.class&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>You may have thought that this class is absolutely useless, because you can simply use <a href="http://msdn.microsoft.com/en-us/library/system.reflection.assembly.aspx"><em>System.Reflection.Assembly</em></a> without wrapping it into separate class. However, I was writing this code with an intention to use it as <a href="http://uk2.php.net/manual/en/class.com.php">COM</a> object in PHP and because PHP can&#8217;t access static COM class methods (which I find very disappointing) I needed to create this class. Now in PHP you can write:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$obj</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Com <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Php.Reflection&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$math</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$obj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;math_class.dll&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;phpclass3.first&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><a href="http://dev.juokaz.com/wp-content/uploads/2009/04/dotnet_global_assembly_cache.gif"><img src="http://dev.juokaz.com/wp-content/uploads/2009/04/dotnet_global_assembly_cache-150x150.gif" alt="dotnet_global_assembly_cache" title="dotnet_global_assembly_cache" width="100" height="100" style="float: right; margin-left: 5px;" class="alignnone size-thumbnail wp-image-572" /></a>This method, in my opinion, is much much more better than registering every single class with <a href="http://en.wikipedia.org/wiki/Global_Assembly_Cache">global assembly cache</a>. It&#8217;s more practical and maintainable to have all required libraries in the same project directory, and not somewhere in Windows core. However, benchmarks are still coming so stay <a href="http://dev.juokaz.com/feed">tuned</a>.</p>
<p>* <em> For everyone interested in Winphp contest, and people building PHP apps in Windows, I recommend reading <a href="http://www.eurowinphp.com/">www.eurowinphp.com</a> blog, which aggregates all participants blogs. For example, Timmy Kokke, another WinPHP participant, recently wrote another great tutorial <a href="http://geekswithblogs.net/tkokke/archive/2009/04/24/how-to-use-.net-assemblies-in-php.aspx">how to make your .NET classes accessible in PHP</a>. Also, you can follow my on Twitter at <a href="http://twitter.com/juokaz">twitter.com/juokaz</a>.</em></p>
]]></content:encoded>
			<wfw:commentRss>http://dev.juokaz.com/winphp-2009/dynamic-assemblies-loading-using-reflection/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Zend Optimizer and PHP comments</title>
		<link>http://dev.juokaz.com/php/zend-optimizer-and-php-comments</link>
		<comments>http://dev.juokaz.com/php/zend-optimizer-and-php-comments#comments</comments>
		<pubDate>Wed, 04 Feb 2009 17:50:17 +0000</pubDate>
		<dc:creator>Juozas</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[comments]]></category>
		<category><![CDATA[framework]]></category>
		<category><![CDATA[optimizer]]></category>
		<category><![CDATA[production]]></category>
		<category><![CDATA[reflection]]></category>
		<category><![CDATA[server]]></category>
		<category><![CDATA[xmlrpc]]></category>
		<category><![CDATA[zend]]></category>
		<category><![CDATA[zend framework]]></category>

		<guid isPermaLink="false">http://dev.juokaz.com/?p=3</guid>
		<description><![CDATA[Zend Optimizer is nice product, but I can&#8217;t make it work with PHP&#8217;s Reflection. Reflection is very good functionality which allows script to read and extract information from it-self&#8217;s source code. For example, Zend_XmlRpc_Server component checks functions signatures (which are phpdoc comments) to determine if given parameter(s) is valid type, etc.
I have been using Zend [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.zend.com/en/products/guard/optimizer/">Zend Optimizer</a> is nice product, but I can&#8217;t make it work with PHP&#8217;s <a href="http://uk3.php.net/oop5.reflection">Reflection</a>. Reflection is very good functionality which allows script to read and extract information from it-self&#8217;s source code. For example, <a href="http://framework.zend.com/manual/en/zend.xmlrpc.server.html">Zend_XmlRpc_Server</a> component checks functions signatures (which are <a href="http://en.wikipedia.org/wiki/PHPDoc">phpdoc</a> comments) to determine if given parameter(s) is valid type, etc.</p>
<p>I have been using Zend Framework for some time now, and found that only possible way to use XmlRpc Server package is to delete all functions signatures checking. My production server has Zend Optimizer installed and it just breaks Reflection functionality.</p>
<p>For tests I created simple reflection example:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 *  My Class
 */</span>
<span style="color: #000000; font-weight: bold;">class</span> test <span style="color: #009900;">&#123;</span> <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$oClassReflect</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ReflectionClass<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Test&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">print</span> <span style="color: #000088;">$oClassReflect</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getDocComment</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>First time script outputs:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">/**
 *  My Class
 */</pre></div></div>

<p>Next times there is <strong>no output</strong> at all &#8211; all comments are gone (in Optimizer&#8217;s cache). I still can get methods of class (source code is not gone), but I cannot use comments to mark params/return types and later use them to validate I/O.</p>
<p>I don&#8217;t know yet if it&#8217;s a problem with my production server, but Zend manuals are not very clear and I can&#8217;t find any information about comments stripping issue. Has anyone had similar problems?</p>
]]></content:encoded>
			<wfw:commentRss>http://dev.juokaz.com/php/zend-optimizer-and-php-comments/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
