Các mảng có bất biến trong PHP không?
Vì PHP không có Generics như Java, nên không thể có các bộ sưu tập được nhập riêng. Các Bộ sưu tập có sẵn trong Thư viện này là nền tảng để bạn tạo các Bộ sưu tập được đánh máy của riêng mình, bạn chỉ cần mở rộng chúng Show DDD là tất cả về việc mã của bạn nói ngôn ngữ kinh doanh, được gọi là Ngôn ngữ phổ biến. Nếu không có Bộ sưu tập trong PHP, điều này rất khó đạt được khi sử dụng declare(strict_types=1);
use DCSG\ImmutableCollections\ImmutableCollection;
final class MyStringCollection extends ImmutableCollection {
protected function validateItems(array $elements): void
{
foreach ($elements as $element) {
if (!\is_string($element)) {
throw new InvalidArgumentException('Element is not a String.');
}
}
}
}
$collection = MyStringCollection::create(['foo', 'bar']);
echo $collection->count(); // 2
$slicedCollection = $collection->slice(0, 1); // MyStringCollection { $elements=['foo']} 2 vì bạn không thể thêm hành vi vào chúng. Vì vậy, điều thường xảy ra là bạn thêm hành vi đó vào thực thể của mình nhưng nó không nên ở đó. Một vấn đề khác là tính có thể thay đổi của declare(strict_types=1);
use DCSG\ImmutableCollections\ImmutableCollection;
final class MyStringCollection extends ImmutableCollection {
protected function validateItems(array $elements): void
{
foreach ($elements as $element) {
if (!\is_string($element)) {
throw new InvalidArgumentException('Element is not a String.');
}
}
}
}
$collection = MyStringCollection::create(['foo', 'bar']);
echo $collection->count(); // 2
$slicedCollection = $collection->slice(0, 1); // MyStringCollection { $elements=['foo']} 2, declare(strict_types=1);
use DCSG\ImmutableCollections\ImmutableCollection;
final class MyStringCollection extends ImmutableCollection {
protected function validateItems(array $elements): void
{
foreach ($elements as $element) {
if (!\is_string($element)) {
throw new InvalidArgumentException('Element is not a String.');
}
}
}
}
$collection = MyStringCollection::create(['foo', 'bar']);
echo $collection->count(); // 2
$slicedCollection = $collection->slice(0, 1); // MyStringCollection { $elements=['foo']} 4 (Đối tượng giá trị) PHẢI luôn bất biến và điều đó là không thể với declare(strict_types=1);
use DCSG\ImmutableCollections\ImmutableCollection;
final class MyStringCollection extends ImmutableCollection {
protected function validateItems(array $elements): void
{
foreach ($elements as $element) {
if (!\is_string($element)) {
throw new InvalidArgumentException('Element is not a String.');
}
}
}
}
$collection = MyStringCollection::create(['foo', 'bar']);
echo $collection->count(); // 2
$slicedCollection = $collection->slice(0, 1); // MyStringCollection { $elements=['foo']} 2 và bạn phải luôn đảm bảo rằng declare(strict_types=1);
use DCSG\ImmutableCollections\ImmutableCollection;
final class MyStringCollection extends ImmutableCollection {
protected function validateItems(array $elements): void
{
foreach ($elements as $element) {
if (!\is_string($element)) {
throw new InvalidArgumentException('Element is not a String.');
}
}
}
}
$collection = MyStringCollection::create(['foo', 'bar']);
echo $collection->count(); // 2
$slicedCollection = $collection->slice(0, 1); // MyStringCollection { $elements=['foo']} 6 của declare(strict_types=1);
use DCSG\ImmutableCollections\ImmutableCollection;
final class MyStringCollection extends ImmutableCollection {
protected function validateItems(array $elements): void
{
foreach ($elements as $element) {
if (!\is_string($element)) {
throw new InvalidArgumentException('Element is not a String.');
}
}
}
}
$collection = MyStringCollection::create(['foo', 'bar']);
echo $collection->count(); // 2
$slicedCollection = $collection->slice(0, 1); // MyStringCollection { $elements=['foo']} 7 đó đều cùng loạiBộ sưu tập vs Mảng
Bộ sưu tập PHP khácCó các Bộ sưu tập khác dành cho PHP ngoài đó, kể tên một số chúng tôi có Bộ sưu tập Doctrine và Bộ sưu tập Illuminate. Những bộ sưu tập đó giải quyết các vấn đề khác nhau, được điều chỉnh cho phù hợp với trường hợp sử dụng cụ thể của chúng và API của chúng rất phong phú và quan trọng hơn là những Bộ sưu tập đó có thể thay đổi. Các kết hợp này gây khó khăn cho việc sử dụng các Bộ sưu tập đó cho các trường hợp sử dụng đơn giản hơn. Đó là lý do tại sao Bộ sưu tập chúng tôi cung cấp ở đây, có API rất nhỏ và thậm chí không để lộ API Iterator. Bằng cách này, bạn có khả năng sử dụng chúng và mở rộng hành vi của nó phù hợp với nhu cầu của bạn Đặc trưng
sử dụng cơ bảnTạo bộ sưu tập danh sách đã nhập declare(strict_types=1);
use DCSG\ImmutableCollections\ImmutableCollection;
final class MyStringCollection extends ImmutableCollection {
protected function validateItems(array $elements): void
{
foreach ($elements as $element) {
if (!\is_string($element)) {
throw new InvalidArgumentException('Element is not a String.');
}
}
}
}
$collection = MyStringCollection::create(['foo', 'bar']);
echo $collection->count(); // 2
$slicedCollection = $collection->slice(0, 1); // MyStringCollection { $elements=['foo']} declare(strict_types=1);
use DCSG\ImmutableCollections\SetImmutableCollection;
final class MyStringSetCollection extends SetImmutableCollection {
protected function validateItems(array $elements): void
{
foreach ($elements as $element) {
if (!\is_string($element)) {
throw new InvalidArgumentException('Element is not a String.');
}
}
}
}
$collection = MyStringSetCollection::create(['foo', 'bar']);
echo $collection->count(); // 2
$slicedCollection = $collection->tail(); // MyStringSetCollection { $elements=['bar']}
$collection = MyStringSetCollection::create(['foo', 'bar', 'foo']); // Throws InvalidArgumentException ví dụChúng tôi cung cấp hai ví dụ đơn giản để hiểu rõ hơn. Một liên quan đến hóa đơn và một liên quan đến Chân tàu chở hàng Thay đổi nhật kýVui lòng xem CHANGELOG để biết thêm thông tin về những thay đổi gần đây thử nghiệm declare(strict_types=1);
use DCSG\ImmutableCollections\ImmutableCollection;
final class MyStringCollection extends ImmutableCollection {
protected function validateItems(array $elements): void
{
foreach ($elements as $element) {
if (!\is_string($element)) {
throw new InvalidArgumentException('Element is not a String.');
}
}
}
}
$collection = MyStringCollection::create(['foo', 'bar']);
echo $collection->count(); // 2
$slicedCollection = $collection->slice(0, 1); // MyStringCollection { $elements=['foo']} 5Đóng gópVui lòng xem ĐÓNG GÓP và QUY TẮC ỨNG XỬ để biết chi tiết Bảo vệNếu bạn phát hiện ra bất kỳ vấn đề nào liên quan đến bảo mật, vui lòng gửi email tới hi@dcsg. tôi thay vì sử dụng trình theo dõi vấn đề Các mảng có được truyền theo giá trị trong PHP không?Khi một mảng được truyền cho một phương thức hoặc hàm trong PHP, nó được truyền theo giá trị trừ khi bạn truyền nó một cách rõ ràng bằng tham chiếu , chẳng hạn như . kiểm tra hàm(&$array) { $array['new'] = 'hey';
Làm cách nào để chuyển mảng trong PHP?php // tạo mảng 'scores' $scores = array(9,7,112,89,633,309); . |