[Zend PHP5 Cerification] Lectures -- 8. OOP

Prior to PHP5 OOPwas a hack on top of the array implementation. PHP5 Changedeverything, in a great way.


Objects are treateddifferently from other types of variables. An object is always passedby reference (in reality, it is passed by handle, but for allpractical purposes there is no difference), rather than byvalue.

Parent classes can be accessed using the specialparent:: namespace

$this.

One of the drawbacks of Theconstructor name is same with the name of the class. If you decidedto rename your class, you would also have to rename your constructor.To avoid these problems, PHP 5 now uses the magic __construct()method as the constructor for any class regardless of the class’name.
Note that, if the __construct() method is not found, PHPwill look for the old PHP 4-style constructor and call thatinstead.

In addition to the __construct() method, we also havea __destruct() method.

Destructionoccurs when all references to an object are gone, and this may notnecessarily take place when you expect it—or even when you want itto. In fact, while you can unset() a variable that references anobject, or overwrite it with another value, the object itself may notbe destroyed right away because a reference to it is heldelsewhere.

Even if an objectstill has one or more active references, the __destruct() method iscalled at the end of script execution—therefore, you are guaranteedthat, at some point, your destructor will be executed. However,there is no way to determine the order in which any two objects inyour scripts will be destroyed. Thiscan sometimes cause problems when an object depends on another toperform one or more functions—for example, if one of your classesencapsulates a database connection and another class needs thatconnection to flush it data to the database, you should not rely onyour destructors to perform a transparent flush to the database whenthe object is deleted: the instance of the first class that providesdatabase connectivity could, in fact, be destroyed before the second,thus making it impossible for the latter to save its data to thedatabase.

.public  Method or property can be accessedexternally.
.protected  Method or property is private to thatclass and can not be accessed externally.
.private  Method orproperty is private and can't also be accessed by extendingclasses.
.final  The final visibility level only applies tomethods and classes. Classes that are declared as final cannot beextended.

Typically, you will want to make all API methods andproperties public, since you will want them to be accessible fromoutside of your objects, while you will want to keep those used forinternal operation as helpers to the API calls protected orprivate.

Constructors and Destructors—along with all othermagic methods will normally bedeclared as public; there are, however, times when you wish to makethe constructor private( e.g. certain design patterns like Singletonor Factory.).

Other Magic methods (SpecialFunctions):
 __construct()
 __destruct()
 __toString()
 __sleep()
 __wakeup()
 __call()
 __get()
 __set()

 

Notethat, like a normal variable, a class property can be initializedwhile it is being declared. However, the initialization is limited toassigning values (but not by evaluating expressions). You can’t,for example, initialize a variable by calling a function—that’ssomething you can only do within one of the class’ methods(typically, the constructor).

Constants, Static
Unlikeregular methods and properties, their static counterparts exist andare accessible as part of a class itself, as opposed to existing onlywithin the scope of one of its instances. This allows you to treatclasses as true containers of interrelated functions and dataelements—which, in turn, is a very handy expedient to avoid namingconflicts.
You should keep in mind that PHP 5 is very strict aboutthe use of static properties and methods. For example, calling staticproperties using object notation will result in a notice:

classfoo {
static $bar = "bat";
static public functionbaz()
{
echo "Hello World";
}
}
$foo = newfoo();
$foo->baz();
echo $foo->bar;

It isnecessary for the static definition to follow the visibilitydefinition; if no visibility definition is declared, the staticmethod or property is considered public.

Class constants arepublic, and accessible from all scopes.


Note that classconstants suffer from the same limitations as regularconstants—therefore, they can only contain scalar values.
Classconstants have several advantages over traditional constants:
.sincethey are encapsulated in a class, they make for much cleanercode.
.they are significantly faster than those declared with thedefine() construct.

Interfaces& Abstract Classes
.Newfeature added to PHP 5
.Be used to create a series of constraintson the base design of a group of classes.

An abstract classessentially defines the basic skeleton of a specific type ofencapsulated entity.
You must declare a class as abstract so longas it has (or inherits without providing a body) at least oneabstract method.


Interfaces are used to specify an APIthat a class must implement. This allows you to create a common“contract” that your classes must implement in order to satisfycertain logical requirements.

If you fail to define all of themethods for a particular interface, or all of the arguments for anygiven interface method, you will see something ugly.

It ispossible to implement more than one interface in the sameclass.

Additionally, a class can extend another class, as wellas implement multiple interfaces at the same time.

Remember—aclass can only extend one parent class, but it can implement multipleinterfaces.


The
instanceofoperator

instanceofallows you to inspect all of the ancestor classes of your object, aswell as any interfaces.

 

Exceptions
Exceptionsprovide an error control mechanism that is more fine-grained thantraditional PHP fault handling, and that allows for a much greaterdegree of control.

.Unified method of error handling
.Makesproper error handling possible
.Can be accomplished with try catchblocks or by setting a unified error handler once
.Try catchblocks take precedence if the error raising code is withinthem.

 

.Exceptions are objects, created (or “thrown”)when an error occurs.
.Exceptions can be handled at differentpoints in a script’s execution, and different types of exceptionscan be handled by separate portions of a script’s code.
.Allunhandled exceptions are fatal.
.Exceptions can be thrown from the__construct method on failure.
.Exceptions change the flow of theapplication.


Exception base class is built into PHPitself.

