Dynamically instantiate class in pure library?

Discussion about the open-source Phalanger [?] project.

Dynamically instantiate class in pure library?

Postby hans » January 29th, 2013, 8:48 pm

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
hans
 
Posts: 12
Joined: December 12th, 2012, 11:32 pm

Re: Dynamically instantiate class in pure library?

Postby Jakub Misek » January 29th, 2013, 9:36 pm

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.
Jakub Misek │ DEVSENSE s.r.o. | @misekjakubjakub@devsense.com
User avatar
Jakub Misek
 
Posts: 2092
Joined: January 4th, 2012, 2:42 pm
Location: Prague

Re: Dynamically instantiate class in pure library?

Postby hans » January 29th, 2013, 11:48 pm

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.
You do not have the required permissions to view the files attached to this post.
hans
 
Posts: 12
Joined: December 12th, 2012, 11:32 pm

Re: Dynamically instantiate class in pure library?

Postby Jakub Misek » January 30th, 2013, 2:22 pm

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!
Jakub Misek │ DEVSENSE s.r.o. | @misekjakubjakub@devsense.com
User avatar
Jakub Misek
 
Posts: 2092
Joined: January 4th, 2012, 2:42 pm
Location: Prague

Re: Dynamically instantiate class in pure library?

Postby hans » January 31st, 2013, 5:26 pm

Sweet.

Thanks!
hans
 
Posts: 12
Joined: December 12th, 2012, 11:32 pm


Return to Phalanger project

Who is online

Users browsing this forum: No registered users and 1 guest

cron

User Control Panel

Login

Who is online

In total there is 1 user online :: 0 registered, 0 hidden and 1 guest (based on users active over the past 5 minutes)
Most users ever online was 151 on December 6th, 2020, 7:46 am

Users browsing this forum: No registered users and 1 guest