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

Bộ sưu tập bất biến đã nhập cho DDD (Thiết kế hướng tên miền)

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ại

Bộ sưu tập vs Mả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']}
    0 và
     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 đều đại diện cho một Nhóm phần tử
  •  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']}
    0 không được PHP hỗ trợ nguyên bản trong khi
     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 thì
  •  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 là cấu trúc dữ liệu của PHP và được sử dụng cho hầu hết mọi thứ
  •  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 không cho phép bạn thêm hành vi mới trong khi
     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']}
    0 cho phé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']}
    2 không cho phép bạn xác định loại cho các thành phần của nó trong khi
     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']}
    0 cho phép

Bộ sưu tập PHP khác

Có 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

  • Xây dựng tĩnh để sử dụng đường ống
  • Sử dụng các phương pháp như
     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']}
    9,
     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
    0,
     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
    1,
     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
    2,
     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
    3,
     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
    4,
     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
    5,
     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
    6,
     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
    7,
     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
    8,
     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
    9,
     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']}
    50,
     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']}
    51,
     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']}
    52,
     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']}
    53

sử dụng cơ bản

Tạ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']}

Tạo bộ sưu tập đã nhập

 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óp

Vui 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); .