Answer the question
In order to leave comments, you need to log in
How to find out the size (number of bytes occupied) of an object in PHP?
Hello.
C++ has a function sizeof(); to determine the size of an object.
Is there anything similar in PHP for determining the size of an object?
Answer the question
In order to leave comments, you need to log in
Is there anything similar in PHP for determining the size of an object?
class Foo {
private $bar;
public function __construct(string $bar) {
$this->bar = $bar;
}
}
$start = memory_get_usage();
$a = new Foo('test1');
$middle = memory_get_usage();
$b = new Foo('longer value');
$end = memory_get_usage();
echo $middle - $start, PHP_EOL; // 56
echo $end - $middle, PHP_EOL; // 56
$before=0;
$a='испытуемая переменная либо массив или объект';
$before = memory_get_usage();
unset($a);
echo 'размер переменной составил: ',$before-memory_get_usage(),' байт';
class MyBigClass
{
var $allocatedSize;
var $allMyOtherStuff;
}
function AllocateMyBigClass()
{
$before = memory_get_usage();
$ret = new MyBigClass;
$after = memory_get_usage();
$ret->allocatedSize = ($after - $before);
return $ret;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question