Tài liệu PHP5 Power Programming P2 - Pdf 86


1.2 Language Features 3

The old object model not only led to the afore-mentioned problems, but
also to fundamental problems that prevented implementing some additional
features on top of the existing object model.
In PHP 5, the infrastructure of the object model was rewritten to work
with object handles. Unless you explicitly clone an object by using the

clone

keyword, you never create behind-the-scenes duplicates of your objects. In
PHP 5, you don’t need a need to pass objects by reference or assign them by
reference.

Note:

Passing by reference and assigning by reference are still sup-
ported, in case you want to actually change a variable’s content (whether
object or other type).

1.2.2 New Object-Oriented Features

The new OO features are too numerous to give a detailed description in this
section. Chapter 3, “PHP 5 OO Language,” details each feature.
The following list provides the main new features:



public


__construct()

, which makes it easier to shift classes inside class hier-
archies:

class MyClass {
function __construct() {
print "Inside constructor";
}
}



Object destructor support by defining a

__destructor()
method.
Allows defining a destructor function that runs when an object
is destroyed:

class MyClass {
function __destruct() {
print ”Destroying object”;
}
}

Gutmans_Ch01 Page 3 Thursday, September 23, 2004 2:35 PM

is now deprecated:

if ($obj instanceof Circle) {
print '$obj is a Circle';
}



Final methods.

The

final

keyword allows you to mark methods so that an inheriting class cannot overload
them:

class MyClass {
final function getBaseClassName() {
return __CLASS__;
}
}



Final classes.
After declaring a class as

final


}
$obj = new MyClass();
$obj_copy = clone $obj;



Class constants.
Class definitions can now include constant values and are referenced
using the class:

class MyClass {
const SUCCESS = "Success";
const FAILURE = "Failure";
}
print MyClass::SUCCESS;



Static methods.
You can now define methods as static by allowing them to be called from
non-object context. Static methods do not define the

$this

variable
because they are not bound to any specific object:

class MyClass {
static function helloWorld() {
print "Hello, world";

6 What Is New in PHP 5? Chap. 1



Abstract classes.
A class may be declared

abstract

to prevent it from being instantiated.
However, you may inherit from an abstract class:

abstract class MyBaseClass {
function display() {
print "Default display routine being called";
}
}



Abstract methods.
A method may be declared

abstract

, thereby deferring its definition to an
inheriting class. A class that includes abstract methods must be declared

abstract




Iterators.
PHP 5 allows both PHP classes and PHP extension classes to implement
an

Iterator

interface. After you implement this interface, you can iterate
instances of the class by using the

foreach()

language
construct:

$obj = new MyIteratorImplementation();
foreach ($obj as $value) {
print "$value";
}

Gutmans_Ch01 Page 6 Thursday, September 23, 2004 2:35 PM

1.2 Language Features 7

For a more complete example, see Chapter 4, “PHP 5 Advanced OOP and
Design Patterns.”




exception-handling paradigm. You are only allowed to throw objects that
inherit from the

Exception

class:

class SQLException extends Exception {
public $problem;
function __construct($problem) {
$this->problem = $problem;
}
}
try {
...
throw new SQLException("Couldn't connect to database");
...
} catch (SQLException $e) {
print "Caught an SQLException with problem $obj->problem";
} catch (Exception $e) {
print "Caught unrecognized exception";
}

Currently for backward-compatibility purposes, most internal functions
do not throw exceptions. However, new extensions make use of this capability,
and you can use it in your own source code. Also, similar to the already exist-
ing

set_error_handler()


print '$arg is empty';
}
}
my_func();
1.3 G
ENERAL
PHP C
HANGES
1.3.1 XML and Web Services
Following the changes in the language, the XML updates in PHP 5 are proba-
bly the most significant and exciting. The enhanced XML functionality in PHP
5 puts it on par with other web technologies in some areas and overtakes them
in others.
1.3.1.1 The Foundation XML support in PHP 4 was implemented using a
variety of underlying XML libraries. SAX support was implemented using the
old Expat library, XSLT was implemented using the Sablotron library (or using
libxml2 via the DOM extension), and DOM was implemented using the more
powerful libxml2 library by the GNOME project.
Using a variety of libraries did not make PHP 4 excel when it came to
XML support. Maintenance was poor, new XML standards were not always
supported, performance was not as good as it could have been, and interopera-
bility between the various XML extensions did not exist.
In PHP 5, all XML extensions have been rewritten to use the superb
libxml2 XML toolkit ( It is a feature-rich, highly main-
tained, and efficient implementation of the XML standards that brings cutting-
edge XML technology to PHP.
Gutmans_Ch01 Page 8 Thursday, September 23, 2004 2:35 PM
1.3 General PHP Changes 9
All the afore-mentioned extensions (SAX, DOM, and XSLT) now use
libxml2, including the new additional extensions SimpleXML and SOAP.

your XML file with ease, accessing elements and attributes.
Consider the following XML file:
<clients>
<client>
<name>John Doe</name>
<account_number>87234838</account_number>
</client>
<client>
<name>Janet Smith</name>
<account_number>72384329</account_number>
Gutmans_Ch01 Page 9 Thursday, September 23, 2004 2:35 PM
10 What Is New in PHP 5? Chap. 1
</client>
</clients>
The following code prints each client’s name and account number:
$clients = simplexml_load_file('clients.xml');
foreach ($clients->client as $client) {
print "$client->name has account number $client

>account_number\n";
}
It is obvious how simple SimpleXML really is.
In case you need to implement an advanced technique in your Sim-
pleXML object that is not supported in this lightweight extension, you can
convert it to a DOM tree by calling it
dom_import_simplexml()
, manipulate it in
DOM, and convert it to SimpleXML using
simplexml_import_dom()
.


Nhờ tải bản gốc
Music ♫

Copyright: Tài liệu đại học © DMCA.com Protection Status