Tuesday 19 September 2017

Standard PHP Library (SPL) with Basic details

Standard PHP Library (SPL) with Basic details

Question: What is full form of SPL?
Standard PHP Library.


Question: What is SPL?
It is collection of interfaces and Classes.


Question: Do we need to download library for SPL?
No, We need not to download external library.


Question: What does provide SPL?
SPL provides following Iterators, Interface and Functions.
  1. Set of standard Data Structure.
  2. a Iterators to traverse over objects
  3. Set of Interfaces
  4. Set of standard Exceptions
  5. SPL Functions
  6. SPL File Handling



Question: What classes are available in SPL data structure?
Doubly Linked Lists: It is a list of nodes linked in both directions to each other. Iterator operations, access to both end node, addition or removal of nodes.
http://php.net/manual/en/class.spldoublylinkedlist.php

Heaps: Heaps are tree-like structures that follow the General heap, each node is greater than or equal to its children.
http://php.net/manual/en/class.splheap.php

Arrays: Arrays are structures that store the data in a continuous way, accessible via indexes.
http://php.net/manual/en/class.splfixedarray.php

Map: A map is a datastructure holding key-value pairs. PHP arrays can be seen as maps from integers/strings to values.
http://php.net/manual/en/class.splobjectstorage.php



Question: What Iterators classes are available in SPL?
  1. AppendIterator
  2. ArrayIterator
  3. CachingIterator
  4. CallbackFilterIterator
  5. DirectoryIterator
  6. EmptyIterator
  7. FilesystemIterator
  8. FilterIterator
  9. GlobIterator
  10. InfiniteIterator
  11. IteratorIterator
  12. LimitIterator
  13. MultipleIterator
  14. NoRewindIterator
  15. ParentIterator
  16. RecursiveArrayIterator
  17. RecursiveCachingIterator
  18. RecursiveCallbackFilterIterator
  19. RecursiveDirectoryIterator
  20. RecursiveFilterIterator
  21. RecursiveIteratorIterator
  22. RecursiveRegexIterator
  23. RecursiveTreeIterator
  24. RegexIterator



Question: What Interfaces are available in SPL?
  1. Countable
  2. OuterIterator
  3. RecursiveIterator
  4. SeekableIterator
  5. SplObserver
  6. SplSubject



Question: What Exception classes are available in SPL?
  1. BadFunctionCallException
  2. BadMethodCallException
  3. DomainException
  4. InvalidArgumentException
  5. LengthException
  6. LogicException
  7. OutOfBoundsException
  8. OutOfRangeException
  9. OverflowException
  10. RangeException
  11. RuntimeException
  12. UnderflowException
  13. UnexpectedValueException



Question: What are the advantage of iterators?
  1. Laziness: the data is only fetched when you actually need it.
  2. Reduced memory usage: SPL iterators encapsulate the list and expose visibility to one element at a time making them far more efficient.



Question: Give an example of Iterating Arrayst?
  
$arr = array("One", "Two", "three", "Four", "Five", "Six");
$iter = new ArrayIterator($arr);
foreach ($iter as $key => $value) {
    echo $key . ":  " . $value ;
}

Output
  
0: One
1: Two
2: three
3: Four
4: Five
5: Six



Question: Give an example of Iterating Directory Listings?
  
$dir = new DirectoryIterator("/");
foreach ($dir as $item) {
    echo $item . "
";
}

Output
  
$RECYCLE.BIN
Backup
banner
blog backup
data
data.log.txt
desktop files
doremon
mm
mongodb



Question: Give an example of Iterating Directory Recursively?
  
$iter = new RecursiveDirectoryIterator("/");
foreach (new RecursiveIteratorIterator($iter) as $item) {
    echo $item . "
";
}