FAQ
Hi,

So PHP uses HashTable to store variables like $_POST,

From a HashTable structure, can I get the array name?

--
Best Regards,
Aaron Lewis - PGP: 0x13714D33 - http://pgp.mit.edu/
Finger Print: 9F67 391B B770 8FF6 99DC D92D 87F6 2602 1371 4D33

Search Discussions

  • Joe Watkins at May 4, 2014 at 3:14 pm

    On Sun, 2014-05-04 at 20:21 +0800, Aaron Lewis wrote:
    Hi,

    So PHP uses HashTable to store variables like $_POST,

    From a HashTable structure, can I get the array name?

    --
    Best Regards,
    Aaron Lewis - PGP: 0x13714D33 - http://pgp.mit.edu/
    Finger Print: 9F67 391B B770 8FF6 99DC D92D 87F6 2602 1371 4D33
    https://gist.github.com/krakjoe/50603be7ffb1ae6e9156

    Cheers
    Joe
  • Johannes Schlüter at May 4, 2014 at 3:46 pm

    On Sun, 2014-05-04 at 16:13 +0100, Joe Watkins wrote:
    On Sun, 2014-05-04 at 20:21 +0800, Aaron Lewis wrote:
    Hi,

    So PHP uses HashTable to store variables like $_POST,

    From a HashTable structure, can I get the array name?
    No, a zval (which might point to a hash tale) doesn't refer to a name.
    For one there in 99.99% is no need but it would cost memory(and CPU
    cycles to maintain) and then there miht e multiple nae (references) or
    none (temporaries etc.)

    If you know the parent such hacks like
    might work, but even this solution is flawed: The easy thing it doesn't
    support is references, it should return an array of all names. It won't
    work for complex things, though.

    If you need such a functionality I'd suggest to take a step back and
    think what you actually want to do ... usually there is a better
    solution.

    johannes
  • Aaron Lewis at May 4, 2014 at 3:54 pm
    FWD I forgot to CC the list ;-(

    ---------- Forwarded message ----------


    Here's what I want,

    If a user runs code like this, and $_GET['MH'] happens to be
    undefined, I want to know both '_GET' and 'MH':

    if ($_GET['MH'] == 'dog')
    {
           do-something
    }

    But so far I can only get the 'MH' part ( I checked the 'Undefined
    index' part in a zend function, can't remember the exact name )



    On Sun, May 4, 2014 at 11:46 PM, Johannes Schlüter
    wrote:
    On Sun, 2014-05-04 at 16:13 +0100, Joe Watkins wrote:
    On Sun, 2014-05-04 at 20:21 +0800, Aaron Lewis wrote:
    Hi,

    So PHP uses HashTable to store variables like $_POST,

    From a HashTable structure, can I get the array name?
    No, a zval (which might point to a hash tale) doesn't refer to a name.
    For one there in 99.99% is no need but it would cost memory(and CPU
    cycles to maintain) and then there miht e multiple nae (references) or
    none (temporaries etc.)

    If you know the parent such hacks like
    might work, but even this solution is flawed: The easy thing it doesn't
    support is references, it should return an array of all names. It won't
    work for complex things, though.

    If you need such a functionality I'd suggest to take a step back and
    think what you actually want to do ... usually there is a better
    solution.

    johannes


    --
    Best Regards,
    Aaron Lewis - PGP: 0x13714D33 - http://pgp.mit.edu/
    Finger Print: 9F67 391B B770 8FF6 99DC D92D 87F6 2602 1371 4D33


    --
    Best Regards,
    Aaron Lewis - PGP: 0x13714D33 - http://pgp.mit.edu/
    Finger Print: 9F67 391B B770 8FF6 99DC D92D 87F6 2602 1371 4D33
  • Tjerk Meesters at May 4, 2014 at 4:28 pm
    Hi,
    On 4 May, 2014, at 11:54 pm, Aaron Lewis wrote:

    FWD I forgot to CC the list ;-(

    ---------- Forwarded message ----------


    Here's what I want,

    If a user runs code like this, and $_GET['MH'] happens to be
    undefined, I want to know both '_GET' and 'MH':
    I suppose you could compare the hash table against the few super globals there are :)
    if ($_GET['MH'] == 'dog')
    {
    do-something
    }

    But so far I can only get the 'MH' part ( I checked the 'Undefined
    index' part in a zend function, can't remember the exact name )



    On Sun, May 4, 2014 at 11:46 PM, Johannes Schlüter
    wrote:
    On Sun, 2014-05-04 at 16:13 +0100, Joe Watkins wrote:
    On Sun, 2014-05-04 at 20:21 +0800, Aaron Lewis wrote:
    Hi,

    So PHP uses HashTable to store variables like $_POST,

    From a HashTable structure, can I get the array name?
    No, a zval (which might point to a hash tale) doesn't refer to a name.
    For one there in 99.99% is no need but it would cost memory(and CPU
    cycles to maintain) and then there miht e multiple nae (references) or
    none (temporaries etc.)

    If you know the parent such hacks like
    might work, but even this solution is flawed: The easy thing it doesn't
    support is references, it should return an array of all names. It won't
    work for complex things, though.

    If you need such a functionality I'd suggest to take a step back and
    think what you actually want to do ... usually there is a better
    solution.

    johannes


    --
    Best Regards,
    Aaron Lewis - PGP: 0x13714D33 - http://pgp.mit.edu/
    Finger Print: 9F67 391B B770 8FF6 99DC D92D 87F6 2602 1371 4D33


    --
    Best Regards,
    Aaron Lewis - PGP: 0x13714D33 - http://pgp.mit.edu/
    Finger Print: 9F67 391B B770 8FF6 99DC D92D 87F6 2602 1371 4D33

    --
    PHP Internals - PHP Runtime Development Mailing List
    To unsubscribe, visit: http://www.php.net/unsub.php
  • Johannes Schlüter at May 4, 2014 at 5:34 pm

    On Sun, 2014-05-04 at 23:54 +0800, Aaron Lewis wrote:
    Here's what I want,

    If a user runs code like this, and $_GET['MH'] happens to be
    undefined, I want to know both '_GET' and 'MH':

    if ($_GET['MH'] == 'dog')
    {
    do-something
    }

    But so far I can only get the 'MH' part ( I checked the 'Undefined
    index' part in a zend function, can't remember the exact name )
    What do you want to achieve? Some developer tool for better messages?

    For the general case of

        $someVar->method()['key1']['key2'] == 'baz'

    there is no good solution (well, you could try to analyze the current
    opcode array, try to find current opline in there and do magic, but
    that's quite crazy and notable effort)

    For the simple case you can look at Joe's example on how to iterate over
    the current symbol table, and if need be combine that with a recursive
    search (mind infinite recursion caused by references) but there the
    effort makes it questionable whether the information gained brings
    value ...

    johannes
  • Aaron Lewis at May 5, 2014 at 3:45 am
    Hi Watkins,

    First of all, pthreads@pthreads.org? Nice account

    I'm trying your patch, but in a different place

    In static inline zval **zend_fetch_dimension_address_inner(HashTable
    *ht, const zval *dim, int dim_type, int type TSRMLS_DC),

    fetch_string_dim:
    if (zend_hash_quick_find(ht, offset_key, offset_key_length+1, hval,
    (void **) &retval) == FAILURE) {
    ...

    fetch_string_dim:
    if (zend_hash_quick_find(ht, offset_key, offset_key_length+1, hval,
    (void **) &retval) == FAILURE) {
    switch (type) {
    case BP_VAR_R:
                             printf ("(R) Undefined index %s, looking up
    array variable name\n", offset_key);

                             HashPosition position;
                             zval **test = NULL;
                             for
    (zend_hash_internal_pointer_reset_ex(EG(active_symbol_table),
    &position);

    zend_hash_get_current_data_ex(EG(active_symbol_table), (void**)&test,
    &position) == SUCCESS;

    zend_hash_move_forward_ex(EG(active_symbol_table), &position)) {
                                 printf ("Comparing with: %s\n",
    ((Bucket*)position)->arKey);
                                 if (*test == ht) {
                                     printf ("Found variable name: %s\n",
    ((Bucket*)position)->arKey);
    //
    RETURN_STRINGL(((Bucket*)position)->arKey,
    ((Bucket*)position)->nKeyLength, 1);
                                 }
                             }

    ....


    Hmm, I got no match ... am I wrong somewhere?

    On Sun, May 4, 2014 at 11:13 PM, Joe Watkins wrote:
    On Sun, 2014-05-04 at 20:21 +0800, Aaron Lewis wrote:
    Hi,

    So PHP uses HashTable to store variables like $_POST,

    From a HashTable structure, can I get the array name?

    --
    Best Regards,
    Aaron Lewis - PGP: 0x13714D33 - http://pgp.mit.edu/
    Finger Print: 9F67 391B B770 8FF6 99DC D92D 87F6 2602 1371 4D33
    https://gist.github.com/krakjoe/50603be7ffb1ae6e9156

    Cheers
    Joe


    --
    Best Regards,
    Aaron Lewis - PGP: 0x13714D33 - http://pgp.mit.edu/
    Finger Print: 9F67 391B B770 8FF6 99DC D92D 87F6 2602 1371 4D33
  • Aaron Lewis at May 5, 2014 at 3:48 am
    Please ignore that, it's badly formatted, see this one instead

    https://gist.github.com/CaledoniaProject/2c2871b5fa36ab6a93e4#file-php-test-inner
    On Mon, May 5, 2014 at 11:45 AM, Aaron Lewis wrote:
    Hi Watkins,

    First of all, pthreads@pthreads.org? Nice account

    I'm trying your patch, but in a different place

    In static inline zval **zend_fetch_dimension_address_inner(HashTable
    *ht, const zval *dim, int dim_type, int type TSRMLS_DC),

    fetch_string_dim:
    if (zend_hash_quick_find(ht, offset_key, offset_key_length+1, hval,
    (void **) &retval) == FAILURE) {
    ...

    fetch_string_dim:
    if (zend_hash_quick_find(ht, offset_key, offset_key_length+1, hval,
    (void **) &retval) == FAILURE) {
    switch (type) {
    case BP_VAR_R:
    printf ("(R) Undefined index %s, looking up
    array variable name\n", offset_key);

    HashPosition position;
    zval **test = NULL;
    for
    (zend_hash_internal_pointer_reset_ex(EG(active_symbol_table),
    &position);

    zend_hash_get_current_data_ex(EG(active_symbol_table), (void**)&test,
    &position) == SUCCESS;

    zend_hash_move_forward_ex(EG(active_symbol_table), &position)) {
    printf ("Comparing with: %s\n",
    ((Bucket*)position)->arKey);
    if (*test == ht) {
    printf ("Found variable name: %s\n",
    ((Bucket*)position)->arKey);
    //
    RETURN_STRINGL(((Bucket*)position)->arKey,
    ((Bucket*)position)->nKeyLength, 1);
    }
    }

    ....


    Hmm, I got no match ... am I wrong somewhere?

    On Sun, May 4, 2014 at 11:13 PM, Joe Watkins wrote:
    On Sun, 2014-05-04 at 20:21 +0800, Aaron Lewis wrote:
    Hi,

    So PHP uses HashTable to store variables like $_POST,

    From a HashTable structure, can I get the array name?

    --
    Best Regards,
    Aaron Lewis - PGP: 0x13714D33 - http://pgp.mit.edu/
    Finger Print: 9F67 391B B770 8FF6 99DC D92D 87F6 2602 1371 4D33
    https://gist.github.com/krakjoe/50603be7ffb1ae6e9156

    Cheers
    Joe


    --
    Best Regards,
    Aaron Lewis - PGP: 0x13714D33 - http://pgp.mit.edu/
    Finger Print: 9F67 391B B770 8FF6 99DC D92D 87F6 2602 1371 4D33


    --
    Best Regards,
    Aaron Lewis - PGP: 0x13714D33 - http://pgp.mit.edu/
    Finger Print: 9F67 391B B770 8FF6 99DC D92D 87F6 2602 1371 4D33

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupphp-internals @
categoriesphp
postedMay 4, '14 at 12:21p
activeMay 5, '14 at 3:48a
posts8
users4
websitephp.net

People

Translate

site design / logo © 2022 Grokbase