That's why you are getting Child as
your output.
Exception Handling
One of the most improved features in PHP5 is that you can now use exceptions, like
other OOP languages out there. PHP5 introduces these exception objects to simplify
your error management.
Let's see how these exceptions occur and how to handle them. Take a look at the
following class, which simply connects to a PostgreSQL server. In the case of failing
to connect to the server, let's see what it usually returns:
//class.db.php
class db
{
function connect()
{
Chapter 3
[ 49 ]
pg_connect("somehost","username","password");
}
}
$db = new db();
$db->connect();
?>
The output is the following.
Warning: pg_connect() [
function.pg-connect]: Unable to connect to PostgreSQL
server: could not translate host name "somehost" to address:
Unknown host in
C:\OOP with PHP5\Codes\ch3\exception1.phpon line
6How are you going to handle it in PHP4? Generally, by using something similar to
the following shown below:
//class.db.php
error_reporting(E_ALL - E_WARNING);
class db
{
function connect()
{
if (!pg_connect("somehost","username","password")) return false;
}
}
$db = new db();
if (!$db->connect()) echo "Falied to connect to PostgreSQL Server";
?>
Now let's see how we can solve it with exception.
Pages:
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71