Yearly Archives: 2011

PHP – Exceptions in PHP5

PHP5 supports Exception model, like C++/C# and Java.

syntax to catch exception:


try {
 //code which can launch an exception
 // [...]
 // manual exception throw
 throw new Exception(“thrown exception”);
 //not executed ! (throw command is caught below
 print “not printed!”;
 }
 catch(Exception $e) { print $e->getMessage();}

this code prints : “thrown exception”.

<!--more-->
 It’s possibile to throw an exception inside a catch construct. The new exception will be caught in the external try-catch block.

try {
 try{
 throw new Exception(“internal exception”);
 }
 catch (Exception $e) { throw new Exception($e->getMessage()); }
 print “not printed!”;
 }
 catch(Exception $e) { print $e->getMessage(); }

This code prints: “internal exception”

Structure of “Exception” built-in class and inheritance


class Exception
 {
 protected $message = ‘Unknown exception’; // exception message
 protected $code = 0; // user defined exception code
 protected $file; // source filename of exception
 protected $line; // source line of exception

function __construct($message = null, $code = 0);

final function getMessage(); // message of exception
 final function getCode(); // code of exception
 final function getFile(); // source filename
 final function getLine(); // source line
 final function getTrace(); // an array of the backtrace()
 final function getTraceAsString(); // formatted string of trace

/* Overrideable */
 function __toString(); // formatted string for display
 }

In case of extension, remember to call the superclass constructor in the extended class constructor
parent::__construct()

Multiple catch constructs
It’s allowed to specify more than one catch after a try construct.
If the exception doesnt’ match with the argument, it will be caught to the next catch block.
Note: an Exception object match also with the objects of its subclasses. Mind the order !


class MyException extends Exception {/* …*/ }

try{
 throw new MyException(“”);
 }
 catch (MyException $e) {// print “MyException”;
 }
 catch (Exception $e) { // never executed
 print “Exception”;
 }

And …

try{
 throw new MyException(“”);
 }
 catch (Exception2 $e) { print “Exception”; }
 catch (Exception $e) { // print “Exception”;
 }
 catch (MyException $e) { print “MyException”; }

Htaccess – Deploy Custom Error Pages

Replicate the following patterns to serve your own set of custom error pages.

Simply replace the “/errors/###.html” with the correct path and file name. Also change the “###” preceding the path to summon pages for other errors. Note: your custom error pages must be larger than 512 bytes in size or they will be completely ignored by Internet Explorer:


# serve custom error pages

ErrorDocument 400 /errors/400.html

ErrorDocument 401 /errors/401.html

ErrorDocument 403 /errors/403.html

ErrorDocument 404 /errors/404.html

ErrorDocument 500 /errors/500.html

WordPress – Adding Comment Numbers To Your Theme

I’ve noticed a lot of themes that don’t come with numbers on the blog’s comments.

This might not be such a bad thing if you don’t get very many comments, but if your blog does get comments it is probably a good idea to show off how many comments you get by numbering them.

Here are the steps you can take to easily add numbers to your WordPress theme’s comments section.

First thing you will want to do is create a backup your comments.php file.
Locate the comments.php file.
Locate the code that starts the comment loop. It will look something like this:

<?php if ( $comments ) : ?>

Place this code immediately above the code in Step 3:

<?php $i = 0; ?>

Now locate the code that looks like this:

<?php foreach ($comments as $comment) : ?>

Placed this code immediately below the code in Step 5:

<?php $i++; ?>

Now use this code where you want to display your comment numbers:

<span>
<?php echo $i; ?>
</span>

Click Save.

Now go to your stylesheet (style.css) and place this code anywhere on the stylesheet (probably best placed in the comments section):

.count {
float:right;
padding: 10px;
font-size:18px;
color:#000000;
}

You can adjust the stylesheet to fit your comment numbers into the placement and appearance that you want them to have.