├── .github └── workflows │ └── tests.yml ├── .gitignore ├── .gitmodules ├── Dockerfile ├── Dockerfile-compile ├── LICENSE ├── Makefile ├── README.md ├── bin └── render-table.php ├── composer.json ├── functions ├── var_class_sizeof.php └── var_sizeof.php ├── library ├── ffi.cpp ├── ffi.h ├── ffi_darwin_arm64.dylib ├── ffi_darwin_x86_64.dylib ├── ffi_linux_aarch64.so ├── ffi_linux_x86_64.so └── spl.h ├── phpunit.xml.dist ├── src └── VarInfo.php └── tests ├── VarClassSizeOfTest.php └── VarSizeOfTest.php /.github/workflows/tests.yml: -------------------------------------------------------------------------------- 1 | name: "Tests" 2 | 3 | on: 4 | push: 5 | 6 | jobs: 7 | phpunit: 8 | name: "Tests" 9 | 10 | runs-on: ${{ matrix.operating-system }} 11 | 12 | strategy: 13 | matrix: 14 | php-version: 15 | - "7.4" 16 | - "8.0" 17 | - "8.1" 18 | - "8.2" 19 | - "8.3" 20 | operating-system: 21 | - "ubuntu-latest" 22 | 23 | steps: 24 | - name: "Checkout" 25 | uses: "actions/checkout@v2" 26 | 27 | - name: "Install PHP" 28 | uses: "shivammathur/setup-php@v2" 29 | with: 30 | php-version: "${{ matrix.php-version }}" 31 | ini-values: memory_limit=-1 32 | tools: composer:v2 33 | 34 | - name: "Install dependencies" 35 | run: "composer install --no-interaction --no-progress --no-suggest" 36 | 37 | - name: "Tests" 38 | run: "./vendor/bin/phpunit" 39 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | php-src 3 | vendor 4 | composer.lock 5 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "php-src"] 2 | path = php-src 3 | url = git@github.com:php/php-src.git 4 | shallow = true 5 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM php:8.1-cli 2 | 3 | COPY --from=mlocati/php-extension-installer /usr/bin/install-php-extensions /usr/bin/ 4 | 5 | RUN install-php-extensions ffi mbstring 6 | 7 | RUN mkdir /app 8 | 9 | RUN groupadd -r ffi && useradd -m -g ffi ffi 10 | 11 | RUN chown -R ffi /app 12 | 13 | USER ffi 14 | 15 | WORKDIR /app 16 | -------------------------------------------------------------------------------- /Dockerfile-compile: -------------------------------------------------------------------------------- 1 | FROM debian:buster-slim 2 | 3 | RUN set -eux; \ 4 | apt-get update; \ 5 | apt-get install -y --no-install-recommends \ 6 | autoconf \ 7 | dpkg-dev \ 8 | file \ 9 | g++ \ 10 | gcc \ 11 | libc-dev \ 12 | make \ 13 | pkg-config \ 14 | re2c \ 15 | build-essential \ 16 | bison \ 17 | libxml2-dev \ 18 | libffi-dev \ 19 | libsqlite3-dev \ 20 | strace; 21 | 22 | COPY . /code 23 | 24 | WORKDIR /code/php-src 25 | 26 | RUN ./buildconf 27 | RUN ./configure --with-ffi 28 | RUN make 29 | RUN make install 30 | 31 | RUN ./buildconf 32 | RUN ./configure --with-ffi 33 | 34 | WORKDIR /code 35 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License 2 | 3 | Copyright (c) 2020 Sukhachev Anton 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining 6 | a copy of this software and associated documentation files (the 7 | "Software"), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 20 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 21 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 22 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | OS := $(shell uname -s) 2 | PLATFORM := $(shell uname -m) 3 | HFOLDERS := -I php-src -I php-src/Zend -I php-src/main -I php-src/TSRM -I php-src/ext/spl 4 | COMMAND := @echo Invalid operation system: $(OS) 5 | 6 | ifeq ($(DEBUG), 1) 7 | DEBUGFLAG := -D DEBUG 8 | else 9 | DEBUGFLAG := 10 | endif 11 | 12 | ifeq ($(OS), Linux) 13 | COMMAND := g++ -O3 -fPIC -shared $(HFOLDERS) $(DEBUGFLAG) -o library/ffi_linux_$(PLATFORM).so library/ffi.cpp 14 | endif 15 | 16 | ifeq ($(OS), Darwin) 17 | COMMAND := clang -shared -undefined dynamic_lookup $(HFOLDERS) $(DEBUGFLAG) -o library/ffi_darwin_$(PLATFORM).dylib library/ffi.cpp 18 | endif 19 | 20 | all: 21 | $(COMMAND) 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # PHP var_sizeof() 2 | 3 | ![Tests](https://github.com/mrsuh/php-var-sizeof/actions/workflows/tests.yml/badge.svg) 4 | 5 | Function for getting size of any PHP variable in bytes.
6 | It must be more accurate tool to calculate total size of PHP variable than `memory_get_usage()`, but it has [restrictions](#warning-restrictions). 7 | 8 | ### How it works 9 | `var_sizeof()` with `var_class_sizeof()` uses FFI to access internal structures of PHP variables.
10 | It calculates the size of internal structures such as `zval`, `_zend_array`, `_zend_object`, etc., as well as additional allocated memory for them.
11 | It doesn't take into calculate the memory of handlers/functions/etc. 12 | 13 | ### Requirements 14 | * PHP >= 7.4 (with FFI) 15 | * Linux(x86_64/aarch64) / Darwin(x86_64/arm64) 16 | 17 | ### How to install 18 | ```bash 19 | composer require mrsuh/php-var-sizeof 20 | ``` 21 | 22 | ### Functions 23 | ```php 24 | int var_sizeof(mixed $var); 25 | ``` 26 | 27 | ```php 28 | int var_class_sizeof(mixed $var); 29 | ``` 30 | 31 | ## Usage 32 | 33 | ```php 34 | getProperties() as $reflectionProperty) { 30 | $properties[] = sdump($reflectionProperty->getValue($var)); 31 | } 32 | 33 | if($var instanceof \ArrayIterator) { 34 | $properties[] = sdump((array)$var); 35 | } 36 | 37 | return sprintf("%s%s", get_class($var), str_replace(['[', ']'], ['{', '}'], json_encode($properties))); 38 | } 39 | 40 | return ''; 41 | } 42 | 43 | class EmptyClass 44 | { 45 | } 46 | 47 | class ClassWithArray 48 | { 49 | public array $array; 50 | 51 | public function __construct(array $array) 52 | { 53 | $this->array = $array; 54 | } 55 | } 56 | 57 | class ClassWithObject 58 | { 59 | public EmptyClass $container; 60 | 61 | public function __construct() 62 | { 63 | $this->container = new EmptyClass(); 64 | } 65 | } 66 | 67 | $tableCommonBuilder = new \MaddHatter\MarkdownTable\Builder(); 68 | $tableCommonBuilder->headers(['type', 'var_sizeof(bytes)', 'memory_get_usage(bytes)']); 69 | 70 | $tableObjectBuilder = new \MaddHatter\MarkdownTable\Builder(); 71 | $tableObjectBuilder->headers(['type', 'var_class_sizeof(bytes)', 'var_sizeof(bytes)', 'memory_get_usage(bytes)']); 72 | 73 | function addCommonRow(\MaddHatter\MarkdownTable\Builder $tableBuilder, $var, int $memory) 74 | { 75 | $tableBuilder->row([sdump($var), number_format(var_sizeof($var)), number_format($memory)]); 76 | } 77 | 78 | function addObjectRow(\MaddHatter\MarkdownTable\Builder $tableBuilder, $var, int $memory) 79 | { 80 | $tableBuilder->row([sdump($var), number_format(var_class_sizeof($var)), number_format(var_sizeof($var)), number_format($memory)]); 81 | } 82 | 83 | $memory = memory_get_usage(); 84 | $null = null; 85 | $memoryUsage = memory_get_usage() - $memory; 86 | addCommonRow($tableCommonBuilder, $null, $memoryUsage); 87 | 88 | $memory = memory_get_usage(); 89 | $bool = true; 90 | $memoryUsage = memory_get_usage() - $memory; 91 | addCommonRow($tableCommonBuilder, $bool, $memoryUsage); 92 | 93 | $memory = memory_get_usage(); 94 | $integer = 1; 95 | $memoryUsage = memory_get_usage() - $memory; 96 | addCommonRow($tableCommonBuilder, $integer, $memoryUsage); 97 | 98 | $memory = memory_get_usage(); 99 | $double = 1.5; 100 | $memoryUsage = memory_get_usage() - $memory; 101 | addCommonRow($tableCommonBuilder, $double, $memoryUsage); 102 | 103 | $memory = memory_get_usage(); 104 | $string = str_replace('{name}', 'world', 'hello {name}'); 105 | $memoryUsage = memory_get_usage() - $memory; 106 | addCommonRow($tableCommonBuilder, $string, $memoryUsage); 107 | 108 | $memory = memory_get_usage(); 109 | $resource = fopen('php://memory', 'r'); 110 | $memoryUsage = memory_get_usage() - $memory; 111 | addCommonRow($tableCommonBuilder, $resource, $memoryUsage); 112 | 113 | $memory = memory_get_usage(); 114 | $callable = function () { 115 | }; 116 | $memoryUsage = memory_get_usage() - $memory; 117 | addCommonRow($tableCommonBuilder, $callable, $memoryUsage); 118 | 119 | $memory = memory_get_usage(); 120 | $list0 = []; 121 | $memoryUsage = memory_get_usage() - $memory; 122 | addCommonRow($tableCommonBuilder, $list0, $memoryUsage); 123 | 124 | $memory = memory_get_usage(); 125 | $list100 = array_fill(0, 100, null); 126 | $memoryUsage = memory_get_usage() - $memory; 127 | addCommonRow($tableCommonBuilder, $list100, $memoryUsage); 128 | 129 | $memory = memory_get_usage(); 130 | $list1000 = array_fill(0, 1000, null); 131 | $memoryUsage = memory_get_usage() - $memory; 132 | addCommonRow($tableCommonBuilder, $list1000, $memoryUsage); 133 | 134 | $memory = memory_get_usage(); 135 | $list10000 = array_fill(0, 10000, null); 136 | $memoryUsage = memory_get_usage() - $memory; 137 | addCommonRow($tableCommonBuilder, $list10000, $memoryUsage); 138 | 139 | $memory = memory_get_usage(); 140 | $array100 = array_fill(0, 99, null); 141 | $array100['index'] = null; 142 | $memoryUsage = memory_get_usage() - $memory; 143 | addCommonRow($tableCommonBuilder, $array100, $memoryUsage); 144 | 145 | $memory = memory_get_usage(); 146 | $array1000 = array_fill(0, 999, null); 147 | $array1000['index'] = null; 148 | $memoryUsage = memory_get_usage() - $memory; 149 | addCommonRow($tableCommonBuilder, $array1000, $memoryUsage); 150 | 151 | $memory = memory_get_usage(); 152 | $array10000 = array_fill(0, 9999, null); 153 | $array10000['index'] = null; 154 | $memoryUsage = memory_get_usage() - $memory; 155 | addCommonRow($tableCommonBuilder, $array10000, $memoryUsage); 156 | 157 | $memory = memory_get_usage(); 158 | $emptyClass = new EmptyClass(); 159 | $memoryUsage = memory_get_usage() - $memory; 160 | addCommonRow($tableCommonBuilder, $emptyClass, $memoryUsage); 161 | addObjectRow($tableObjectBuilder, $emptyClass, $memoryUsage); 162 | 163 | $memory = memory_get_usage(); 164 | $classWithArray0 = new ClassWithArray([]); 165 | $memoryUsage = memory_get_usage() - $memory; 166 | addCommonRow($tableCommonBuilder, $classWithArray0, $memoryUsage); 167 | addObjectRow($tableObjectBuilder, $classWithArray0, $memoryUsage); 168 | 169 | $memory = memory_get_usage(); 170 | $classWithArray100 = new ClassWithArray(array_fill(0, 100, null)); 171 | $memoryUsage = memory_get_usage() - $memory; 172 | addCommonRow($tableCommonBuilder, $classWithArray100, $memoryUsage); 173 | addObjectRow($tableObjectBuilder, $classWithArray100, $memoryUsage); 174 | 175 | $memory = memory_get_usage(); 176 | $classWithArray1000 = new ClassWithArray(array_fill(0, 1000, null)); 177 | $memoryUsage = memory_get_usage() - $memory; 178 | addCommonRow($tableCommonBuilder, $classWithArray1000, $memoryUsage); 179 | addObjectRow($tableObjectBuilder, $classWithArray1000, $memoryUsage); 180 | 181 | $memory = memory_get_usage(); 182 | $classWithArray10000 = new ClassWithArray(array_fill(0, 10000, null)); 183 | $memoryUsage = memory_get_usage() - $memory; 184 | addCommonRow($tableCommonBuilder, $classWithArray10000, $memoryUsage); 185 | addObjectRow($tableObjectBuilder, $classWithArray10000, $memoryUsage); 186 | 187 | $memory = memory_get_usage(); 188 | $classWithObject = new ClassWithObject(); 189 | $memoryUsage = memory_get_usage() - $memory; 190 | addCommonRow($tableCommonBuilder, $classWithObject, $memoryUsage); 191 | addObjectRow($tableObjectBuilder, $classWithObject, $memoryUsage); 192 | 193 | $memory = memory_get_usage(); 194 | $listIterator = new \ArrayIterator(); 195 | for($i = 0;$i < 100; $i++) { 196 | $listIterator[] = null; 197 | } 198 | $memoryUsage = memory_get_usage() - $memory; 199 | addCommonRow($tableCommonBuilder, $listIterator, $memoryUsage); 200 | addObjectRow($tableObjectBuilder, $listIterator, $memoryUsage); 201 | 202 | $memory = memory_get_usage(); 203 | $arrayIterator = new \ArrayIterator(); 204 | for($i = 0;$i < 100; $i++) { 205 | $arrayIterator[sprintf('index%d', $i)] = null; 206 | } 207 | $memoryUsage = memory_get_usage() - $memory; 208 | addCommonRow($tableCommonBuilder, $arrayIterator, $memoryUsage); 209 | addObjectRow($tableObjectBuilder, $arrayIterator, $memoryUsage); 210 | 211 | echo sprintf("PHP %s %s(%s)\n\n", phpversion(), php_uname('s'), php_uname('m')); 212 | echo $tableCommonBuilder->render(); 213 | echo "\n"; 214 | echo $tableObjectBuilder->render(); 215 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "mrsuh/php-var-sizeof", 3 | "type": "library", 4 | "license": "MIT", 5 | "description": "Provides functions for getting full size of any PHP variable", 6 | "keywords": [ 7 | "sizeof", 8 | "var_sizeof", 9 | "var_class_sizeof", 10 | "memory_usage" 11 | ], 12 | "autoload": { 13 | "files": [ 14 | "functions/var_sizeof.php", 15 | "functions/var_class_sizeof.php" 16 | ], 17 | "psr-4": { 18 | "Mrsuh\\": "src/" 19 | } 20 | }, 21 | "authors": [ 22 | { 23 | "name": "Sukhachev Anton", 24 | "email": "mrsuh6@gmail.com" 25 | } 26 | ], 27 | "require": { 28 | "php": ">=7.4", 29 | "ext-ffi": "*" 30 | }, 31 | "require-dev": { 32 | "maddhatter/markdown-table": "^1.0", 33 | "phpunit/phpunit": "^9" 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /functions/var_class_sizeof.php: -------------------------------------------------------------------------------- 1 | value; 21 | switch (Z_TYPE_P(zv_ptr)) { 22 | case IS_UNDEF: 23 | case IS_NULL: 24 | case IS_FALSE: 25 | case IS_TRUE: 26 | case IS_LONG: 27 | case IS_DOUBLE: 28 | case IS_CONSTANT_AST: 29 | return sizeof(*zv_ptr); 30 | case IS_CALLABLE: 31 | return sizeof(*zv_ptr) + sizeof(*value.func); 32 | case IS_REFERENCE: 33 | return sizeof(*zv_ptr) + sizeof(*value.ref); 34 | case IS_RESOURCE: 35 | return sizeof(*zv_ptr) + sizeof(*value.res); 36 | case IS_STRING: 37 | return sizeof(*zv_ptr) + ZSTR_LEN(value.str); 38 | case IS_ARRAY: { 39 | HashTable *ht = value.arr; 40 | HashPosition pos; 41 | zval *data; 42 | int size = sizeof(*zv_ptr) + sizeof(*ht); 43 | 44 | for ( 45 | zend_hash_internal_pointer_reset_ex(ht, &pos); 46 | (data = zend_hash_get_current_data_ex(ht, &pos)) != NULL; 47 | zend_hash_move_forward_ex(ht, &pos) 48 | ) { 49 | size += zval_sizeof(data) - sizeof(zval); 50 | } 51 | 52 | int allocSize = 0; 53 | if (HT_IS_PACKED(ht)) { 54 | size += HT_PACKED_SIZE(ht); 55 | } else { 56 | size += HT_SIZE(ht); 57 | } 58 | 59 | return size; 60 | } 61 | 62 | case IS_OBJECT: { 63 | zend_object *object = value.obj; 64 | int size = sizeof(*zv_ptr); 65 | 66 | if(object->ce == spl_ce_ArrayIterator) { 67 | spl_array_object *spl_object = spl_array_from_obj(object); 68 | size += sizeof(*spl_object); 69 | size += zval_sizeof(&spl_object->array) - sizeof(zval); 70 | 71 | return size; 72 | } 73 | 74 | size += sizeof(*object); 75 | size += properties_sizeof(object->properties_table, object->ce->default_properties_count); 76 | 77 | return size; 78 | } 79 | } 80 | 81 | return 0; 82 | } 83 | 84 | int properties_sizeof(zval *srcProperty, int count) { 85 | int size = 0; 86 | if (count > 0) { 87 | zval *endProperty = srcProperty + count; 88 | 89 | do { 90 | size += zval_sizeof(srcProperty); 91 | srcProperty++; 92 | } while (srcProperty != endProperty); 93 | } 94 | 95 | return size; 96 | } 97 | 98 | int zval_class_sizeof(zval *zv_ptr) { 99 | 100 | if (Z_TYPE_P(zv_ptr) != IS_OBJECT) { 101 | return 0; 102 | } 103 | 104 | zend_object *object = zv_ptr->value.obj; 105 | 106 | zend_class_entry *classEntry = object->ce; 107 | 108 | int propertiesSize = properties_sizeof( 109 | classEntry->default_properties_table, 110 | classEntry->default_properties_count 111 | ); 112 | 113 | int staticMembersSize = properties_sizeof( 114 | classEntry->default_static_members_table, 115 | classEntry->default_static_members_count 116 | ); 117 | 118 | int inheritanceCacheSize = sizeof(*classEntry->inheritance_cache); 119 | 120 | int functionTableSize = HT_SIZE(&classEntry->function_table); 121 | int propertiesInfoSize = HT_SIZE(&classEntry->properties_info); 122 | int constantsTableSize = HT_SIZE(&classEntry->constants_table); 123 | 124 | return 125 | sizeof(*classEntry) + 126 | ZSTR_LEN(classEntry->name) + 127 | propertiesSize + 128 | staticMembersSize + 129 | inheritanceCacheSize + 130 | functionTableSize + 131 | propertiesInfoSize + 132 | constantsTableSize; 133 | } 134 | 135 | zval *get_zval_by_name(char *name) { 136 | 137 | HashTable *symbol_table = zend_array_dup(zend_rebuild_symbol_table()); 138 | 139 | zend_string *key_name = zend_string_init(name, strlen(name), 0); 140 | zval *data = zend_hash_find(symbol_table, key_name); 141 | 142 | zend_string_release(key_name); 143 | zend_array_destroy(symbol_table); 144 | 145 | return data; 146 | } 147 | 148 | extern "C" int var_sizeof(char *name) { 149 | 150 | zval *data = get_zval_by_name(name); 151 | if (data != NULL) { 152 | return zval_sizeof(data); 153 | } 154 | 155 | return 0; 156 | } 157 | 158 | extern "C" int var_class_sizeof(char *name) { 159 | 160 | zval *data = get_zval_by_name(name); 161 | if (data != NULL) { 162 | return zval_class_sizeof(data); 163 | } 164 | 165 | return 0; 166 | } 167 | -------------------------------------------------------------------------------- /library/ffi.h: -------------------------------------------------------------------------------- 1 | #include "zend_types.h" 2 | 3 | int zval_sizeof(zval *zv_ptr); 4 | 5 | int zval_class_sizeof(zval *zv_ptr); 6 | 7 | int properties_sizeof(zval *srcProperty, int count); 8 | 9 | zval *get_zval_by_name(char *name); 10 | -------------------------------------------------------------------------------- /library/ffi_darwin_arm64.dylib: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrsuh/php-var-sizeof/e4d88da9b1913c263941fdce8f435c870d4eb846/library/ffi_darwin_arm64.dylib -------------------------------------------------------------------------------- /library/ffi_darwin_x86_64.dylib: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrsuh/php-var-sizeof/e4d88da9b1913c263941fdce8f435c870d4eb846/library/ffi_darwin_x86_64.dylib -------------------------------------------------------------------------------- /library/ffi_linux_aarch64.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrsuh/php-var-sizeof/e4d88da9b1913c263941fdce8f435c870d4eb846/library/ffi_linux_aarch64.so -------------------------------------------------------------------------------- /library/ffi_linux_x86_64.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrsuh/php-var-sizeof/e4d88da9b1913c263941fdce8f435c870d4eb846/library/ffi_linux_x86_64.so -------------------------------------------------------------------------------- /library/spl.h: -------------------------------------------------------------------------------- 1 | #include "zend_types.h" 2 | 3 | typedef struct _spl_array_object { 4 | zval array; 5 | uint32_t ht_iter; 6 | int ar_flags; 7 | unsigned char nApplyCount; 8 | zend_function *fptr_offset_get; 9 | zend_function *fptr_offset_set; 10 | zend_function *fptr_offset_has; 11 | zend_function *fptr_offset_del; 12 | zend_function *fptr_count; 13 | zend_class_entry* ce_get_iterator; 14 | zend_object std; 15 | } spl_array_object; 16 | 17 | static inline spl_array_object *spl_array_from_obj(zend_object *obj) /* {{{ */ { 18 | return (spl_array_object*)((char*)(obj) - XtOffsetOf(spl_array_object, std)); 19 | } 20 | -------------------------------------------------------------------------------- /phpunit.xml.dist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | tests 13 | 14 | 15 | 16 | 17 | 18 | src 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /src/VarInfo.php: -------------------------------------------------------------------------------- 1 | var_sizeof("var"); 46 | } 47 | 48 | /** 49 | * @param mixed $var 50 | */ 51 | public static function varClassSizeof($var): int 52 | { 53 | self::init(); 54 | 55 | return (int)self::$libc->var_class_sizeof("var"); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /tests/VarClassSizeOfTest.php: -------------------------------------------------------------------------------- 1 | assertEquals(1360, var_class_sizeof($a)); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /tests/VarSizeOfTest.php: -------------------------------------------------------------------------------- 1 | assertEquals(16, var_sizeof($a)); 11 | } 12 | 13 | public function testSizeOfDouble(): void 14 | { 15 | $a = 1.0; 16 | $this->assertEquals(16, var_sizeof($a)); 17 | } 18 | 19 | public function testSizeOfObject(): void 20 | { 21 | $a = new \stdClass(); 22 | $this->assertEquals(72, var_sizeof($a)); 23 | } 24 | 25 | public function testSizeOfList0(): void 26 | { 27 | $a = []; 28 | $this->assertEquals(336, var_sizeof($a)); 29 | } 30 | 31 | public function testSizeOfList1000(): void 32 | { 33 | $a = array_fill(0,1000,null); 34 | $this->assertEquals(16464, var_sizeof($a)); 35 | } 36 | 37 | public function testSizeOfArray1000(): void 38 | { 39 | $a = array_fill(0,999,null); 40 | $a['index'] = null; 41 | $this->assertEquals(41032, var_sizeof($a)); 42 | } 43 | } 44 | --------------------------------------------------------------------------------