Author - StudySection Post Views - 86 views
php collections

Collections In Php

The base interface that covers functionality in the library which is common to all the data structures is defined as Collections. Collection class is a replacement for the traditional array data structure. Collections also contain member elements just like arrays but they are considered to be objects instead of simpler types of arrays and strings.

Some Characteristics of the collections class are:

  • Wrapper around the array of objects is established by the collection class.
  • Unstable sorting algorithm: this means if the rank of the items is the same then it does not provide a guarantee that the order of those items stays the same with successive sorts of that collection.
  • With the help of the collection class, we can easily update or add the existing and new elements respectively.
  • It follows the lazy initialization techniques in which objects are only created only on demand. So it saves system resources.

Problems with an array:

  • As we know array does not allow to add or remove elements after its creation.
  • Elements that we have to store in an array should be known in advance.

Example of an array:

<?php
class User
{
public $data = array();
// ...
}
$user = new User(abcd);
foreach ($user->data as $data1) {
echo $data1->name;
}

The above example has the following problems:

  • An array is defined as a public member variable that breaks encapsulation
  • Due to ambiguity in the indexing, we don’t know how to traverse the array to find the particular item.
  • It increases the unnecessary load on the database as if we only want to print certain information like the name of the user it fetches all the data related to that user.

By creating a collection class These problems with arrays can be solved.

Example:

<?php
class Collection
{
private $users = array();
public function addUser($obj, $key = null) {
}
public function deleteUser($key) {
}
public function getUser($key) {
}
}

In the $users array we can store the objects.

addUser(): we can add new members to the collection or we can say new objects can be created with this method.

$key parameter can be used to define the specified location in which that particular object is stored and if null is passed to the $key then it will choose the next free location to add that object.
If we try to add an object by using a $key that is already in use then it will throw an exception to prevent overwriting of existing information.

Example:
public function addUser($obj, $key = null) {
if ($key == null) {
$this->users[] = $obj;
}
else {
if (isset($this->users[$key])) {
throw new KeyHasUseException("Key $key already in use.");
}
else {
$this->users[$key] = $obj;
}
}
}

deleteUser(): it removes the object
getUser(): retrieves the object.
By using the $key as a parameter we can specify the particular object that we want to delete or retrieve. But in case of invalid key or we can say if that key does not exist then an exception will be thrown.
Example:

public function deleteUser($key) {
if (isset($this->users[$key])) {
unset($this- >users[$key]);
}
else {
throw new KeyInvalidException("Invalid key $key.");
}
}
public function getUser($key) {
if (isset($this->users[$key])) {
return $this->users[$key];
}
else {
throw new KeyInvalidException("Invalid key $key.");
}
}

The English language is the most widely used language as a medium of communication around the world. Having a certification for the English language can be an advantage. StudySection provides an English Certification Exam that tests English language proficiency in English grammar, reading, and writing.

Leave a Reply

Your email address will not be published. Required fields are marked *