{"id":209,"date":"2010-02-12T01:39:05","date_gmt":"2010-02-12T01:39:05","guid":{"rendered":"http:\/\/www.muratyaman.co.uk\/wp\/?p=209"},"modified":"2020-04-01T11:58:34","modified_gmt":"2020-04-01T10:58:34","slug":"class-library-as-dll-for-business-model-to-be-used-in-php","status":"publish","type":"post","link":"https:\/\/www.muratyaman.co.uk\/blog\/index.php\/2010\/02\/class-library-as-dll-for-business-model-to-be-used-in-php\/","title":{"rendered":"Class Library as DLL for Business Model to be used in PHP"},"content":{"rendered":"<p><strong>Just an idea, not a new one, and I promise, it is not another &#8220;framework&#8221;:<\/strong><\/p>\n<p>Is it worth to create a class library as a DLL for a business model and use that DLL in PHP?<br \/>\nWhat are the advantages and disadvantages?<\/p>\n<p>As soon as we say DLL, it is a solution that can be used only in Windows \ud83d\ude41<\/p>\n<p>First, let us discuss what we mean about this class library.<br \/>\n1. For proof of concept, our classes will be very primitive. Here is a sample C# namespace:<\/p>\n<pre lang=\"csharp\">\r\nusing System;\r\n\r\nnamespace myClassLib\r\n{\r\n\r\n\tpublic class Class1\r\n\t{\r\n\t\tpublic string s1;\r\n\t\t\r\n\t\tpublic Class1(){\r\n\t\t\ts1 = \"default s1\";\r\n\t\t}\r\n\t}\r\n\t\r\n\tpublic class Class2\r\n\t{\r\n\t\tpublic string s2;\r\n\t\tpublic Class1 c1;\r\n\t\t\r\n\t\tpublic Class2(){\r\n\t\t\ts2 = \"default s2\";\r\n\t\t\tc1 = new Class1();\r\n\t\t}\r\n\t\t\r\n\t\tpublic bool changeData(){\r\n\t\t\ts2 = \"who did it?\";\r\n\t\t\tc1.s1 = \"s1 did it\";\r\n\t\t\treturn true;\r\n\t\t}\r\n\t}\t\r\n}\r\n<\/pre>\n<p>There is nothing fancy about it; only 2 related classes with string properties.<\/p>\n<p>2. Compile the class library and copy the <strong>myClassLib.dll<\/strong> into the Apache binary folder, next to the <strong>httpd.exe<\/strong>, start Apache.<\/p>\n<p>3. Write a simple PHP code, load the library (<a href=\"http:\/\/php.net\/manual\/en\/class.dotnet.php\">DOTNET<\/a>) and refer to the classes in it:<\/p>\n<pre lang=\"php\">\r\n<?php\r\n$obj2 = new DOTNET(\"myClassLib,Version=1.0.3695.2377,Culture=neutral\", \"myClassLib.Class2\");\r\n$obj1 = $obj2->c1;\r\necho 'obj2->s2 = ' . $obj2->s2 . '<br>';\r\necho 'obj2->c1->s1 = ' . $obj2->c1->s1 . '<br>';\r\necho 'obj1->s1 = ' . $obj1->s1 . '<br>';\r\n$obj2->changeData();\r\necho 'obj2->s2 = ' . $obj2->s2 . '<br>';\r\necho 'obj2->c1->s1 = ' . $obj2->c1->s1 . '<br>';\r\necho 'obj1->s1 = ' . $obj1->s1 . '<br>';\r\n$obj1->s1 = \"No, I didn't!\";\r\necho 'obj2->s2 = ' . $obj2->s2 . '<br>';\r\necho 'obj2->c1->s1 = ' . $obj2->c1->s1 . '<br>';\r\necho 'obj1->s1 = ' . $obj1->s1 . '<br>';\r\nunset($obj2);\r\n?>\r\n<\/pre>\n<p>The output should be like:<\/p>\n<p>obj2->s2 = default s2<br \/>\nobj2->c1->s1 = default s1<br \/>\nobj1->s1 = default s1<br \/>\nobj2->s2 = who did it?<br \/>\nobj2->c1->s1 = s1 did it?<br \/>\nobj1->s1 = s1 did it?<br \/>\nobj2->s2 = who did it?<br \/>\nobj2->c1->s1 = No, I didn&#8217;t!<br \/>\nobj1->s1 = No, I didn&#8217;t!<\/p>\n<p>Please note if you came across the problem of loading the assembly, I changed my file called <strong>AssemblyInfo.cs<\/strong> and set type visibility to true.<\/p>\n<pre lang=\"csharp\">\r\n\r\n\/\/some code\r\n\r\n\/\/ This sets the default COM visibility of types in the assembly to invisible.\r\n\/\/ If you need to expose a type to COM, use [ComVisible(true)] on that type.\r\n\r\n[assembly: ComVisible(true)]\r\n\r\n\/\/some code\r\n\r\n<\/pre>\n<p><strong>Advantages:<\/strong><br \/>\n1. You can see the business model or logic is in a compiled and secure state inside a DLL.<br \/>\n2. I think the library is loaded for every request, but I expect it to be faster than loading PHP classes in text files.<br \/>\n3. Instead of writing a PHP extension in C with difficulty, you can do it with one of many programming languages supported by .Net easily.<\/p>\n<p><strong>Disadvantages:<\/strong><br \/>\n1. You have to re-compile the DLL after each modification.<br \/>\n2. There may be overhead of loading the DLL. There are PHP caching tools as well.<br \/>\n3. Stability: I noticed that Apache has crashed a few times.<br \/>\n4. You may be forced to learn another programming language.<\/p>\n<p>So, it can be done.<\/p>\n<p><strong>Next steps:<\/strong><br \/>\n1. Testing and sorting out stability issues.<br \/>\n2. Testing with many classes, more <strong>complex<\/strong> classes and types, with more interaction with PHP (passing data in and out).<br \/>\n3. Finding a way of loading the DLL <strong>only once<\/strong> when Apache (web server process) is started, such as <a href=\"http:\/\/en.wikipedia.org\/wiki\/OLE_Automation\">OLE Automation<\/a> Servers, Word, Excel.<br \/>\n4. Benchmarking.<br \/>\n5. Optionally, moving the database operations from PHP to the DLL<br \/>\n   + an ORM (<a href=\"http:\/\/en.wikipedia.org\/wiki\/Object-relational_mapping\">Object-relational Mapping<\/a>) functionality could be ideal<\/p>\n<p>6. Porting: We can achieve all using Java and JVM which can run on operating systems other than Windows<br \/>\n  + also there are JPA (<a href=\"http:\/\/en.wikipedia.org\/wiki\/Java_Persistence_API\">Java Persistence API<\/a>) tools for Java ready to use<\/p>\n<p>PHP can also load Java classes. <a href=\"http:\/\/uk2.php.net\/manual\/en\/java.examples.php\">Here <\/a>are some examples.<\/p>\n<p>Happy coding!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Just an idea, not a new one, and I promise, it is not another &#8220;framework&#8221;&#8230;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[44,47,46,45,26],"class_list":["post-209","post","type-post","status-publish","format-standard","hentry","category-technology","tag-c-sharp","tag-java","tag-jpa","tag-orm","tag-php"],"_links":{"self":[{"href":"https:\/\/www.muratyaman.co.uk\/blog\/index.php\/wp-json\/wp\/v2\/posts\/209","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.muratyaman.co.uk\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.muratyaman.co.uk\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.muratyaman.co.uk\/blog\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.muratyaman.co.uk\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=209"}],"version-history":[{"count":12,"href":"https:\/\/www.muratyaman.co.uk\/blog\/index.php\/wp-json\/wp\/v2\/posts\/209\/revisions"}],"predecessor-version":[{"id":900,"href":"https:\/\/www.muratyaman.co.uk\/blog\/index.php\/wp-json\/wp\/v2\/posts\/209\/revisions\/900"}],"wp:attachment":[{"href":"https:\/\/www.muratyaman.co.uk\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=209"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.muratyaman.co.uk\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=209"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.muratyaman.co.uk\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=209"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}