Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/PhpSpreadsheet/Shared/ObjectIdHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace PhpOffice\PhpSpreadsheet\Shared;

class ObjectIdHelper
{
public static function id(object $object): int
{
if (function_exists('spl_object_id')) {
return spl_object_id($object);
}

return abs(crc32(spl_object_hash($object)));
}
}

3 changes: 2 additions & 1 deletion src/PhpSpreadsheet/Style/Style.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
use PhpOffice\PhpSpreadsheet\Shared\ObjectIdHelper;

class Style extends Supervisor
{
Expand Down Expand Up @@ -398,7 +399,7 @@ public function applyFromArray(array $styleArray, bool $advancedBorders = true):
} else {
// Style cache is stored by Style::getHashCode(). But calling this method is
// expensive. So we cache the php obj id -> hash.
$objId = spl_object_id($style);
$objId = ObjectIdHelper::id($style);

// Look for the original HashCode
$styleHash = self::$cachedStyles['hashByObjId'][$objId] ?? null;
Expand Down
7 changes: 4 additions & 3 deletions src/PhpSpreadsheet/Worksheet/Worksheet.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use PhpOffice\PhpSpreadsheet\RichText\RichText;
use PhpOffice\PhpSpreadsheet\Shared;
use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
use PhpOffice\PhpSpreadsheet\Shared\ObjectIdHelper;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
use PhpOffice\PhpSpreadsheet\Style\Color;
Expand Down Expand Up @@ -355,7 +356,7 @@ public function __construct(?Spreadsheet $parent = null, string $title = 'Worksh
{
// Set parent and title
$this->parent = $parent;
$this->hash = spl_object_id($this);
$this->hash = ObjectIdHelper::id($this);
$this->setTitle($title, false);
// setTitle can change $pTitle
$this->setCodeName($this->getTitle());
Expand Down Expand Up @@ -414,7 +415,7 @@ public function __destruct()

public function __wakeup(): void
{
$this->hash = spl_object_id($this);
$this->hash = ObjectIdHelper::id($this);
}

/**
Expand Down Expand Up @@ -3759,7 +3760,7 @@ public function __clone()
}
}
}
$this->hash = spl_object_id($this);
$this->hash = ObjectIdHelper::id($this);
}

/**
Expand Down