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

Home php教程 php手冊(cè) PHP5試用(一)

PHP5試用(一)

Jun 21, 2016 am 09:14 AM
function gt php5 private

php5

雖然 PHP5 還沒有正式發(fā)布(開發(fā)版本已經(jīng)提供下載),但我們現(xiàn)在就可以開始體驗(yàn)一下新的版本 將要帶給我們的驚喜。在以下的介紹中,我們將重點(diǎn)講述 PHP5 中的三大特色功能。這三大特點(diǎn)為:

* 新的對(duì)象模式 (New Object Mode)
* 異常處理 (Exceptions)
* 名稱空間 (Namespace)

在開始之前,要聲明兩點(diǎn):

* 文章中的例子為了說(shuō)明如何操作,有些部分使用了 PHP4 的表現(xiàn)手段,這僅僅是為了提高文章的可讀性。
* 文章中描述的部分與 PHP5 的最終發(fā)布版可能會(huì)有一些出入

在 PHP5 沒有最終正式發(fā)布前,你可以隨時(shí)從 http://snaps.php.net 下載到最新的編譯版本來(lái)親自體驗(yàn)一下 PHP5 所帶給我們這些嶄新的功能。


新的對(duì)象模式

PHP5 中的對(duì)象已經(jīng)進(jìn)行了較系統(tǒng)、較全面的調(diào)整,現(xiàn)在的樣子可能看起來(lái)會(huì)有些類似于 Java。本小節(jié)著重講述 PHP5 中新的對(duì)象模式,并舉了一些較簡(jiǎn)易的例子來(lái)說(shuō)明。就讓本節(jié)成為你的 PHP5 之旅的一個(gè)新起點(diǎn)吧。:)

* 構(gòu)造函數(shù)和析構(gòu)函數(shù)
* 對(duì)象的引用
* 對(duì)象的克隆
* 對(duì)象中的私有、公共及受保護(hù)模式
* 接口 (Interfaces)
* 抽象類
* __call
* __set 和 __get
* 靜態(tài)成員


構(gòu)造函數(shù)和析構(gòu)函數(shù)

在 PHP4 中,當(dāng)函數(shù)與對(duì)象同名時(shí),這個(gè)函數(shù)將成為該對(duì)象的構(gòu)造函數(shù),并且在 PHP4 中沒有析構(gòu)函數(shù)的概念。
在 PHP5 中,構(gòu)造函數(shù)被統(tǒng)一命名為 __construct,并且引入了析構(gòu)函數(shù)的概念,被統(tǒng)一命名為 __destruct。

例一:構(gòu)造函數(shù)和析構(gòu)函數(shù)

class foo {
var $x;
function __construct($x) {
$this->x = $x;
}
function display() {
print($this->x);
}
function __destruct() {
print("bye bye");
}
}
$o1 = new foo(4);
$o1->display();
?>

在上面的例子中,當(dāng)你終止調(diào)用 foo 類的時(shí)候,其析構(gòu)函數(shù)將會(huì)被調(diào)用,上例中會(huì)輸出 “bye bye”。


對(duì)象的引用

眾所周知,在PHP4 中,傳遞變量給一個(gè)函數(shù)或方法,實(shí)際是把這個(gè)變量做了一次復(fù)制,也就意味著你傳給函數(shù)或方法的是這個(gè)變量的一個(gè)副本,除非你使用了引用符號(hào) “&” 來(lái)聲明是要做一個(gè)引用,而不是一個(gè) Copy。在 PHP5 中,對(duì)象總是以引用的形式存在的,對(duì)象中的賦值操作同樣也都是一個(gè)引用操作。

例二:對(duì)象的引用


class foo {
var $x;
function setX($x) {
$this->x = $x;
}
function getX() {
return $this->x;
}
}
$o1 = new foo;
$o1->setX(4);
$o2 = $o1;
$o1->setX(5);
if($o1->getX() == $o2->getX()) print("Oh my god!");
?>


對(duì)象的克隆

如上所述,當(dāng)一個(gè)對(duì)象始終以引用的形式來(lái)被調(diào)用時(shí),如果我想得到該對(duì)象的一個(gè)副本,該怎么辦呢?PHP5 提供了一個(gè)新的功能,就是對(duì)象的克隆,語(yǔ)法為 __clone。

例三:對(duì)象的克隆
class foo {
var $x;
function setX($x) {
$this->x = $x;
}
function getX() {
return $this->x;
}
}
$o1 = new foo;
$o1->setX(4);
$o2 = $o1->__clone();
$o1->setX(5); if($o1->getX() != $o2->getX()) print("Copies are independant");
?>

對(duì)象克隆的方法在其它很多應(yīng)用程序語(yǔ)言中都是存在的,所以你不必?fù)?dān)心它的穩(wěn)定性。:)


對(duì)象中的私有、公共及保護(hù)模式

PHP4 中,一個(gè)對(duì)象的所有方法和變量都是公共的,這意味著你可以在一個(gè)對(duì)象的外部操作其中的任意一個(gè)變量和方法。PHP5 引入了三種新的用來(lái)控制這種存取權(quán)限的模式,它們是:公共的(Public)、受保護(hù)的(Protected)及私有的(Private)。

