亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

Home php教程 php手冊 PHP 5/Zend Engine 2.0的改進

PHP 5/Zend Engine 2.0的改進

Jun 21, 2016 am 09:15 AM
class function gt print

近兩年沒有使用PHP寫過程序了,今天要用PHP,就在網(wǎng)上查了查,看到了PHP5,一時之間興趣又大了起來,于是翻譯了這篇文章。
文章來源于http://www.php.net/。
新的對象模型
PHP中的對象處理部分已完全重寫,具有更佳的性能和更多的功能。在先前的PHP版本中,對象被當做原始的簡單類型
(如integer和string)來處理,這種方法的缺點是當變量被賦值或作為參數(shù)傳遞時,得到的是對象拷貝。而在新版本中,
對象是通過句柄來引用的,而不是通過對象的值(句柄想象為對象的標識符)。
很多PHP程序員可能未意識到老的對象模型的“copying quirks“,因此以前的大多數(shù)PHP程序?qū)⒉恍枰鋈魏胃?
即可運行,或只做很少的改動。

私有和保護成員
PHP 5引進了私有和保護成員變量,它們可以定義可視化的類屬性。
示例
保護成員變量能在該類的子類中被訪問,而私有成員變量只能在所屬類中被訪問。
Hello; print "MyClass::printHello() " . $this->Bar; print "MyClass::printHello() " . $this->Foo; }}class MyClass2 extends MyClass { protected $Foo; function printHello() { MyClass::printHello(); /* Should print */ print "MyClass2::printHello() " . $this->Hello; /* Shouldn't print out anything */ print "MyClass2::printHello() " . $this->Bar; /* Shouldn't print (not declared)*/ print "MyClass2::printHello() " . $this->Foo; /* Should print */ }}$obj = new MyClass();print $obj->Hello; /* 不輸出任何內(nèi)容,以下類同 */print $obj->Bar; /* Shouldn't print out anything */print $obj->Foo; /* Shouldn't print out anything */$obj->printHello(); /* Should print */$obj = new MyClass2();print $obj->Hello; /* Shouldn't print out anything */print $obj->Bar; /* Shouldn't print out anything */print $obj->Foo; /* Shouldn't print out anything */$obj->printHello();?>

私有和保護方法

PHP 5(ZEND引擎2)中,私有方法和保護方法也被引入。
示例:
aPrivateMethod(); }}class Bar extends Foo { public function aPublicMethod() { echo "Bar::aPublicMethod() called.\n"; $this->aProtectedMethod(); }}$o = new Bar;$o->aPublicMethod();?>
以前代碼中的用戶自定義類或方法中雖然沒有定義"public," "protected" 或 "private"等關(guān)鍵字,但無需修改即可運行。

抽象類和方法
PHP 5還引入了抽象類和方法。抽象方法只聲明方法的”符號”,而不提供它的實現(xiàn)。一個包含抽象方法的類需要聲明為”abstract”。
例如:
test();?>
抽象類不能實例化。
舊的代碼中的用戶自定義類或方法中雖未定義"abstract”關(guān)鍵字,但無需修改就可以運行。
接口(Interfaces)
ZEND引擎2.0引入了接口。一個類可以實現(xiàn)任意的接口列表。
例如:

舊的代碼中的用戶定義類或方法中雖然沒有定義"interface”關(guān)鍵字,但無需修改就可以正常運行。

類類型提示(Class Type Hints)
在保留類無需定義類型的同時,PHP 5引入了類類型提示來聲明,以期望把對象的類通過參數(shù)傳遞給一個方法。
例如:
a($b);$a->b($b);?>
這些類類型提示不是象一些需要類型定義的語言那樣在編譯中進行檢查,而是在運行時進行檢查。這就意味著:

is equivalent to:

這種語法只用于對象或類,不適用于內(nèi)建(built-in)類型。

