Passing Parameters to COM constructors?

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

I posted this to the notes page of the COM PHP documentation... I will
post an update there if I find a solution here.

I have need for a commercial COM/DOTNET class library that requires a
paramater to be passed when the object is created.

It does not appear as though PHP allows for the instantiation of
COM/DOTNET objects that require a parameter to be passed to their
constructor... I am not sure if I am being clear, but suppose to create
a new Mercedes you would use:

$car = new COM("Car.Mercedes");

What if the constructor for that class required that you specified a
model of the car in order to instantiate it?


$car = new COM("Car.Mercedes('SL600')");

....or something like that. Is it possible? Is there a work-around?



Re: Passing Parameters to COM constructors?

Quoted text here. Click to load it

I do not know if it is possible since I haven't used com for a project
since 2001.  My first thought is no.  My thought process is basically
that PHP is calling the com object and allocating the memory for the com
object but not exactly maintaining it as an object like PHP would.

However, most languages will allow you to call the constructor after you
have initialized the object (depending).  However it is not known as the
best practice but yet again workarounds are not best practice either.

You might be able to find something that deals with this in the PEAR
library, however without spending time looking for it I am unsure.


Re: Passing Parameters to COM constructors?

There is such a thing as a COM constructor? The standard way to create
an object in COM is to get its class factory, then call
TClassFactory::CreateInstance, which doesn't accept initialization data.

Re: Passing Parameters to COM constructors?

Thanks guys, for the info. I am actually trying to get a 3rd party
DOTNET component working in PHP. The main class of this component
requires me to pass a parameter to the constructor (see my example
above) -- sorry that I used COM in the example instead of DOTNET- I was
just trying to be demonstrative and thought that both approached would
work the same way.

As it stands, it looks as though I am SOL in trying to use this product
(Aspose Powerpoint) with PHP :(  I am really hoping there is a way!

Re: Passing Parameters to COM constructors? wrote:
Quoted text here. Click to load it

There might be, but most of the work is going to be done outside

I am guessing you are trying to work with the Presentation class.
If I remember correctly, this class is not inheritable, so you
can't extend it.  You need to create a container object for the
Presentation object.  In VB.NET, it could look like this:

Public Class PresentationContainer
  Public ContainedPresentation As Aspose.PowerPoint.Presentation
  Public Sub New()
    Set ContainedPresentation to Nothing
  End Sub
  Public Sub InitPresentation(file As String)
    Me.ContainedPresentation = New Aspose.PowerPoint.Presentation(file)
  End Sub
End Class

Then you compile the container and access the Presentation from
PHP as PresentationContainer::ContainedPresentation.


Re: Passing Parameters to COM constructors?

Wow.. such a simple idea, but a perfect solution. With a little
tweaking to your code I have succesfully done what I set out to do:
create an instance of that *&%^ presentation object :-)

I will post more formal directions & sample code to this thread once I
work out a few more details using .NET with PHP. For example, the
component returns a .net Image object. Well, if I want to save this
image to a file, I can call ImageObject.Save(Filename, Format) but the
Format is a .net enumeration that I can't seem to access from PHP (nor
can I find the actual values for this enumeration).

Thank you very much, NC, for your help on this- I knew there was a way!

Re: Passing Parameters to COM constructors? wrote:
Quoted text here. Click to load it


Quoted text here. Click to load it

Again, consider implementing this functionality inside the
container object and expose to PHP only things that you can't
mess up.  Also, I have a different recollection of the Image::Save()
method syntax: the second argument is not an enumeration, but an
ImageFormat object...  


Site Timeline