PHP Tutorial
|
Object-Oriented PHP
Although PHP does not require the use of objects (as, for example, Java does), PHP does support object-oriented programming. Object-oriented PHP became available back in version 3 of the language, was greatly improved upon in version 4, and evolved even further in PHP 5. Programmers accustomed to a full-fledged OOP language will appreciate being able to apply those skills to their PHP applications (although they may also be annoyed at some of PHP's OOP omissions) and those new to the concept can benefit from the accessibility of the language to start learning object-oriented PHP.
Object-oriented programming starts with defining a class, which is a template for a thing. In pedestrian terms, a class states what information can be stored and what actions can be taken. The "information" elements are variables and the "actions" are functions. Inside of a class these are called attributes and methods, respectively (both are considered to be members of the class). Once a class has been defined, instances of the class can be made, each instance being an object variable. This is the basic premise behind object-oriented PHP and OOP in general. The figure at right shows the creation of an object based upon a HelloWorld class, followed by PhpED's code completion box, listing the available methods in that class.
The beauty and true power of object-oriented PHP comes from its more advanced subjects. The first, and most important, of these are inheritance and access control. Master these two concepts and you're well on your way to implementing object-oriented programming in your PHP applications.
PHP Error and Exception Handling
As with any programming language, knowing how to display, comprehend, and handle the errors that occur--and they will occur, no matter how experienced you are--is critical to any real success. There are four subjects related to PHP error and exception handling:
- Configuring how PHP treats errors
- Handling errors in your PHP scripts
- Handling exceptions using object-oriented programming
- Debugging code
Configuring PHP's treatment of errors comes down to two issues. One is what kinds of problems should be reported by PHP. PHP ranks the types of errors and you can use this scale to declare what level of errors should be considered noteworthy. The other configuration issue is whether errors are displayed by PHP or not. Understand that showing errors, while necessary for debugging, is a security concern. Also know that having PHP not display an error is not the same thing as not having an error. This is where error handling comes into play. Error handling is simply a matter of overwriting how PHP handles any errors that occur. There are any number of improvements you can make as to how errors are handled, and the professional Web site does just that.
Object-oriented PHP offers an alternative method of error handling, called exception handling. It starts with PHP 5's built-in Exception class. This class can be used on its own, or extended using the OOP concept of inheritance. PHP 5 also offers the OOP try...catch construct, which drastically changes the way you program a script.
Debugging is perhaps the most vital of any skill to master. Different scripts will imply different debugging techniques (for example, debugging a database script requires approaches different than those for debugging a Web services script), but some techniques are universal:
- Paying attention to PHP errors
- Knowing the values of variables (see the figure)
- Tracking where, in the script, problems occur (inside of functions, conditionals, loops, etc.)
- Checking the HTML source of a page
|
Article continues:
<<previous page
next page>>
|
|
Download NuSphere PHP IDE
Download a free trial of the fast PHP EDitor and robust Integrated Development Environment for PHP.
|
Buy NuSphere PhpED® now
|
Special Team4 Offer
Get 4 copies of PhpED for the price of 3!
Optimum solution for development teams.
|
|
Need more than 4 licenses? Contact Us for more quantity discounts, please use "Ordering/Payment issue" subject on the form.
|
|
"To be honest its bloody awesome, I have looked at loads of PHP editors and this is THE only one that actual works straight out of the box!!! Brilliant, well done."
Andrew Breward, Director of Technology caboodal.com
|
|