Final關(guān)鍵字(final)
PHP 5引入了“final”關(guān)鍵字以定義在子類中不能被覆蓋的成員或方法。
例:
class Foo { final function bar() { // ... }}?>
以前所寫代碼中的用戶自定義類或方法中雖未定義"final"關(guān)鍵字,但無需修改就可以運行了。
對象克隆(Object Cloning)
PHP 4在對象被復(fù)制時,用戶不能判斷運行那個拷貝構(gòu)造函數(shù)。在復(fù)制時,PHP 4根據(jù)對象的屬性
一位一位地復(fù)制一個同樣的復(fù)制品。
每次都要建立一個完全一樣的復(fù)制品并不總是我們想要的。一個很好的復(fù)制構(gòu)造例子是,當有
一個代表一個GTK窗口的對象,它擁有該窗口的所有資源,當你建立一個拷貝時,你可能需要一
個新的窗口,它擁有原窗口的所有屬性,但需要擁有新窗口的資源。另外一個例子是你有一個
對象引用了另外一個對象,當你復(fù)制父對象時,你希望建立那個引用對象的新實例,以使復(fù)制品有一個單獨的拷貝。
對一個對象的拷貝通過調(diào)用對象的__clone()方法完成:
$copy_of_object = $object->__clone();
?>

當開發(fā)者請求建立一個對象的新的拷貝時,ZEND引擎會檢查是否已經(jīng)定義了__clone()方法。如果未定義
的話,它會調(diào)用一個默認的__clone()方法來復(fù)制該對象的所有屬性。如果定義了該方法,該方法會負責(zé)
在拷貝中設(shè)置必要的屬性。為使用方便,引擎會提供一個函數(shù)從源對象中導(dǎo)入所有的屬性,這樣它就可
以先得到一個具有值的源對象拷貝,然后只需要對需要改變的屬性進行覆蓋即可。
例:
class MyCloneable {
static $id = 0;

function MyCloneable() {
$this->id = self::$id++;
}

function __clone() {
$this->name = $that->name;
$this->address = "New York";
$this->id = self::$id++;
}
}

$obj = new MyCloneable();

$obj->name = "Hello";
$obj->address = "Tel-Aviv";

print $obj->id . "\n";

$obj = $obj->__clone();

print $obj->id . "\n";
print $obj->name . "\n";
print $obj->address . "\n";
?>

統(tǒng)一的構(gòu)造方法
ZEND引擎允許開發(fā)者定義類的構(gòu)造方法。具有構(gòu)造方法的類在新建時會首先調(diào)用構(gòu)造方法,構(gòu)造
方法適用于在正式使用該類前進行的初始化。
在PHP4中,構(gòu)造方法的名稱與類名相同。由于在派生類中調(diào)用父類的作法比較普遍,因此導(dǎo)致在
PHP4中當類在一個大型的類繼承中進行移動時,處理方式有點笨拙。當一個派生類被移動到一個不同
的父類中時,父類的構(gòu)造方法名必然是不同的,這樣的話派生類中的有關(guān)調(diào)用父類構(gòu)造方法的語句需要改寫。
PHP5引入了一個定義構(gòu)造方法的標準方式,通過調(diào)用它們的__construct()來定義。
示例:
class BaseClass {
function __construct() {
print "In BaseClass constructor\n";
}
}

class SubClass extends BaseClass {
function __construct() {
parent::__construct();
print "In SubClass constructor\n";
}
}

$obj = new BaseClass();
$obj = new SubClass();
?>

為向后兼容,當PHP5類不能找到__construct()方法時,會通過老的方法也就是類名
來查找構(gòu)造方法。這意味著唯一可能產(chǎn)生兼容性問題的是在以前的代碼中已經(jīng)使用了
一個名為__construct()的方法名。

析構(gòu)方法
定義析構(gòu)方法是十分有用的。析構(gòu)方法可以記錄調(diào)試信息,關(guān)閉數(shù)據(jù)庫連接,還有做其它的掃尾
工作。PHP4中并無此機制,盡管PHP已支持注冊在請求結(jié)束時需要運行的函數(shù)。
PHP5引入了與其它面向?qū)ο笳Z言如Java語言相似的析構(gòu)方法:當最后一個該對象的引用被清除時,
系統(tǒng)將會在該對象從內(nèi)存中釋放前調(diào)用名為__destruct()的析構(gòu)方法。
示例:
class MyDestructableClass {
function __construct() {
print "In constructor\n";
$this->name = "MyDestructableClass";
}

function __destruct() {
print "Destroying " . $this->name . "\n";
}
}

$obj = new MyDestructableClass();
?>

和構(gòu)造方法相似,引擎將不調(diào)用父類的析構(gòu)方法,為調(diào)用該方法,你需要在子
類的析構(gòu)方法中通過parent::__destruct()語句進行調(diào)用。

常量
PHP 5 引入了類常量(per-class constants)定義:
class Foo {
const constant = "constant";
}

echo "Foo::constant = " . Foo::constant . "\n";
?>

PHP5允許常量中包含表達式,但在編譯時常量中的表達式將被計算,
因此常量不能在運行中改變它的值。
class Bar {
const a = 1const b = 1const c = a | b;
}
?>
以前代碼中的用戶自定義類或方法中雖然未定義"const”關(guān)鍵字,
但無需修改就可以運行。

異常(Exceptions)
PHP4中沒異常處理,PHP5引入了與其它與語言相似的異常處理模型。
class MyExceptionFoo extends Exception {
function __construct($exception) {
parent::__construct($exception);
}
}

try {
throw new MyExceptionFoo("Hello");
} catch (MyExceptionFoo $exception) {
print $exception->getMessage();
}
?>

以前代碼中的用戶自定義類或方法中雖未定義'catch', 'throw' 和 'try'關(guān)鍵字,但無需修改
就可以運行。

函數(shù)返回對象值
在PHP4中,函數(shù)不可能返回對象的值并對返回的對象進行方法調(diào)用,隨著Zend Engine 2
(ZEND引擎2)的出現(xiàn),以下調(diào)用成為可能:
class Circle {
function draw() {
print "Circle\n";
}
}

class Square {
function draw() {
print "Square\n";
}
}

function ShapeFactoryMethod($shape) {
switch ($shape) {
case "Circle":
return new Circle();
case "Square":
return new Square();
}
}

ShapeFactoryMethod("Circle")->draw();
ShapeFactoryMethod("Square")->draw();
?>

靜態(tài)類中的靜態(tài)成員變量可初始化
例如:
class foo {
static $my_static = 5;
}

print foo::$my_static;
?>



靜態(tài)方法(Static Methods)
PHP5引入了關(guān)鍵字'static'來定義一個靜態(tài)方法,這樣可以從對象外進行調(diào)用。
例如:

class Foo {
public static function aStaticMethod() {
// ...
}
}

Foo::aStaticMethod();
?>

虛擬變量$this在被定義為靜態(tài)(static)的方法中無效。


Instanceof

PHP5引入了 “instanceof“關(guān)鍵字來確定一個對象是否是某一個對象的實例,或某一個對象的派生,或使用了某一個接口。

示例:

class baseClass { }

$a = new baseClass;

if ($a instanceof basicClass) {
echo "Hello World";
}
?>





靜態(tài)函數(shù)變量(Static function variables)
所有的靜態(tài)變量現(xiàn)在在編譯時進行處理,這允許開發(fā)者通過引用來指定靜態(tài)變量。這個變化提高了效率但意味著不可能對靜態(tài)變量進行間接引用。


函數(shù)中通過引用方式傳遞的參數(shù)允許有默認值
例如:

function my_function(&$var = null) {
if ($var === null) {
die("$var needs to have a value");
}
}
?>



__autoload()



在初始化一個未定義的類時,__autoload()攔截函數(shù)(interceptor function)將被自動調(diào)
用。類名將作為__autoload()攔截函數(shù)唯一參數(shù)傳遞給它。
例如:
function __autoload($className) {
include_once $className . ".php";
}

$object = new ClassName;
?>



方法和屬性調(diào)用的重載
所有方法調(diào)用和屬性訪問都可以通用 __call(), __get() 和 __set()方法來重載。

例: __get() 和 __set()
class Setter {
public $n;
public $x = array("a" => 1, "b" => 2, "c" => 3);

function __get($nm) {
print "Getting [$nm]\n";

if (isset($this->x[$nm])) {
$r = $this->x[$nm];
print "Returning: $r\n";
return $r;
} else {
print "Nothing!\n";
}
}

function __set($nm, $val) {
print "Setting [$nm] to $val\n";

if (isset($this->x[$nm])) {
$this->x[$nm] = $val;
print "OK!\n";
} else {
print "Not OK!\n";
}
}
}

$foo = new Setter();
$foo->n = 1;
$foo->a = 100;
$foo->a++;
$foo->z++;
var_dump($foo);
?>





示例: __call()

class Caller {
var $x = array(1, 2, 3);

function __call($m, $a) {
print "Method $m called:\n";
var_dump($a);
return $this->x;
}
}

$foo = new Caller();
$a = $foo->test(1, "2", 3.4, true);
var_dump($a);
?>



Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1488
72
What are the differences between Huawei GT3 Pro and GT4? What are the differences between Huawei GT3 Pro and GT4? Dec 29, 2023 pm 02:27 PM

Many users will choose the Huawei brand when choosing smart watches. Among them, Huawei GT3pro and GT4 are very popular choices. Many users are curious about the difference between Huawei GT3pro and GT4. Let’s introduce the two to you. . What are the differences between Huawei GT3pro and GT4? 1. Appearance GT4: 46mm and 41mm, the material is glass mirror + stainless steel body + high-resolution fiber back shell. GT3pro: 46.6mm and 42.9mm, the material is sapphire glass + titanium body/ceramic body + ceramic back shell 2. Healthy GT4: Using the latest Huawei Truseen5.5+ algorithm, the results will be more accurate. GT3pro: Added ECG electrocardiogram and blood vessel and safety

How to use classes and methods in Python How to use classes and methods in Python Apr 21, 2023 pm 02:28 PM

Concepts and instances of classes and methods Class (Class): used to describe a collection of objects with the same properties and methods. It defines the properties and methods common to every object in the collection. Objects are instances of classes. Method: Function defined in the class. Class construction method __init__(): The class has a special method (construction method) named init(), which is automatically called when the class is instantiated. Instance variables: In the declaration of a class, attributes are represented by variables. Such variables are called instance variables. An instance variable is a variable modified with self. Instantiation: Create an instance of a class, a specific object of the class. Inheritance: that is, a derived class (derivedclass) inherits the base class (baseclass)

Fix: Snipping tool not working in Windows 11 Fix: Snipping tool not working in Windows 11 Aug 24, 2023 am 09:48 AM

Why Snipping Tool Not Working on Windows 11 Understanding the root cause of the problem can help find the right solution. Here are the top reasons why the Snipping Tool might not be working properly: Focus Assistant is On: This prevents the Snipping Tool from opening. Corrupted application: If the snipping tool crashes on launch, it might be corrupted. Outdated graphics drivers: Incompatible drivers may interfere with the snipping tool. Interference from other applications: Other running applications may conflict with the Snipping Tool. Certificate has expired: An error during the upgrade process may cause this issu simple solution. These are suitable for most users and do not require any special technical knowledge. 1. Update Windows and Microsoft Store apps

What does function mean? What does function mean? Aug 04, 2023 am 10:33 AM

Function means function. It is a reusable code block with specific functions. It is one of the basic components of a program. It can accept input parameters, perform specific operations, and return results. Its purpose is to encapsulate a reusable block of code. code to improve code reusability and maintainability.

What does class mean in python? What does class mean in python? May 21, 2019 pm 05:10 PM

Class is a keyword in Python, used to define a class. The method of defining a class: add a space after class and then add the class name; class name rules: capitalize the first letter. If there are multiple words, use camel case naming, such as [class Dog()].

Replace the class name of an element using jQuery Replace the class name of an element using jQuery Feb 24, 2024 pm 11:03 PM

jQuery is a classic JavaScript library that is widely used in web development. It simplifies operations such as handling events, manipulating DOM elements, and performing animations on web pages. When using jQuery, you often encounter situations where you need to replace the class name of an element. This article will introduce some practical methods and specific code examples. 1. Use the removeClass() and addClass() methods jQuery provides the removeClass() method for deletion

Vue error: Unable to use v-bind to bind class and style correctly, how to solve it? Vue error: Unable to use v-bind to bind class and style correctly, how to solve it? Aug 26, 2023 pm 10:58 PM

Vue error: Unable to use v-bind to bind class and style correctly, how to solve it? In Vue development, we often use the v-bind instruction to dynamically bind class and style, but sometimes we may encounter some problems, such as being unable to correctly use v-bind to bind class and style. In this article, I will explain the cause of this problem and provide you with a solution. First, let’s understand the v-bind directive. v-bind is used to bind V

How to solve the '[Vue warn]: v-bind:class/ :class' error How to solve the '[Vue warn]: v-bind:class/ :class' error Aug 26, 2023 am 08:17 AM

How to solve the "[Vuewarn]:v-bind:class/:class" error During the development process of using Vue, we often encounter some error prompts. One of the common errors is "[Vuewarn]:v-bind:class" /:class" error. This error message usually appears when we use v-bind:class or :class attribute, indicating that Vue cannot correctly parse the class value we set. Then, if

See all articles