Home Backend Development PHP Tutorial PHP5 中的三大特色功能_PHP

PHP5 中的三大特色功能_PHP

Jun 01, 2016 pm 12:25 PM
function Function object method feature


  PHP5 中的三大特色功能。这三大特点为:
  
  * 新的对象模式 (New Object Mode)
  
  * 异常处理 (Exceptions)
  
  * 名称空间 (Namespace)
  
  在开始之前,要声明两点:
  
  * 文章中的例子为了说明如何操作,有些部分使用了 PHP4 的表现手段,这仅仅是为了提高文章的可读性。
  
  * 文章中描述的部分与 PHP5 的最终发布版可能会有一些出入
  
  在 PHP5 没有最终正式发布前,你可以随时从 http://snaps.php.net 下载到最新的编译版本来亲自体验一下 PHP5 所带给我们这些崭新的功能。
  
  新的对象模式
  
  PHP5 中的对象已经进行了较系统、较全面的调整,现在的样子可能看起来会有些类似于 Java。本小节着重讲述 PHP5 中新的对象模式,并举了一些较简易的例子来说明。就让本节成为你的 PHP5 之旅的一个新起点吧。:)
  
  * 构造函数和析构函数
  
  * 对象的引用
  
  * 对象的克隆
  
  * 对象中的私有、公共及受保护模式
  
  * 接口 (Interfaces)
  
  * 抽象类
  
  * __call
  
  * __set 和 __get
  
  * 静态成员
  
  构造函数和析构函数
  
  在 PHP4 中,当函数与对象同名时,这个函数将成为该对象的构造函数,并且在 PHP4 中没有析构函数的概念。
  
  在 PHP5 中,构造函数被统一命名为 __construct,并且引入了析构函数的概念,被统一命名为 __destruct。
  
  例一:构造函数和析构函数
  
    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();
  ?>
  在上面的例子中,当你终止调用 foo 类的时候,其析构函数将会被调用,上例中会输出 “bye bye”。
  
  对象的引用
  
  众所周知,在PHP4 中,传递变量给一个函数或方法,实际是把这个变量做了一次复制,也就意味着你传给函数或方法的是这个变量的一个副本,除非你使用了引用符号 “&” 来声明是要做一个引用,而不是一个 Copy。在 PHP5 中,对象总是以引用的形式存在的,对象中的赋值操作同样也都是一个引用操作。
  
  例二:对象的引用
  
    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!");
  ?>
  
  对象的克隆
  
  如上所述,当一个对象始终以引用的形式来被调用时,如果我想得到该对象的一个副本,该怎么办呢?PHP5 提供了一个新的功能,就是对象的克隆,语法为 __clone。
  
  例三:对象的克隆
    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");
  ?>
  对象克隆的方法在其它很多应用程序语言中都是存在的,所以你不必担心它的稳定性。:)
  
  对象中的私有、公共及保护模式
  
  PHP4 中,一个对象的所有方法和变量都是公共的,这意味着你可以在一个对象的外部操作其中的任意一个变量和方法。PHP5 引入了三种新的用来控制这种存取权限的模式,它们是:公共的(Public)、受保护的(Protected)及私有的(Private)。
  
  公共模式(Public):允许在对象外部进行操作控制。
  
  私有模式(Private):只允许本对象内的方法对其进行操作控制。
  
  受保护模式(Protected):允许本对象及其父对象对其进行操作控制。
  
  例四: 对象中的私有、公共及受保护模式
  
    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();
  ?>
  
  提示:对象中的变量总是以私有形式存在的,直接操作一个对象中的变量不是一个好的面向对象编程的习惯,更好的办法是把你想要的变量交给一个对象的方法去处理。
  
  接口 (Interfaces)
  
  众所周知,PHP4 中的对象支持继承,要使一个对象成为另一个对象的派生类,你需要使用类似 “class foo extends parent” 的代码来控制。 PHP4 和 PHP5 中,一个对象都仅能继承一次,多重继承是不被支持的。不过,在 PHP5 中产生了一个新的名词:接口,接口是一个没有具体处理代码的特殊对象,它仅仅定义了一些方法的名称及参数,此后的对象就可以方便的使用 'implement' 关键字把需要的接口整合起来,然后再加入具体的执行代码。
  
  例五:接口
  
    interface displayable {
  function display();
  }
  interface printable {
  function doprint();
  }
  
  class foo implements displayable,printable {
  function display() {
  // code
  }  function doprint() {
  // code
  }
  }
  ?>
  这对提高代码的可读性及通俗性有很大的帮助,通过上面的例子可以看到,对象 foo 包含了 displayable 和 printable 两个接口,这时我们就可以清楚的知道,对象 foo 一定会有一个 display() 方法和一个 print() 方法,只需要去了解接口部分,你就可以轻易的操作该对象而不必去关心对象的内部是如何运作的。
  
  抽象类
  
  抽象类不能被实例化。
  
  抽象类与其它类一样,允许定义变量及方法。
  
  抽象类同样可以定义一个抽象的方法,抽象类的方法不会被执行,不过将有可能会在其派生类中执行。
  
  例六:抽象类
  
    abstract class foo {
  protected $x;
  abstract function display();
  function setX($x) {
  $this->x = $x;
  }
  }
  class foo2 extends foo {
  function display() {
  // Code
  }
  }
  ?>
  
  __call
  
  PHP5 的对象新增了一个专用方法 __call(),这个方法用来监视一个对象中的其它方法。如果你试着调用一个对象中不存在的方法,__call 方法将会被自动调用。
  
  例七:__call
  
    class foo {
  function __call($name,$arguments) {
  print("Did you call me? I'm $name!");
  }
  } $x = new foo();
  $x->doStuff();
  $x->fancy_stuff();
  ?>
  
  __call 实现“过载”动作
  
  这个特殊的方法可以被用来实现“过载(overloading)”的动作,这样你就可以检查你的参数并且通过调用一个私有的方法来传递参数。
  
  例八:使用 __call 实现“过载”动作
  
    class Magic {
  function __call($name,$arguments) {
  if($name=='foo') {
  if(is_int($arguments[0])) $this->foo_for_int($arguments[0]);
  if(is_string($arguments[0])) $this->foo_for_string($arguments[0]);
  }
  }  private function foo_for_int($x) {
  print("oh an int!");
  }  private function foo_for_string($x) {
  print("oh a string!");
  }
  } $x = new Magic();
  $x->foo(3);
  $x->foo("3");
  ?>
  
  > __set 和 __get
  
  这是一个很棒的方法,__set 和 __get 方法可以用来捕获一个对象中不存在的变量和方法。
  
  例九: __set 和 __get
  
    class foo {
  function __set($name,$val) {
  print("Hello, you tried to put $val in $name");
  }
  function __get($name) {
  print("Hey you asked for $name");
  }
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

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.

Undress AI Tool

Undress AI Tool

Undress images for free

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)

