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 . "
";
}



Friday 15 September 2017

Web sockets online Tutorial - Page2

Web sockets online Tutorial - Page2

Question: Difference between socket and websocket?
WebSockets run from browsers and connecting to Server over a similar to HTTP. it require a permanent connection to its server.
Sockets are more powerful and generic and not restricted to http protocol OR browser.



Question: How to detect the message type in onmessage event?
  
socket.onmessage = function(event){
   if(typeOf event.data === String ) {
      console.log("Received data string");
   }
}



Question: How does Web Socket support Single TCP Connection?
In Web a new TCP connection is initiated for a each HTTP request and terminated after the response is received which means A new TCP connection need for each HTTP request. Whereas In WebSocket, the HTTP connection is upgraded and client and server communicate over that same TCP connection.


Question: How Web Socket support on Bi-directional?
In Web client(like browser) only can send the request to server then server send the response to the same client only.
Whereas In WebSocket, there is no pattern like request/response and Both (Client/Server) can send message to other party.


Question: How Web Socket support on Full Duplex?
In Web client(like browser) only can send the request to server then server send the response to the same client only. At a given time, either client is talking to server or server is talking to client. Whereas In WebSocket they can talk each other.


Question: What is the WebSocket API?
The WebSocket API is the asynchronous communication from client to server and it takes place over single TCP socket using the ws (unsecure) or wss (secure) protocol and can be used by any client or server application.


Question: How can we secure WebSocket API?
Normal Web Socket API start with ws but secure API start with wss.

Question: What is Ajax Long-Polling?
  1. A client requests a webpage using HTTP.
  2. The server does not immediately respond with the requested information but waits until there's new information available.
  3. When there's new information available, the server responds with the new information.
  4. The client receives the new information and immediately sends another request to the server, re-starting the process.



Question: What is Comet in HTML5?
Comet is a collection of techniques prior to HTML5 which use streaming and long-polling to achieve real time applications.


Question: What is Server Sent Events?
Server Sent Events (SSE) connections can only push data to the browser.
online stock quotes, or twitters updating timeline or feed are good examples of an application that could benefit from SSE.


Question: How to add http headers in Websockets client API?
We can not add custom header in Web sockets.


Question: How can we pass auth while calling Connecting WebSocket?
 var ws = new WebSocket("ws://username:password@example.com/path");



Question: Is PHP support for Web Sockets available?
Yes, for this you need install the library.
Apache Module: https://github.com/disconnect/apache-websocket
PHP WebSocket: http://code.google.com/p/phpwebsocket/
Ratchet: https://github.com/cboden/Ratchet
Wrench: https://github.com/varspool/Wrench