Re: problems compiling new PECL module
| From: | Gustavo Lopes | Date: | Mon, 10 Jan 2011 19:44:23 +0000 |
| Subject: | Re: problems compiling new PECL module | ||
| References: | 1 2 3 4 5 6 7 | Groups: | php.pecl.dev |
| Request: | Send a blank email to pecl-dev+get-7934@lists.php.net to get a copy of this message | ||
On Mon, 10 Jan 2011 19:10:48 -0000, Tim Gustafson <tjg@tgustafson.com>
wrote:
In C++, what I want to do is: ---------- noise::module::Const n_const; n_const.SetConstValue(-1.0); noise::module::Abs n_abs; n_abs.SetSourceModule(0, n_const); noise::module::Module n_module* = NULL; n_module = n_abs.GetSourceModule(0); /* this should return a pointer to n_const */ ---------- The PHP translation would look something like this: ---------- $n_const = new Noise_Const(); $n_const->SetConstValue(-1.0); $n_abs = new Noise_Abs(); $n_abs->SetSourceModule(0, $n_const); /* this is now working properly */ $n_module = $n_abs->GetSourceModule(0); /* $n_module should now be a reference to $n_const; this currently core dumps */ ---------- So, the return value for GetSourceModule is an instance of a noise::module::Module (the abstract class for the other two classes) that has already been instantiated in my PHP code and assigned via SetSourceModule. Does that make sense?I see two options here. Option 1: * In PHP method Noise_Const::SetSourceModule, you merely unwrap $n_const and use the result to call the native SetSourceModule. * In PHP method Noise_Abs::GetSourceModule, you call the native GetSourceModule and create a new PHP object of type Noise_Const from the native result. Therefore, the new object will be !== from the original object. Option 2: * In PHP method Noise_Const::SetSourceModule, you unwrap $n_const and use the result to call the native SetSourceModule, but you also store the zval* in the object structure or in an object property (if I recall, if you store the zval* in the object structure, you should also copy the zval in order not to have problems in the request shutdown phase -- do not merely copy the pointer and increment the refcount). Alternatively, you could instead store the object handle, as long as you take care of the object (store) refcount. * In PHP method Noise_Abs::GetSourceModule, you skip calling the native function and instead return the object you have stored and increment the zval refcount or copy the zval (if you stored the handle, you must create a new zval and also increment the object refcount). You will be returning the same object (===) that was passed to SetSourceModule. -- Gustavo Lopes