0

I have some c++ OOP classes (separated files) , and I want to create objects in PHP code that can call the functions in those classes.. (PHP classes derived from c++ classes).

Because the php is not more than a scripting language, I've got some functions that can only writen by a low level language that has a closed relation with the hardware, (so the c++ is the best choice).

but I want to use those functions(that are without a main function) in a php code, is this possible?.

SmootQ
  • 2,096
  • 7
  • 33
  • 58

2 Answers2

2

PHP classes derived from c++ classes

I don't think there is any way for such close interaction.

You would have to either

Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • "Write an external program and run it using exec()", But I don't want to write it as a programm, because it has no main function. I only want to call the source code functions, that's all. – SmootQ Dec 06 '10 at 20:35
  • @Simo in that case, you'll have to write a PHP extension. I don't think there is any other way. – Pekka Dec 06 '10 at 20:35
  • 2
    As a note: you can always write an extension in C++, and then the parts that interact with PHP can be written in C. Assuming you get all the compiler and linker magic correct, it should be doable (ie: extern C in the C++ code, compile your code with the same GCC version as what compiled the php code... etc. etc.) – Dragontamer5788 Dec 06 '10 at 21:13
1

I think the easiest to achieve what you intend to do is to write your C++ functions and export them flat (i.e. not as class). And, using SWIG, write a facade class that calls those functions. In PHP, you'd simply use your facade class in the OOP manner.

Zach Saw
  • 4,308
  • 3
  • 33
  • 49