# Making a variable global question

- Thread: https://externals.io/message/3671
- Original archive: https://news-web.php.net/php.internals/3671
- Started by Ken Spencer on 2003-08-01T00:44:46+00:00
- Messages: 3
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #3671 — Ken Spencer — 2003-08-01T00:44:46+00:00

URL: https://externals.io/message/3671#3671

I am trying to make a function for my extension that makes a predefined 
variable global 

I have tried something along the lines of:

 if(zend_hash_find(&EG(symbol_table), var, strlen(var)+1,  (void **)  
   &vars_data)!=FAILURE)
{
  ZEND_SET_SYMBOL(EG(active_symbol_table), var,   *vars_data);
}

but it seg faults upon second execution. 

Can someone reccomend a more successful method of accomplishing this? 


Chhers,

Ken

---

## #3681 — Mark Spruiell — 2003-08-01T15:46:45+00:00

URL: https://externals.io/message/3671#3681 · In reply to: #3671

Hi Ken,

I'm fairly new to PHP extension programming, so forgive me if I'm
missing the obvious, but your code appears to be trying to copy a
global variable to a local scope. Is that your intent?

If so, why can't the script do this itself by using a "global"
declaration for the variable? For example:

<?
$v = "foo";

function bar()
{
  global $v;
  echo "v = $v\n";
}
?>

As for why you're getting a segfault, I suspect it's a reference
counting issue. Have a look at the definition of
ZEND_SET_SYMBOL_WITH_LENGTH in zend_API.h and you'll see it is
modifying the container you retrieved from EG(symbol_table).

Good luck,
- Mark

> I am trying to make a function for my extension that makes a predefined
> variable global
>
> I have tried something along the lines of:
>
>  if(zend_hash_find(&EG(symbol_table), var, strlen(var)+1,  (void **)
>    &vars_data)!=FAILURE)
> {
>   ZEND_SET_SYMBOL(EG(active_symbol_table), var,   *vars_data);
> }
>
> but it seg faults upon second execution.
>
> Can someone reccomend a more successful method of accomplishing this?
>
>
> Chhers,
>
> Ken

---

## #3692 — Zeev Suraski — 2003-08-01T22:16:46+00:00

URL: https://externals.io/message/3671#3692 · In reply to: #3671

At 03:44 01/08/2003, Ken Spencer wrote:


>I am trying to make a function for my extension that makes a predefined
>variable global
>
>I have tried something along the lines of:
>
>  if(zend_hash_find(&EG(symbol_table), var, strlen(var)+1,  (void **)
>    &vars_data)!=FAILURE)
>{
>   ZEND_SET_SYMBOL(EG(active_symbol_table), var,   *vars_data);
>}

You're messing with the reference count here.  ZEND_SET_SYMBOL() 'takes 
over' this zval, and the original copy becomes corrupted.  That's probably 
why it fails the second time you try it.  You need to use 
ZEND_SET_SYMBOL_WITH_LENGTH() instead, and take care of the reference count 
manually.

When touching a zval coming back from an engine-controlled symbol table, 
you must take care of the reference count (in this case, increment it).  If 
you want the two variables to be references, that is, changing one would 
change the other - you should also turn on the is_ref bit, and not forget 
to first separate it.  It should look something like this:

if(zend_hash_find(&EG(symbol_table), var, strlen(var)+1,  (void 
**)  &vars_data)!=FAILURE) {
         SEPARATE_ZVAL_IF_NOT_REF(vars_data);
         (*vars_data)->refcount++;
         ZEND_SET_SYMBOL_WITH_LENGTH(EG(active_symbol_table), 
var,  strlen(var)+1, *vars_data, (*vars_data)->refcount, 1);
}

Zeev
