Cheating PHP integers

前端 未结 1 1300
渐次进展
渐次进展 2020-12-16 07:32

This is in relation to my post here but taken in a completely different direction Charset detection in PHP

essentially, i\'m looking to reduce the memory that many h

相关标签:
1条回答
  • 2020-12-16 07:50

    Don't tell nobody!

    class IntegerstringArray IMPLEMENTS ArrayAccess {
    
        var $evil = "0000111122220000ffff";
    
                     // 16 bit each
    
    
        function offsetExists ( $offset ) {
            return (strlen($this->evil) / 4) - 1 >= $offset;
        }
    
        function offsetGet ( $offset ) {
            return hexdec(substr($this->evil, $offset * 4, 4));
        }
    
        function offsetSet ( $offset , $value ) {
    
            $hex = dechex($value);
            if ($fill = 4 - strlen($hex)) {
                $hex = str_repeat("0",  $fill) . $hex;
            }
    
            for ($i=0; $i<4; $i++) {
                $this->evil[$offset*4+$i] = $hex[$i];
            }
        }
    
        function offsetUnset ( $offset ) {
            assert(false);
        }
    
    }
    

    So you can pretty much create an array object from this:

    $array = new IntegerstringArray();
    $array[2] = 65535;
    print $array[2];
    

    It internally stores a list and accepts 16-bit integers. The array offsets must be consecutive.

    Not tested. Just as an implementation guide.

    0 讨论(0)
提交回复
热议问题