How to write a novel in the Tomato Free Novel app. Share the tutorial on how to write a novel in Tomato Novel. How to write a novel in the Tomato Free Novel app. Share the tutorial on how to write a novel in Tomato Novel. Mar 28, 2024 pm 12:50 PM

Tomato Novel is a very popular novel reading software. We often have new novels and comics to read in Tomato Novel. Every novel and comic is very interesting. Many friends also want to write novels. Earn pocket money and edit the content of the novel you want to write into text. So how do we write the novel in it? My friends don’t know, so let’s go to this site together. Let’s take some time to look at an introduction to how to write a novel. Share the Tomato novel tutorial on how to write a novel. 1. First open the Tomato free novel app on your mobile phone and click on Personal Center - Writer Center. 2. Jump to the Tomato Writer Assistant page - click on Create a new book at the end of the novel.

How to recover deleted contacts on WeChat (simple tutorial tells you how to recover deleted contacts) How to recover deleted contacts on WeChat (simple tutorial tells you how to recover deleted contacts) May 01, 2024 pm 12:01 PM

Unfortunately, people often delete certain contacts accidentally for some reasons. WeChat is a widely used social software. To help users solve this problem, this article will introduce how to retrieve deleted contacts in a simple way. 1. Understand the WeChat contact deletion mechanism. This provides us with the possibility to retrieve deleted contacts. The contact deletion mechanism in WeChat removes them from the address book, but does not delete them completely. 2. Use WeChat’s built-in “Contact Book Recovery” function. WeChat provides “Contact Book Recovery” to save time and energy. Users can quickly retrieve previously deleted contacts through this function. 3. Enter the WeChat settings page and click the lower right corner, open the WeChat application "Me" and click the settings icon in the upper right corner to enter the settings page.

The difference between vivox100s and x100: performance comparison and function analysis The difference between vivox100s and x100: performance comparison and function analysis Mar 23, 2024 pm 10:27 PM

