首 页   · 站长博客 · 用户注册 · 会员登陆  · 会员排行  ·最新主题  ·最近回复  精华区  版权声明  ·论坛管理
  当前登录身份:游客,请先登录。  笔名: 口令: 验证码:   
楼 主  index »  PHP安装/语法/函数 » [转帖]php5全新概念接触(纯英文资料)  


  作者:一起PHP
  注册时间:2004-02-27
  主题/回复:247/1521
  积分:4649
  等级:★★★★★☆(十一级)
  称号:论坛圣人

  nqp@nqp.me
  4304410
  www.nqp.me

 

 发表:2004-04-07 18:47:30 阅读 1957 次 回复 0 次 得分0  |   字号 字色
[转帖]php5全新概念接触(纯英文资料)
资料来源: www.zend.com  php5信息中心
整理: www.17php.com

接上篇
-----------------------------------
12. abstract classes
A class may be declared as abstract so as 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"; 
    } 


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

abstract class MyBaseClass { 
    abstract function display(); 


14. Class type hints
Function declarations may include class type hints for their parameters. If the functions are called with an incorrect class type an error occurs.

function expectsMyClass(MyClass $obj) { 



15. Support for dereferencing objects which are returned from methods.
In PHP 4, you could not directly dereference objects which are returned from methods. You would have to first assign the object to a dummy variable and then dereference it. 
PHP 4:

$dummy = $obj->method(); 
$dummy->method2(); 

PHP 5:

$obj->method()->method2(); 

16. Iterators
PHP 5 allows both PHP classes and PHP extension classes to implement an Iterator interface. Once you implement this interface you will be able to iterate instances of the class by using the foreach() language construct.

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


17. __autoload()
Many developers writing object-oriented applications create one PHP source file per-class definition. One of the biggest annoyances is having to write a long list of needed includes at the beginning of each script (one for each class). In PHP 5, this is no longer necessary. You may define an __autoload() function which is automatically called in case you are trying to use a class which hasn’t been defined yet. By calling this function the scripting engine is giving a last chance to load the class before PHP bails out with an error.

function __autoload($class_name) { 
    include_once($class_name . "php"); 


$obj  = new MyClass1(); 
$obj2 = new MyClass2(); 

Other New Language Features
1. Exception handling
PHP 5 adds the ability for the well known try/throw/catch structured exception handling paradigm. You are only allowed to throw objects which 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 backwards compatibility purposes most internal functions do not throw exceptions. However, new extensions are making use of this capability and you can use it in your own source code. Also, similar to the already existing set_error_handler() you may use set_exception_handler() to catch an unhandled exception before the script terminates.

2. foreach with references
In PHP 4, you could not iterate through an array and modify its values. PHP 5 supports this by allowing you to mark the foreach() loop with the ‘&’ (reference) sign, thus, making any values you change affect the array you’re iterating over.

foreach ($array as &$value) { 
    if ($value === "NULL") { 
    $value = NULL; 
    } 


3. default values for by-reference parameters
In PHP 4, default values could only be given to parameters which are passed by-value. Giving default values to by-reference parameters is now supported.

function my_func(&$arg = null) { 
    if ($arg === NULL) { 
    print '$arg is empty'; 
    } 

my_func(); 

General PHP changes
XML and Web Services
Following the changes in the language, the XML updates in PHP 5 are most probably 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.

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 weren’t always supported, performance wasn’t as good as it could have been, and interoperability between the varies XML extensions did not exist. 

In PHP 5, all XML extensions have been rewritten to use the superb libxml2 XML toolkit (http://www.xmlsoft.org/). It is a very feature rich, highly maintained and efficient implementation of the XML standards bringing the cutting edge of XML technology to PHP.
All the above mentioned extensions (SAX, DOM and XSLT) now use libxml2 including the new additional extensions SimpleXML and SOAP.

SAX
As mentioned, the new SAX implementation has switched from using Expat to libxml2. Although the new extension should be compatible there may be some small subtle differences. Developers who still want to work with the Expat library can do so by configuring and building PHP accordingly (not recommended).

DOM
Although DOM support in PHP 4 was also based on the libxml2 library, it was quite buggy, had memory leaks and the API in many cases was not W3C compliant. The DOM extension went through a thorough facelift for PHP 5. Not only was the extension mostly rewritten it is now also W3C complaint. For example, function names now use studlyCaps as described by the W3C standard making it easier for you to read general W3C documentation and implementing what you learnt, right away in PHP. In addition, the DOM extension now supports three kinds of schemas for XML validation, DTD, XML Schema and RelaxNG. 

As a result of these changes PHP 4 code using DOM will not always run in PHP 5. However, in most cases adjusting the function names to the new standard will probably do the trick.

XSLT
In PHP 4, there were two extensions that supported XSL Transformations. The first was using the Sablotron extension and the second was using the XSLT support in the DOM extension. In PHP 5, a new XSL extension was written and, as mentioned, is based on the libxml2 extension. As in PHP 5, the XSL Transformation does not take the XSLT stylesheet as a parameter but depends on the DOM extension to load it, the stylesheet can be cached in memory and may be applied to many documents saving execution time

SimpleXML
Probably when looking back in a year or two it will be clear that SimpleXML has revolutionized the way PHP developers work with XML files. SimpleXML could really be called "XML for Dummies". Instead of having to deal with DOM or even worse SAX, SimpleXML represents your XML file as a native PHP object. You can read, write or iterate over 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> 
</client> 
</clients> 

The following piece of 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 "; 


It’s obvious how simple SimpleXML really is. 

And in case there is something advanced you need to do to your SimpleXML object which isn’t supported in this lightweight extension, you can convert it to a DOM tree by calling dom_import_simplexml(), manipulate it in DOM and covert it back to SimpleXML using simplexml_import_dom(). Thanks to both extensions using the same underlying XML library switching between these two has been made a reality.

SOAP
Official native SOAP support in PHP 4 was lacking. The most commonly used SOAP implementation was PEAR’s but as it was implemented entirely in PHP it could not perform as well as a built-in C extension. Other available C extensions never reached stability and wide adoption and, therefore, were not included in the main PHP 5 distribution. 

SOAP support in PHP 5 was completely rewritten as a C extension and, although it was only completed at a very late stage in the beta process, it was incooperated into the default distribution due to its thorough implementation of most of the SOAP standard. 

The following calls SomeFunction() defined in a WSDL file:

$client = new SoapClient("some.wsdl"); 
$client->SomeFunction($a, $b, $c); 

New MySQLi (MySQL Improved) extension
For PHP 5, MySQL AB (http://www.mysql.com) has written a new MySQL extension that allows you to take full advantage of the new functionality in MySQL 4.1 and later. As opposed to the old MySQL extension, the new one gives you both a functional and an object oriented interface so that you can choose what you prefer. New features supported by this extension include prepared statements and variable binding, SSL and compressed connections, transaction control, replication support and more...

SQLite extension
Support for SQLite (http://www.sqlite.org) was first introduced in the PHP 4.3.x series. It is an embedded SQL library which does not require an SQL server and is very suitable for applications which don’t require the scalability of SQL servers or if you’re deploying at an ISP who doesn’t give you access to an SQL server. Contrary to what its name implies SQLite is very feature rich and supports transactions, sub-selects, views and large DB files. It is mentioned here as a PHP 5 feature because it was introduced so late in the PHP 4 series and as it takes advantage of PHP 5 by providing an object oriented interface and supporting iterators.

Tidy extension
PHP 5 includes support for the useful Tidy (http://tidy.sf.net/) library. It allows PHP developers to parse, diagnose, clean and repair HTML documents. The Tidy extension supports both a functional and an object oriented interface, and it’s API uses the PHP 5 exception mechanism.

Perl extension
Although not bundled in the default PHP 5 package, the Perl extension allows you to call Perl scripts, use Perl objects and use other Perl functionality natively from within PHP. This new extension sits within the PECL (PHP Extension Community Library) repository a http://pecl.php.net/package/perl.

Other New Things in PHP 5:
New memory manager
The Zend Engine features a new memory manager. The two main advantages are better support for multi-threaded environments (allocations don’t need to do any mutual exclusion locks) and after each request freeing the allocated memory blocks is much more efficient. As this is an underlying infra-structure change you will not notice it directly as the end-user.

Dropped support for Windows 95
Running PHP on the Windows 95 platform is not supported anymore due to it not supporting functionality which PHP uses. As Microsoft has officially stopped supporting it over a year ago the PHP development community decided that this is a wise decision.

Summary
You must surely be impressed by the amount of improvements in PHP 5. As mentioned earlier, this chapter doesn’t cover all of the improvements but only the main ones. Other improvements include additional features, a lot of bug fixes and very much improved infrastructure. The following chapters will cover PHP 5 and will give you in-depth coverage of the mentioned new features and others which were omitted.
--------------------------------------------------
全篇完
 
  页数1/1首页 « 1 » 末页
  发表回复:您还没有登陆,无法发表回复。请先[登陆]

一起PHP技术联盟 主办:一起PHP 联系方式:站长QQ:4304410 QQ群:8423742 20159565 站长博客 E-mail: nqp@nqp.me 执行时间:0.005sec
SimsmaBBS 2008 (v6.0) Developed by 17php.com,Copyright(C)2003-2010 All rights reserved. 副本授权:一起PHP官方专用版