Page 1 of 1

Dynamically instantiate class in pure library?

PostPosted: January 29th, 2013, 8:48 pm
by hans
Hello,

I have some PHP that I'm trying to convert to a pure library for performance reasons (our project uses autoload). Some spots in our code dynamically instantiate classes based on class name. This doesn't seem to be working when compiled as a pure library. It does work when running a multi-script assembly with auto-load. The class is namespaced.

Is this intended behavior?

Thanks,
- Hans

Re: Dynamically instantiate class in pure library?

PostPosted: January 29th, 2013, 9:36 pm
by Jakub Misek
Hi,

This should work. Just note, the name of the class must not be relative to current namespace. You have to use full namespaced name.

Re: Dynamically instantiate class in pure library?

PostPosted: January 29th, 2013, 11:48 pm
by hans
I created a sample solution which shows the problem. It's just a C# console app referencing a PHP lib which does a static and dynamic class instantiation. I'm using the latest Phalanger sources from Github.

Re: Dynamically instantiate class in pure library?

PostPosted: January 30th, 2013, 2:22 pm
by Jakub Misek
Great! I see two issues in Phalanger here, one in the sample code.

- class name should be "PhpLib\\TargetClass" (not "\\TargetInstantiation\\PhpLib\\TargetClass")

- Phalanger does not handle leading '\' character

- Since the library is compiled as pure, and you are only referencing it from C# code, there is no information for Phalanger that this DLL has to be loaded into the PHP context.

First two issues are easy to workaround from your code. Second one will be fixed in Phalanger in some future update.

The last issue can be fixed in two ways:
* load assemblies containing functions/types, you would like to use dynamically, from your C# code
Code: Select all
PHP.Core.ApplicationContext.Default.AssemblyLoader.Load(typeof(PhpLib.Library).Assembly, null);
This loads types from pure assembly into PHP context, so they can be resolved dynamically.

* Add the pure library into app.config file, so it will be loaded automatically when PHP context is initialized
Code: Select all
  <phpNet>
    <classLibrary>
      <add url="PhpLib.dll" />
    </classLibrary>
  </phpNet>


Let me know if it works for you,
Thanks!

Re: Dynamically instantiate class in pure library?

PostPosted: January 31st, 2013, 5:26 pm
by hans
Sweet.

Thanks!