首 页   · 站长博客 · 用户注册 · 会员登陆  · 会员排行  ·最新主题  ·最近回复  精华区  版权声明  ·论坛管理
  当前登录身份:游客,请先登录。  笔名: 口令: 验证码:   
楼 主  index »  PHP发展动态/业界新闻 » [转帖]全新OO模式的PHP 5.0.0RC1正式发布linux版本  


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

  nqp@nqp.me
  4304410
  www.nqp.me

 

 发表:2004-04-07 18:45:17 阅读 3686 次 回复 0 次 得分0  |   字号 字色
[转帖]全新OO模式的PHP 5.0.0RC1正式发布linux版本
新闻来源:www.zend.com  php5信息中心

整理: www.17php.com

 PHP5 新概念简介

 Introduction
Only time will tell if the PHP 5 release will be as successful as the releases of its two predecessors (PHP 3 and PHP 4). The new features and changes aim to rid PHP of any weaknesses it may have had and make sure that it stays in the lead as the best web scripting language on the globe. 

This book covers PHP 5 and its new features in great detail. However, for those of you familiar with PHP 4, and are eager to know what is new in PHP 5, then this chapter is for you. 

The chapter will cover: 

The new language features 
News concerning PHP extensions 
Other noteworthy changes 

Language Features
New Object Oriented model
When Zeev Suraski added the object-oriented syntax back in the days of PHP 3, it was added as "syntactic sugar for accessing collections". The object-oriented model also had support for inheritance and allowed a class (and object) to aggregate both methods and properties, but not much more. When Zeev and Andi rewrote the scripting engine for PHP 4, it was a completely new engine, running much faster, much more stable and with many more features. However, the object-oriented model first introduced in PHP 3, was barely touched. 

Although the object model had serious limitations it was used extensively around the world, often in very large PHP applications. This impressive use of the OOP paradigm with PHP 4 despite its weaknesses led to it being the main focus for the PHP 5 release. 

So what were some of the limitations in PHP 3 & 4? The biggest limitation (which led to further limitations) was the fact that the copy semantics of objects were the same as for native types. So how did this actually affect the PHP developer? When you’d assign a variable (that points to an object) to another variable, a copy of the object would be created. Not only did this impact performance but it usually also lead to obscure behavior and bugs in PHP 4 applications because many developers thought that both variables would be pointing at the same object which wasn’t the case. They were pointing at separate copies of the same object, changing one would not change the other. 

For example:

class Person { 
    var $name; 
    function getName() { 
    return $this->name; 
    } 
    function setName($name) { 
    $this->name = $name; 
    } 
    function Person($name) { 
    $this->setName($name); 
    } 


function changeName($person, $name) { 
    $person->setName($name); 


$person = new Person("Andi"); 
changeName($person, "Stig"); 
print $person->getName(); 

In PHP 4, this piece of code would print out "Andi". The reason is that we pass the object $person to the changeName() function by-value, and thus, $person is copied and changeName() works on a copy of $person. 

This behavior is not very intuitive, as many developers would expect the Java-like behavior. In Java variables actually hold a handle (or pointers) to the object, and therefore, when it is copied only the handle and not the entire object is duplicated.
There were two kinds of users in PHP 4, the ones who were aware of this problem and the ones who weren’t. The latter would usually not notice this problem and their code was written in a way where it didn’t really matter if the problem existed or not. Surely some of these people had sleepless nights trying to track down weird bugs which they couldn’t pinpoint. The former group dealt with this problem by always passing and assigning objects by reference. This would prevent the engine from copying their objects but would be quite a headache as the code included numerous ‘&’ signs. 

The old object model not only led to the above-mentioned problems but also led 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 will never create behind the scene duplicates of your objects. In PHP 5, there is neither a need to pass objects by reference nor assigning them by reference. 

Note: Passing by reference and assigning by reference is still supported, in case you want to actually change a variable’s content (whether object or other type).

New Object Oriented Features
The new object oriented features are too numerous to give a detailed description in this section. The object oriented language chapter goes over each feature in detail. 

The following is a list of the main new features:

1. public/private/protected access modifiers for methods and properties
Allows the use of common OO access modifiers to control access to methods and properties.

class MyClass { 
    private $id; 

    public function getId() { 
    return $this->id; 
    } 


2. Unified constructor name __construct()
Instead of the constructor being the name of the class, it should now be declared as __construct(), making it easier to shift classes inside class hierarchies.

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


3. 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"; 
    } 

?> 

4. Interfaces
Gives the ability for a class to fulfill more than one is-a relationships. A class can inherit from one class only but may implement as many interfaces as it wants.

interface Display { 
    function display(); 


class Circle implements Display { 
    function display() { 
    print "Displaying circle"; 
    } 


5. instanceof operator
Language level support for is-a relationship checking. The PHP 4 is_a() function is now deprecated.

if ($obj instance of Circle) { 
    print '$obj is a Circle'; 


6. final methods
The final keyword allows you to mark methods so that an inheriting class can't overload them.

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


7. final classes
After declaring a class as final, it can’t be inherited. The following example would error out:

final class FinalClass { 


class BogusClass extends FinalClass { 


8. Explicit object cloning
In order to clone an object you have to use the clone keyword. You may declare a __clone() method which will be called during the clone process (after the properties have been copied from the original object).

class MyClass { 
    function __clone() { 
    print "Object is being cloned"; 
    } 

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

9. Class constants
Classes definitions can now include constant values, and are referenced using the class.

class MyClass { 
    const SUCCESS = "Success"; 
    const FAILURE = "Failure"; 

print MyClass::SUCCESS; 

10. Static members
Classes definitions can now include static members (properties), accessible via the class. Common usage of static members is in the Singleton pattern.

class Singleton { 
    static $instance = NULL; 
    function getInstance() { 
    if ($this->instance == NULL) { 
    $this->instance = new Singleton(); 
    } 
    return $this->instance; 
    } 


11. Static methods
You can now define methods as static allowing them to be called from non-object context. Static methods don’t define the $this variable as they aren’t bound to any specific object.

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

MyClass::helloWorld(); 
?> 
------------------------------
未完待续

 
  页数1/1首页 « 1 » 末页
  发表回复:您还没有登陆,无法发表回复。请先[登陆]

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