Just an idea, not a new one, and I promise, it is not another “framework”:

Is it worth to create a class library as a DLL for a business model and use that DLL in PHP?
What are the advantages and disadvantages?

As soon as we say DLL, it is a solution that can be used only in Windows 🙁

First, let us discuss what we mean about this class library.
1. For proof of concept, our classes will be very primitive. Here is a sample C# namespace:

using System;

namespace myClassLib
{

	public class Class1
	{
		public string s1;
		
		public Class1(){
			s1 = "default s1";
		}
	}
	
	public class Class2
	{
		public string s2;
		public Class1 c1;
		
		public Class2(){
			s2 = "default s2";
			c1 = new Class1();
		}
		
		public bool changeData(){
			s2 = "who did it?";
			c1.s1 = "s1 did it";
			return true;
		}
	}	
}

There is nothing fancy about it; only 2 related classes with string properties.

2. Compile the class library and copy the myClassLib.dll into the Apache binary folder, next to the httpd.exe, start Apache.

3. Write a simple PHP code, load the library (DOTNET) and refer to the classes in it:

c1;
echo 'obj2->s2 = ' . $obj2->s2 . '
'; echo 'obj2->c1->s1 = ' . $obj2->c1->s1 . '
'; echo 'obj1->s1 = ' . $obj1->s1 . '
'; $obj2->changeData(); echo 'obj2->s2 = ' . $obj2->s2 . '
'; echo 'obj2->c1->s1 = ' . $obj2->c1->s1 . '
'; echo 'obj1->s1 = ' . $obj1->s1 . '
'; $obj1->s1 = "No, I didn't!"; echo 'obj2->s2 = ' . $obj2->s2 . '
'; echo 'obj2->c1->s1 = ' . $obj2->c1->s1 . '
'; echo 'obj1->s1 = ' . $obj1->s1 . '
'; unset($obj2); ?>

The output should be like:

obj2->s2 = default s2
obj2->c1->s1 = default s1
obj1->s1 = default s1
obj2->s2 = who did it?
obj2->c1->s1 = s1 did it?
obj1->s1 = s1 did it?
obj2->s2 = who did it?
obj2->c1->s1 = No, I didn’t!
obj1->s1 = No, I didn’t!

Please note if you came across the problem of loading the assembly, I changed my file called AssemblyInfo.cs and set type visibility to true.


//some code

// This sets the default COM visibility of types in the assembly to invisible.
// If you need to expose a type to COM, use [ComVisible(true)] on that type.

[assembly: ComVisible(true)]

//some code

Advantages:
1. You can see the business model or logic is in a compiled and secure state inside a DLL.
2. I think the library is loaded for every request, but I expect it to be faster than loading PHP classes in text files.
3. Instead of writing a PHP extension in C with difficulty, you can do it with one of many programming languages supported by .Net easily.

Disadvantages:
1. You have to re-compile the DLL after each modification.
2. There may be overhead of loading the DLL. There are PHP caching tools as well.
3. Stability: I noticed that Apache has crashed a few times.
4. You may be forced to learn another programming language.

So, it can be done.

Next steps:
1. Testing and sorting out stability issues.
2. Testing with many classes, more complex classes and types, with more interaction with PHP (passing data in and out).
3. Finding a way of loading the DLL only once when Apache (web server process) is started, such as OLE Automation Servers, Word, Excel.
4. Benchmarking.
5. Optionally, moving the database operations from PHP to the DLL
+ an ORM (Object-relational Mapping) functionality could be ideal

6. Porting: We can achieve all using Java and JVM which can run on operating systems other than Windows
+ also there are JPA (Java Persistence API) tools for Java ready to use

PHP can also load Java classes. Here are some examples.

Happy coding!