-1

Possible Duplicate:
How can I use C++ code to interact with PHP?

I have the following test.php file:

<html>
 <head> 
 <title>Personalized Hello World</title> 
 </head> 

 <body> 

 <?php include "/home/test.hpp"; 

$s= new example();

 ?> 


 </body>

</html>

the test.hpp file is:

#ifndef TEST_
#define TEST_


class example
{
    public:
    example();
    void show();
};

#endif

I have the test.so file. I compile .php with: php test.php -dextension=test.so

I have the error:

PHP Fatal error:  Class 'example' not found in /var/www/test.php on line 10

How to resolve this? THX

Community
  • 1
  • 1
sunset
  • 1,359
  • 5
  • 22
  • 35

1 Answers1

1

You are trying to include C++ code from a PHP script. The PHP interpreter is not able to parse C++ code. Only PHP code. Because is a PHP interpreter. Not a C++ Compiler. These are different languages.

As @J0HN advises, you should read that : How can I use C++ code to interact with PHP?. It explains how to write extensions to PHP, which is the right way to interface PHP with C++ code.

Community
  • 1
  • 1
slaphappy
  • 6,894
  • 3
  • 34
  • 59
  • ok. thx for your advice. so should i create a .php file that connects with the methods in my .cpp code and than link all with the .so file. please light me up a little. I am asking you this because what if the methods from the .cpp file includes other .h files? do i have to wrapp all .cpp files in the .php or just the ost importat ones? – sunset Aug 31 '11 at 14:04
  • No, actually, you have to write a special module in C or C++ (AFAIR) that exports the API you want to use in PHP, compile that as a .so file, and then you can use these functions in your PHP code. – slaphappy Aug 31 '11 at 14:07
  • I think this article should help you : http://devzone.zend.com/article/4486-Wrapping-C-Classes-in-a-PHP-Extension – slaphappy Aug 31 '11 at 14:08
  • so i don't need to rewrite the methods from my .cpp code? i did saw the article you gave. i don't understand it quite clearly. do you have another easiest example? appreciate. thx – sunset Aug 31 '11 at 14:09
  • one more thing. WHERE IS CONFIG.m4? i didn;t find it – sunset Aug 31 '11 at 14:14
  • I'm sorry, but writing PHP extensions is not straightforward. This article is about as simple as it can get if you want to actually get some things done. If it's really too hard for you, there are other methods for interfacing PHP with C++ code; http://php.net/manual/en/book.soap.php http://pecl.php.net/package/ffi http://php.net/manual/en/function.exec.php. Pick the one you're most confortable with. Be wary that `exec()` kills kittens though. – slaphappy Aug 31 '11 at 14:16
  • config.m4 should be in the PHP sources. – slaphappy Aug 31 '11 at 14:19
  • @sunset: Perhaps a chat room would be better suited to this line of inquiry. You have posed a series of connected questions in a conversational style, which is not a good fit to our knowledgebase format. Thanks – Lightness Races in Orbit Aug 31 '11 at 14:26