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

1
  • Can you provide line numbers so we know which line 79 is? Commented Sep 13, 2011 at 11:15

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

4 Comments

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 stackoverflow.com/questions/7398259/…. PLEASE HELP!! THX VERY MUCH. REALLY APPRECIATE!!
@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 :).
what about the second question? how to access a variable from php? thx
Nope, sorry it's PHP C++ stuff that I don't know.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.