Dynamically instantiate class in pure library?
5 posts
• Page 1 of 1
Dynamically instantiate class in pure library?
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
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?
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.
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. | @misekjakub │ jakub@devsense.com │
-
Jakub Misek - Posts: 2092
- Joined: January 4th, 2012, 2:42 pm
- Location: Prague
Re: Dynamically instantiate class in pure library?
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?
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
* Add the pure library into app.config file, so it will be loaded automatically when PHP context is initialized
Let me know if it works for you,
Thanks!
- 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);
* 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. | @misekjakub │ jakub@devsense.com │
-
Jakub Misek - Posts: 2092
- Joined: January 4th, 2012, 2:42 pm
- Location: Prague
Re: Dynamically instantiate class in pure library?
Sweet.
Thanks!
Thanks!
- hans
- Posts: 12
- Joined: December 12th, 2012, 11:32 pm
5 posts
• Page 1 of 1
Who is online
Users browsing this forum: No registered users and 21 guests