公共模式(Public):允許在對(duì)象外部進(jìn)行操作控制。
私有模式(Private):只允許本對(duì)象內(nèi)的方法對(duì)其進(jìn)行操作控制。
受保護(hù)模式(Protected):允許本對(duì)象及其父對(duì)象對(duì)其進(jìn)行操作控制。

例四: 對(duì)象中的私有、公共及受保護(hù)模式

class foo {
private $x;
public function public_foo() {
print("I'm public");
}
protected function protected_foo() {
$this->private_foo(); //Ok because we are in the same class we can call private methods
print("I'm protected");
}
private function private_foo() {
$this->x = 3;
print("I'm private");
}
}
class foo2 extends foo {
public function display() {
$this->protected_foo();
$this->public_foo();
// $this->private_foo(); // Invalid! the function is private in the base class
}
} $x = new foo();
$x->public_foo();
//$x->protected_foo(); //Invalid cannot call protected methods outside the class and derived classes
//$x->private_foo(); //Invalid private methods can only be used inside the class $x2 = new foo2();
$x2->display();
?>

提示:對(duì)象中的變量總是以私有形式存在的,直接操作一個(gè)對(duì)象中的變量不是一個(gè)好的面向?qū)ο缶幊痰牧?xí)慣,更好的辦法是把你想要的變量交給一個(gè)對(duì)象的方法去處理。


接口 (Interfaces)

眾所周知,PHP4 中的對(duì)象支持繼承,要使一個(gè)對(duì)象成為另一個(gè)對(duì)象的派生類,你需要使用類似 “class foo extends parent” 的代碼來(lái)控制。 PHP4 和 PHP5 中,一個(gè)對(duì)象都僅能繼承一次,多重繼承是不被支持的。不過,在 PHP5 中產(chǎn)生了一個(gè)新的名詞:接口,接口是一個(gè)沒有具體處理代碼的特殊對(duì)象,它僅僅定義了一些方法的名稱及參數(shù),此后的對(duì)象就可以方便的使用 'implement' 關(guān)鍵字把需要的接口整合起來(lái),然后再加入具體的執(zhí)行代碼。

例五:接口

interface displayable {
function display();
}
interface printable {
function doprint();
}

class foo implements displayable,printable {
function display() {
// code
} function doprint() {
// code
}
}
?>

這對(duì)提高代碼的可讀性及通俗性有很大的幫助,通過上面的例子可以看到,對(duì)象 foo 包含了 displayable 和 printable 兩個(gè)接口,這時(shí)我們就可以清楚的知道,對(duì)象 foo 一定會(huì)有一個(gè) display() 方法和一個(gè) print() 方法,只需要去了解接口部分,你就可以輕易的操作該對(duì)象而不必去關(guān)心對(duì)象的內(nèi)部是如何運(yùn)作的。



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

What is the difference between php5 and php8 What is the difference between php5 and php8 Sep 25, 2023 pm 01:34 PM

The differences between php5 and php8 are in terms of performance, language structure, type system, error handling, asynchronous programming, standard library functions and security. Detailed introduction: 1. Performance improvement. Compared with PHP5, PHP8 has a huge improvement in performance. PHP8 introduces a JIT compiler, which can compile and optimize some high-frequency execution codes, thereby improving the running speed; 2. Improved language structure, PHP8 introduces some new language structures and functions. PHP8 supports named parameters, allowing developers to pass parameter names instead of parameter order, etc.

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 private mean in java What does private mean in java Nov 24, 2022 pm 06:27 PM

In Java, private means "private" and is an access control modifier used to modify classes, properties and methods. Class members modified with private can only be accessed and modified by the methods of the class itself, and cannot be accessed and referenced by any other class (including subclasses of the class); therefore, the private modifier has the highest level of protection.

How to change port 80 in php5 How to change port 80 in php5 Jul 24, 2023 pm 04:57 PM

How to change port 80 in php5: 1. Edit the port number in the Apache server configuration file; 2. Edit the PHP configuration file to ensure that PHP works on the new port; 3. Restart the Apache server, and the PHP application will start running on the new port. run on the port.

Detailed explanation of the role and function of the MySQL.proc table Detailed explanation of the role and function of the MySQL.proc table Mar 16, 2024 am 09:03 AM

Detailed explanation of the role and function of the MySQL.proc table. MySQL is a popular relational database management system. When developers use MySQL, they often involve the creation and management of stored procedures (StoredProcedure). The MySQL.proc table is a very important system table. It stores information related to all stored procedures in the database, including the name, definition, parameters, etc. of the stored procedures. In this article, we will explain in detail the role and functionality of the MySQL.proc table

How to Fix Can't Connect to App Store Error on iPhone How to Fix Can't Connect to App Store Error on iPhone Jul 29, 2023 am 08:22 AM

Part 1: Initial Troubleshooting Steps Checking Apple’s System Status: Before delving into complex solutions, let’s start with the basics. The problem may not lie with your device; Apple's servers may be down. Visit Apple's System Status page to see if the AppStore is working properly. If there's a problem, all you can do is wait for Apple to fix it. Check your internet connection: Make sure you have a stable internet connection as the "Unable to connect to AppStore" issue can sometimes be attributed to a poor connection. Try switching between Wi-Fi and mobile data or resetting network settings (General > Reset > Reset Network Settings > Settings). Update your iOS version:

See all articles