1

I have in [example.cc] a method :

std::string Car::accelerate (std::string n)
{
cout<<n<<endl;
return n;
}

I would like to call this method from a php extension

I wrote this in my [test_php.cc] extension:

 char *strr=NULL;
 int strr_len;
 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &strr, &strr_len) == FAILURE) {
        RETURN_NULL();
  }
  std::string s(strr);
RETURN_STRING(car->accelerate(s),1);

I have the following error:

warning: deprecated conversion from string constant to ‘char*’
/home/me.cc:79: error: cannot convert ‘std::string’ to ‘const char*’ in initialization
/home/me.cc: At global scope:warning: deprecated conversion from string constant to ‘char*’

If i change the return_string(..) _ with a simple call car->accelerate(s); it works..it's true it doesn't print anything as a return function. need some help. appreciate

Gordon
  • 312,688
  • 75
  • 539
  • 559
sunset
  • 1,359
  • 5
  • 22
  • 35

1 Answers1

2

Given your recent comment I'll propose this as an (uneducated) answer.

RETURN_STRING() takes a const char* in it's first parameter. That's what you call a "C-String", and there is no automatic conversion from std::string to const char * (at least for the compiler).

What you want to do is pass a const char* as the first argument. To generate a const char* from a std::string you call the method c_str(). I.e. change your last line to:

RETURN_STRING(car->accelerate(s) . c_str(), 1);
V.S.
  • 2,924
  • 4
  • 32
  • 43
  • thx a lot! It works. I am new t php. I would like to ask you how to create php extension to nested classes ? I mean my example.cc looks: class a{ public: a(); class b {public: b(); ...variables};}; how to create the php_example.cc? AND another question is here http://stackoverflow.com/questions/7398259/how-to-access-a-variable-from-a-class-using-php-extension/7398305#comment-8937444. PLEASE HELP!! THX VERY MUCH. REALLY APPRECIATE!! – sunset Sep 13 '11 at 11:31
  • @sunset, I apologise, but (you may not realise this) your question wasn't a PHP question. It was answerable with purely a knowledge of C++ and a lot of experience of compiler errors! What's more I've never hadd any need to nest classes, so I can't help there. Good luck though :). – V.S. Sep 13 '11 at 11:38
  • what about the second question? how to access a variable from php? thx – sunset Sep 13 '11 at 11:42
  • Nope, sorry it's PHP C++ stuff that I don't know. – V.S. Sep 13 '11 at 11:45