Home php教程 php手册 Php5.0说明------为面向对象而生的php5

Php5.0说明------为面向对象而生的php5

Jun 13, 2016 am 10:24 AM
php5 for object illustrate For

Php5.0说明 ------为面向对象而生的php5 为面向对象而生的PHP5 --------------------------------------------- [摘要]目前开发中的PHP5,其面向对象的机能已经被大幅度的强化了。下一代的PHP将会是怎样的一种语言呢?下面我们来详细讲解一下目前发布的PHP5的beta release。 (一) Zend 2.0的诞生 现在的PHP4所使用的基本文法是被称之为Zend 引擎的脚本编译引擎。这个就是PHP4的优良机能的原因之一,是作为对PHP3的改进而生成的一种语言。大家一直认为,PHP4的性能根据当初的目标,比PHP3有了很大的提升,在网络编程的世界里占据了很大的份额。 开发了Zend 引擎的Zend公司是在开发PHP4的同时,由PHP3的主要开发者Zeev Suraski和Andi Gutmans所创立的企业合并而来的。Zend的名称是由Zeev和Andi的名字合起来组成的。Zend公司的商业模式是,持续不断的为open source提供zend 引擎的PHP内核 (core),同时提升周边产品开发和贩卖的利益。以open source software作为基盘的商业,在世界范围内大多数正在苦战的企业中,算是比较好的典型例子了。 ■PHP4的局限 托PHP4成功的福,这个用途的适用范围逐渐变广起来。作为企业级的用途而使用PHP的说法时有所闻。因此,就有了这样一个问题,构筑大规模网站的时候,代码的再利用性十分差。具体来说就是,PHP4的面向对象性能很弱,因此习惯于使用Java等的技术人员对此有很多的抱怨。 逐步的改善PHP4的面向对象的性能,大幅度的更改基本文法,开发者达成了更新PHP记述方法的开拓目的。 ■Zend 2.0开始开发 随后,Zend公司PHP中心的开发者们在2001年7月发表了作为下一代PHP语言引擎的Zend 2.0引擎的构想。以[Zend Engine version 2.0: Feature Overview and Design] (http://www.zend.com/engine2/ZendEngine-2.0.pdf)作为目标的同时,面向对象的性能大幅度的强化了。 目前的PHP4 Zend 引擎的扩张情况与昔日的PHP3如出一辙。这就意味着,要提升新的语言引擎的主版本号,明确方法目标,迎接来自开发团体的称赞。 Ze2的开发,与以往的Zend引擎一样,都是运行在open source的模式下的。最新的源代码在CVS上被全面的公开,因为是面向开放的开发者的,关于开发的议论非常的活跃。 现在Ze2被决定采用于PHP的下一个版本PHP5中。最终发布的时间现在还未定,但是假如根据Zend公司2003年4月1日发布的Newsletter的话,现在的应该就是Beta Release了。 (二) PHP5的新特性 接下来请按照顺序看一下被强化的PHP5的性能。首先是最为重要的面向对象性能,类的实体特性在大幅度的被修改着。这里说的仅是关于类的新特性。 · 对象的参照过渡是默认的(default) · 引入访问属性的限制 · 引入访问方法的限制 · 抽象类和抽象方法 · 接口 · final声明 · 名空间 · 类内常量 · 类变量 · 统一构建器 · 析构函数(Distructor) · 其他附属特性 以上内容是根据2003年4月22日CVS上登录版本资料所写的,在正式的发布之前,也有变动的可能性。 ■对象的默认参照过渡 在PHP4中,在以变量$var1为类的实体对象的时候,如果$var2 = $var1;那么,在$var2中,$var1的复制被代入。明显的,$var2为了指向与$var1相同的对象,就要写成$var2 =& $var1,必须要加上&作为参照。 而在PHP5,对象的代入将成为自动的参照过渡。也就是说, $var2=$var1,两者指向相同的对象。如果想要同php4一样,带入copy,那么就会运用到导入__clone()的方法。 $var2 = $var1->__clone();此处,clone前面是两个连续的“_” (这仅仅是类的实体的特性) ■引入访问属性的限制 在PHP4的类中,连同属性和方法在内,可以自由的访问类的内外任何地方,而没有限制。因此,用户就无法防范属性的无意中的更改。 而在PHP5中,同C++和Java一样,导入了private, protected, public三个等级的访问限制,使得类的设计者能够对属性和方法的使用方法进行限定。以下是各种访问限制的意思。 · Public: 可以自由的在类的内外任何地方进行参照、变更 · Private: 只能在这个类的方法中进行参照、变更 · Protected:能够在这个类以及继承了这个类的另一个类的方法中进行参照、变更。另外,在继承的类中,能够写入访问指定。 在PHP4中的“var”,同以往一样与public有着相同的意思。下面就来举一个例子,让我们来看看访问限制是怎样起作用的。 PHP代码:-------------------------------------------------------------------------------- class Hoge1 {  private $var1 = A;  protected $var2 = B;  protected $var3 = C;  function setLower() {   $this->var1 = a;   $this->var2 = b;   $this->var3 = c;  }  function var1() {   return $this->var1;  }  function var2() {   return $this->var2;  }  function var3() {   return $this->var3;  } } -------------------------------------------------------------------------------- 在这个类中,带有$var1, $var2, $var3三个属性。$var1被声明为private, $var2和$var3是protected.在此处 PHP代码:-------------------------------------------------------------------------------- $hoge=new Hoge1; echo’var1:’.$hoge->var1.” n” -------------------------------------------------------------------------------- 如果尝试参照不允许从外部进行访问的private属性,那么就会出现如下错误: Fatal error: Cannot access private property hoge1::$var1 in /path/to/script.php on line XX 对于protected的$var2也是相同的。 但是,因为$hoge的方法是没有private和protected的,所以下面的代码能够正常运作,返回内部私有和保护变量的值。 PHP代码:-------------------------------------------------------------------------------- echo var1: . $hoge->var1() . " "; // var1: A echo var2: . $hoge->var2() . " "; // var2: B echo var3: . $hoge->var3() . " "; // var3: C $hoge->setLower(); echo var1: . $hoge->var1() . " "; // var1: a echo var2: . $hoge->var2() . " "; // var2: b echo var3: . $hoge->var3() . " "; // var3: c -------------------------------------------------------------------------------- 其次,为了能够看到protected的属性的状态,我们试着创造了继承了Hoge1的类Hoge2 PHP代码:-------------------------------------------------------------------------------- class Hoge2 extends Hoge1 {  public $var3 = 3;  function d_var1() {   return $this->var1;  }  function d_var2() {   return $this->var2;  }  function d_var3() {   return $this->var3;  } } -------------------------------------------------------------------------------- 在类Hoge2中,只有$var3被声明为public。在属性是protected的情况下,从子类进行访问有何种限制,是由子类的属性声明决定的。在Hoge2中,因为$var3被声明是public,因此无论是从何处都可以访问Hoge2的$var3(实体是Hoge1的$var3)。因为$var1在Hoge1中是private,因此,在Hoge2子类中Hoge1的$var1不会被继承,而在Hoge2中有可能会做出名为$var1的属性,因此,必须要明确区分Hoge1::$var1和Hoge2::$var1。 PHP代码:-------------------------------------------------------------------------------- $hoge = new Hoge2; echo var1: . $hoge->var1 . " ";   // var1: // echo var2: . $hoge->var2 . " ";  // Error echo var3: . $hoge->var3 . " ";   // var3: 3 echo var1: . $hoge->d_var1() . " "; // var1: echo var2: . $hoge->d_var2() . " "; // var2: B echo var3: . $hoge->d_var3() . " "; // var3: 3 -------------------------------------------------------------------------------- $hoge->var1是与Hoge1::var1没有关系的变量,因此不会有任何显示,因为var2有protected访问限制,所以如果不通过method就直接参照$var2,就会出现致命错误。 ■引入访问方法的限制 与上述相同,此处也分为private, protected, public三种。 · Public: 能够从任何地方调用 · Private: 只能够从这个类的method内调用 · Protected: 只能够从这个类以及subclass的method中调用 此处的意思同Java和C++相同,请不要搞混。 ■抽象(abstract)的类和抽象的方法 支持与Java相同的抽象类和抽象方法。抽象方法只提供了方法名的调用方式,而没有提供实体。另外,持有抽象方法的类,必须抽象宣言类本身。如果想要直接作成抽象类的对象,那么就会出现如下的致命错误。 Fatal error: Cannot instantiate abstract class ClassName 产生错误的实际的例子如下所示: PHP代码:-------------------------------------------------------------------------------- abstract class MyAbstract {  abstract public function test();  public function test2() {   echo "MyAbstract::test2() called. ";  } } class MyImplement extends MyAbstract {  public function test() {   echo "MyImplement::test() called. ";  } } $obj = new MyImplement; $obj->test(); ?> -------------------------------------------------------------------------------- ■接口(interface) 支持与Java相同的接口(interface)。接口是适合所描述的外部调用形式而设计组合起来的。 接口的实体不能够记录。相反的,实现接口的类必须持有与这个接口的方法相对应的实体。另外,类能够实现多个接口,因此,有可能实现多重继承。 PHP代码:-------------------------------------------------------------

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

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.

Convert an array or object to a JSON string using PHP's json_encode() function Convert an array or object to a JSON string using PHP's json_encode() function Nov 03, 2023 pm 03:30 PM

JSON (JavaScriptObjectNotation) is a lightweight data exchange format that has become a common format for data exchange between web applications. PHP's json_encode() function can convert an array or object into a JSON string. This article will introduce how to use PHP's json_encode() function, including syntax, parameters, return values, and specific examples. Syntax The syntax of the json_encode() function is as follows: st

Source code exploration: How are objects called in Python? Source code exploration: How are objects called in Python? May 11, 2023 am 11:46 AM

Wedge We know that objects are created in two main ways, one is through Python/CAPI, and the other is by calling a type object. For instance objects of built-in types, both methods are supported. For example, lists can be created through [] or list(). The former is Python/CAPI and the latter is a calling type object. But for instance objects of custom classes, we can only create them by calling type objects. If an object can be called, then the object is callable, otherwise it is not callable. Determining whether an object is callable depends on whether a method is defined in its corresponding type object. like

What is the Request object in PHP? What is the Request object in PHP? Feb 27, 2024 pm 09:06 PM

The Request object in PHP is an object used to handle HTTP requests sent by the client to the server. Through the Request object, we can obtain the client's request information, such as request method, request header information, request parameters, etc., so as to process and respond to the request. In PHP, you can use global variables such as $_REQUEST, $_GET, $_POST, etc. to obtain requested information, but these variables are not objects, but arrays. In order to process request information more flexibly and conveniently, you can

How to convert MySQL query result array to object? How to convert MySQL query result array to object? Apr 29, 2024 pm 01:09 PM

Here's how to convert a MySQL query result array into an object: Create an empty object array. Loop through the resulting array and create a new object for each row. Use a foreach loop to assign the key-value pairs of each row to the corresponding properties of the new object. Adds a new object to the object array. Close the database connection.

Use Python's __contains__() function to define the containment operation of an object Use Python's __contains__() function to define the containment operation of an object Aug 22, 2023 pm 04:23 PM

Use Python's __contains__() function to define the containment operation of an object. Python is a concise and powerful programming language that provides many powerful features to handle various types of data. One of them is to implement the containment operation of objects by defining the __contains__() function. This article will introduce how to use the __contains__() function to define the containment operation of an object, and give some sample code. The __contains__() function is Pytho

Use Python's __le__() function to define a less than or equal comparison of two objects Use Python's __le__() function to define a less than or equal comparison of two objects Aug 21, 2023 pm 09:29 PM

Title: Using Python's __le__() function to define a less than or equal comparison of two objects In Python, we can define comparison operations between objects by using special methods. One of them is the __le__() function, which is used to define less than or equal comparisons. The __le__() function is a magic method in Python and is a special function used to implement the "less than or equal" operation. When we compare two objects using the less than or equal operator (<=), Python

What is the difference between arrays and objects in PHP? What is the difference between arrays and objects in PHP? Apr 29, 2024 pm 02:39 PM

In PHP, an array is an ordered sequence, and elements are accessed by index; an object is an entity with properties and methods, created through the new keyword. Array access is via index, object access is via properties/methods. Array values ​​are passed and object references are passed.

See all articles