How to catch fatal Errors in PHP

In this tutorial we will see how to catch the fatal errors in PHP.Normally we skip the fatal errors like calling a function which doesn't exist in file.


The following error types cannot be handled with a user defined function: E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR, E_COMPILE_WARNING, and most of E_STRICT raised in the file where set_error_handler() is called.

We can now use it with register_shutdown_function to handle fatal error, log, redirect, html page, instead of the infamous white page.We will use register_shutdown_function function to catch the fatal errors.

Note :  register_shutdown_function will gets executed after your script execution ends either it is successful or ends with any error.

So before performing any action in this function we need to check if really any fatal occurs or not. If occurs then we will show error page else it will continue to run as it is.




<?php
register_shutdown_function( "handle_fatal_error" );

function handle_fatal_error() {
  $errfile = "unknown file";
  $errstr  = "shutdown";
  $errno   = E_CORE_ERROR;
  $errline = 0;

  $error = error_get_last();

  if( $error !== NULL) {
    $errno   = $error["type"];
    $errfile = $error["file"];
    $errline = $error["line"];
    $errstr  = $error["message"];
 echo "file-".$error["file"]. " type ".$error["type"]. " line ".$error["line"]." message ".$error["message"];die;
        
  }
}

//This function is undefined
echo myfunction();die;


It will handle the fatal error and you can pass the error in mail also.

Build web components using polymer.js

Polymer takes the Web Component standard and makes it available for the latest browsers. This allows you to write custom tags in a standard manner. Polymer.js is a library for creating Web Components which are a set of W3C standards and upcoming browser APIs for defining your own custom HTML elements.With the help of polyfills and sugar, it can create these custom elements and bring Web Component support to browsers that don’t play nice with the standard just yet.The Polymer library to make the most of Web Components, a powerful new platform feature for extending HTML and componentizing your apps.

Polymer.js


What is web components ?
Web Components are a collection of standards which are working their way through the W3C and landing in browsers as we speak. In a nutshell, they allow us to bundle markup and styles into custom HTML elements.What's truly amazing about these new elements is that they fully encapsulate all of their HTML and CSS.That means the styles that you write always render as you intended, and your HTML is safe from the prying eyes of external JavaScript.

The easiest way to understand how web components allow for custom HTML elements is to first look at an existing element we already know from HTML5.the <video> tag. Using this element you can place a video with just a few of lines of code, for example:


<!DOCTYPE html>
<html>
<body>

<video width="320" height="240">
  <source src="movie.mp4" type="video/mp4">
  <source src="movie.ogg" type="video/ogg">
  Your browser does not support the video tag.
</video>

</body>
</html>

You may only seen the 5 lines of clean code but have you ever think how it will work of just writing a small piece of code.By default the browser will hide the verbose code.But imagine if you could use this same approach and create you own web component like .

<google-map lat="33.790" long="233.390"></google-map>

Instead of using a google libary for map and including external css and js of google map just create a simple web component of writing one line of code and automatically include the google map in your page. it's really amazing.Instead of using a google libary for map and including external css and js of google map just create a simple web component of writing one line of code and automatically include the google map in your page. it's really amazing.

Browser Support:

Chrome Android
Chrome
Canary
Firefox
IE 10+
Safari 6+
Mobile Safari

You can use the polymer using it's offical documentation.It's quite easy and simple to learn.Polymer.js

What is stateless protocol in HTTP

Stateless protocol means each command is executed independently, without any knowledge of the commands that came before it.That is the protocol cant remember what u did in your last web session.But this is over come using cookies,which maintain states of the users to remember their previous activity.It is the protocol used to convey information of World Wide Web (WWW).HTTP protocol is a stateless and connectionless protocol. HTTP is called as a stateless protocol because each command is request is executed independently, without any knowledge of the requests that were executed before it. It is the protocol used for the web. It is based on a request/request paradigm. In this protocol the communication generally takes place over a TCP/IP protocol.

Stateless protocol in HTTP


A stateless protocol does not require the server to retain information or status about each user for the duration of multiple requests.


HTTP Request Methods:

GET Method: The Get method is used to getting the data from the server. Get method appends the parameters passed as query string to a URL, in the form of key- value pairs. for example, if a parameter is name = Williams, then this string will be appended in the URL. By default the method is Get.

POST Method: The post method is used for sending data to the server. In post method the query string is appended along the request object, they do not get appended in the URL, so parameters transfer in hidden form.

HEAD Method: When a user wants to know about the headers, like MIME types, charset, Content- Length then we use Head method. With this no body content is returned.

TRACE Method: Trace on the jsp resource returns the content of the resource. Asks for a loopback of the request message, so that the use can see what is being recieved on the other side.

DELETE Method: It is used for delete the resources, files at the requested URL

OPTIONS Method: It lists the Http methods to which the thing at the requested URL can respond.

PUT Method: It put the enclosed information at the requestedURL.

CONNECT Method: It connects for the purpose of tunneling.

Download the eBook of react js for beginners

ReactJS basically is an open-source JavaScript library which is used for building user interfaces specifically for single page applications...

Popular