class Exception {
// The error message associatedwith this exception
protected $message = ’Unknown Exception’;
//The error code associated with this exception
protected $code =0;
// The pathname of the file where the exceptionoccurred
protected $file;
// The line of the file where theexception occurred
protected $line;
// Constructor
function__construct ($message = null, $code = 0);
// Returns themessage
final function getMessage();
// Returns the errorcode
final function getCode();
// Returns the file name
finalfunction getFile();
// Returns the file line
final functiongetLine();
// Returns an execution backtrace as an array
finalfunction getTrace();
// Returns a backtrace as a string
finalfunction getTraceAsString();
// Returns a string representation ofthe exception
function __toString();
}


Almost all ofthe properties of an Exception are automatically filled in for you bythe interpreter—generally speaking, you only need to provide amessage and a code, and all the remaining information will be takencare of for you.

The throwconstruct
Although it is common practice to do so, you don’tneed to create the Exception object directly in the throwexpression.

Exceptions then “bubble up” until they areeither handled by the script or cause a fatal exception. The handlingof exceptions is performed using a try...catchblock.

try {
if ($error) {
throw new Exception ("Thisis my error");
}
} catch (Exception $e) {
// Handleexception
}


Note how the catch() portion of thestatement requires us
to hint thetype of Exception that we want tocatch; one of the best features ofexceptions is the fact that you can decide which kind of exception totrap.

Since you are free toextend the base Exception class, this means that different nestedor Chainedtry..catch blocks can be used to trap and deal with different typesof errors.


Once an exception has been caught, execution ofthe script will follow from directly after the last catch block.Toavoid fatal errors from uncaught exceptions, you could wrap yourentire application in a try... catch block—which would, however, berather inconvenient. Luckily, there is a better solution—PHP allowsus to define a “catch-all” function that is automatically calledwhenever an exception is not handled. This function is set up bycalling
set_exception_handler().

functionhandleUncaughtException($e)
{echo$e->getMessage();
}
set_exception_handler("handleUncaughtException");
thrownew Exception("You caught me!");
echo "This isnever displayed";


Note that, because the catch-allexception handler is only called after the exception has bubbled upthrough the entire script, it, just like an all-encompassing try...catch block, is the end of the line for your code—in other words,the exception has already caused a fatal error, and you are justgiven the opportunity to handle it, but not to recover from it. Forexample, the code above will never output You caught me!, because theexception thrown will bubble up and cause handleUncaughtException()to be executed; the script will then terminate.

If you wish torestore the previously used exception handler, be it the default of afatal error or another user defined callback, you can userestore_exception_handler().

LazyLoading
Prior to PHP 5,instantiating an undefined class, or using one of its methods in astatic way would cause a fatal error. This meant that you needed toinclude all of the class files that you might need, rather thanloading them as they were needed—just so that you wouldn’t forgetone—or come up with complicated file inclusion mechanisms to reducethe needless processing of external files.To solve this problem, PHP5 features an “autoload” facility that makes it possible toimplement “lazy loading”, or loading of classes on-demand. Whenreferencing a non-existent class, be it as a type hint, static call,or attempt at instantiating an object, PHP will try to call the
__autoload()global function so that the script may be given an opportunity toload it. If, after the call to autoload(), the class is still notdefined, the interpreter gives up and throws a fatal error.

function__autoload($class)
{
// Require PEAR-compatibleclasses
require_once str_replace("_", "/",$class);
}
$obj = new Some_Class();

When instantiatingSome_Class, __autoload() is called and passed “Some_Class.php” asits argument. The function then replaces the underscores with forwardslashes, and includes the file using require_once(). Using__autoload() is of great help when you are working with only onenaming scheme; it allows lazy-loading of classes, so that classesthat are never used are also never loaded. However, once you startmixing code and using different libraries (e.g.: PEAR and some legacyapplication) you will rapidly run into cases that __autoload() cannothandle without becoming too bulky and slow. The StandardPHP Library (SPL), luckily, offers asimpler solution to this problem by allowing you to stack autoloaderson top of each other.If one fails to load a class, the next one inthe chain is called, until either the class has been loaded, or nomore autoloaders are part of the chain (in which case, a fatal erroroccurs).

The default  spl_autoload()
thisbuilt-infunction checks all include paths for filenames that matchthe name of the class that needs loading in lowercase letters,followed by .inc, .php, or the extensions specified using acomma-separated string as the only parameter to a call to
spl_autoload_extensions().

Additionalautoloaders can be added to the stack by callingspl_autoload_register().
Thefirst call to this function replaces the __autoload() call in theengine with its own implementation—this means that, if you alreadyhave a user-defined __autoload() you will need to register it withSPL in order for it to continueworking.

spl_autoload_register(’spl_autoload’);
if(function_exists(’__autoload’)){
spl_autoload_register(’__autoload’);
}


Typehinting
Classcan serve as typehinting.

Reflection
Thereflection API provides a manner to obtain detailed information aboutcode.

 With PHP’s new objectmodel comes the Reflection API a collection of functions and objectsthat allows you to examine the contents of a script’s code, such asfunctions and objects, at runtime.

Reflection can be veryhandy in a number of circumstances; for example, it can be used togenerate simple documentation, or for determining whether certainfunctionality is available to a script, and soon.

get_defined_functions()

ReflectionFunction
ReflectionClass
ReflectionMethod

 http://ca3.php.net/reflection 

 OOPConclusions
.Slowerthan procedural code, but allows complex tasks to be understood morereadily
.Dataand related methods are grouped into one cohesive structure
.Multipleinstances of the same object can be created
.Objects are now dealtwith by reference rather than by value, objects must be explicitlycloned to be copied

posted @ 2010-06-29 17:28  DavidHHuan  阅读(390)  评论(0编辑  收藏  举报