Online
 
Friday, 09 January 2009
 
 
More article:
Related Content:

Your Ad Here

Tutorial PHP Exceptions
 
Article Index
Tutorial PHP Exceptions
Page 2
Page 3
Page 4
Page 5

Exceptions are an entirely new concept to PHP 5 and represent the object-oriented approach to triggering a nonfatal error in PHP scripts. In PHP 4, the only way to trigger an error was to use the trigger_error() function and then process that error within a custom error handler set by set_error_handler(). Exceptions, on the other hand, enable you to cause errors as well as deal with errors in a much more reasonable fashion.

Understanding the Call Stack

To understand how exceptions work, first you must understand the concept of a call stack. A call stack is, in essence, a record of the order by which functions and methods have been called within your script. Consider the following script example:

<?php
function firstFunction() {
secondFunction();
}
function secondFunction() {
thirdFunction();
}
function thirdFunction() {
echo "Hello, world!";
}
firstFunction();
?>

When this script executes, it is clear that firstFunction() will call secondFunction(), which will in turn call thirdFunction(). What is the call stack when we are within the thirdFunction() call? It would look something like the following:

thirdFunction();
secondFunction();
firstFunction();

Looking at this, it is clear that a call stack is nothing more than a record of the order that the functions were called. The first function pushed onto the call stack is firstFunction() because it was the first function called and, as expected, the current function, thirdFunction(), is at the top of the stack. As these function calls return, they are then popped off the stack until we are back in the global scope.

The concept of the call stack is an important part of exception handling, as you will see shortly.


Tags: Add more tags...,
This entry was posted on . You can follow any responses to this entry through the RSS 2.0 feed. You can leave a comment. Tags: Simple PHP, Pear, Easy PHP, PHP Tutorial, PHP MySQL, XSLT, Sap Tutorial, CSS Tutorial, XSL FO Java, SQL Tutorial.
Users' Comments (0)

Comment an article
  Name
  E-mail
   Title
Available characters: 4000
 Notify me of follow-up comments
This image contains a scrambled text, it is using a combination of colors, font size, background, angle in order to disallow computer to automate reading. You will have to reproduce it to post on my homepage
Enter what you see:

No comment posted

Mobile Wallpaper 148
Statistic


Last Post

 
Top! Top!