Both vivox100s and x100 mobile phones are representative models in vivo's mobile phone product line. They respectively represent vivo's high-end technology level in different time periods. Therefore, the two mobile phones have certain differences in design, performance and functions. This article will conduct a detailed comparison between these two mobile phones in terms of performance comparison and function analysis to help consumers better choose the mobile phone that suits them. First, let’s look at the performance comparison between vivox100s and x100. vivox100s is equipped with the latest

How to set font size on mobile phone (easily adjust font size on mobile phone) How to set font size on mobile phone (easily adjust font size on mobile phone) May 07, 2024 pm 03:34 PM

Setting font size has become an important personalization requirement as mobile phones become an important tool in people's daily lives. In order to meet the needs of different users, this article will introduce how to improve the mobile phone use experience and adjust the font size of the mobile phone through simple operations. Why do you need to adjust the font size of your mobile phone - Adjusting the font size can make the text clearer and easier to read - Suitable for the reading needs of users of different ages - Convenient for users with poor vision to use the font size setting function of the mobile phone system - How to enter the system settings interface - In Find and enter the "Display" option in the settings interface - find the "Font Size" option and adjust it. Adjust the font size with a third-party application - download and install an application that supports font size adjustment - open the application and enter the relevant settings interface - according to the individual

The secret of hatching mobile dragon eggs is revealed (step by step to teach you how to successfully hatch mobile dragon eggs) The secret of hatching mobile dragon eggs is revealed (step by step to teach you how to successfully hatch mobile dragon eggs) May 04, 2024 pm 06:01 PM

Mobile games have become an integral part of people's lives with the development of technology. It has attracted the attention of many players with its cute dragon egg image and interesting hatching process, and one of the games that has attracted much attention is the mobile version of Dragon Egg. To help players better cultivate and grow their own dragons in the game, this article will introduce to you how to hatch dragon eggs in the mobile version. 1. Choose the appropriate type of dragon egg. Players need to carefully choose the type of dragon egg that they like and suit themselves, based on the different types of dragon egg attributes and abilities provided in the game. 2. Upgrade the level of the incubation machine. Players need to improve the level of the incubation machine by completing tasks and collecting props. The level of the incubation machine determines the hatching speed and hatching success rate. 3. Collect the resources required for hatching. Players need to be in the game

What exactly is self-media? What are its main features and functions? What exactly is self-media? What are its main features and functions? Mar 21, 2024 pm 08:21 PM

With the rapid development of the Internet, the concept of self-media has become deeply rooted in people's hearts. So, what exactly is self-media? What are its main features and functions? Next, we will explore these issues one by one. 1. What exactly is self-media? We-media, as the name suggests, means you are the media. It refers to an information carrier through which individuals or teams can independently create, edit, publish and disseminate content through the Internet platform. Different from traditional media, such as newspapers, television, radio, etc., self-media is more interactive and personalized, allowing everyone to become a producer and disseminator of information. 2. What are the main features and functions of self-media? 1. Low threshold: The rise of self-media has lowered the threshold for entering the media industry. Cumbersome equipment and professional teams are no longer needed.

Quickly master: How to open two WeChat accounts on Huawei mobile phones revealed! Quickly master: How to open two WeChat accounts on Huawei mobile phones revealed! Mar 23, 2024 am 10:42 AM

In today's society, mobile phones have become an indispensable part of our lives. As an important tool for our daily communication, work, and life, WeChat is often used. However, it may be necessary to separate two WeChat accounts when handling different transactions, which requires the mobile phone to support logging in to two WeChat accounts at the same time. As a well-known domestic brand, Huawei mobile phones are used by many people. So what is the method to open two WeChat accounts on Huawei mobile phones? Let’s reveal the secret of this method. First of all, you need to use two WeChat accounts at the same time on your Huawei mobile phone. The easiest way is to

What are the functions of Xiaohongshu account management software? How to operate a Xiaohongshu account? What are the functions of Xiaohongshu account management software? How to operate a Xiaohongshu account? Mar 21, 2024 pm 04:16 PM

As Xiaohongshu becomes popular among young people, more and more people are beginning to use this platform to share various aspects of their experiences and life insights. How to effectively manage multiple Xiaohongshu accounts has become a key issue. In this article, we will discuss some of the features of Xiaohongshu account management software and explore how to better manage your Xiaohongshu account. As social media grows, many people find themselves needing to manage multiple social accounts. This is also a challenge for Xiaohongshu users. Some Xiaohongshu account management software can help users manage multiple accounts more easily, including automatic content publishing, scheduled publishing, data analysis and other functions. Through these tools, users can manage their accounts more efficiently and increase their account exposure and attention. In addition, Xiaohongshu account management software has

See all articles