The beginning of in-depth exploration of PHP 5.0 object model_PHP tutorial

WBOY
Release: 2016-07-13 17:35:05
Original
714 people have browsed it

  面向对象编程被设计来为大型软件项目提供解决方案,尤其是多人合作的项目. 当源代码增长到一万行甚至更多的时候,每一个更动都可能导致不希望的副作用. 这种情况发生于模块间结成秘密联盟的时候,就像第一次世界大战前的欧洲。

  //haohappy注:喻指模块间的关联度过高,相互依赖性太强.更动一个模块导致其它模块也必须跟着更动。

  想像一下,如果有一个用来处理登录的模块允许一个信用卡处理模块来分享它的数据库连接. 当然出发点是好的,节省了进行另一个数据库连接的支出.然而有时,登录处理模块改变了其中一个变量的名字,就可能割断了两者间的协议.导致信用卡模块的处理出错,进而导致处理发票的模块出错. 很快地,体系中所有无关的模块都可能由此出错.

  因此,我觉得有点戏剧性地,绝大多数程序员都对耦合和封装心存感激. 耦合是两个模块间依赖程度的量度. 耦合越少越好.我们希望能够从已有的项目中抽走一个模块并在另一个新项目中使用.

  我们也希望在某个模块内部大规模的更动而不用担心对其他模块的影响. 封装的原则可以提供这个解决方案.模块被看待成相对独立,并且模块间的数据通信通过接口来进行. 模块不通过彼此的变量名来窥探另一个模块,它们通过函数来礼貌地发送请求.

  封装是你可以在任何编程语言中使用的一个原则. 在PHP和许多面向过程的语言中,可以偷懒是很有诱惑的.没有什么可以阻止你通过模块来构建一个假想的WEB. 面向对象编程是使程序员不会违背封装原则的一种方法.

  在面向对象编程中,模块被组织成一个个对象. 这些对象拥有方法和属性. 从抽象的角度来看,方法是一个对象的所做的动作,而属性是对象的特性.从编程角度来看,方法就是函数而属性是变量. 在一个理想化的面向对象体系中,每个部份都是一个对象. 体系由对象及对象间通过方法来形成的联系构成.

  一个类定义了对象的属性. 如果你在烘烤一组甜饼对象,那么类将会是甜饼机. 类的属性和方法是被调用的成员. 人们可以通过说出数据成员或者方法成员来表达.

  每种语言提供了不同的途径来访问对象. PHP从C++中借用概念,提供一个数据类型用来在一个标识符下包含函数和变量。最初设计PHP的时候,甚至PHP3被开发出时,PHP并不打算提供开发超过10万行代码的大型项目的能力。随着PHP和Zend引擎的发展,开发大型项目变得有可能,但无论你的项目规模多大,用类来书写你的脚本将可以让代码实现重用。这是一个好主意,特别当你愿意与别人分享你的代码的时候。

  有关对象的想法是计算机科学上最令人兴奋的概念之一。开始很难掌握它,但我可以保证,一旦你掌握了它,用它的思维来思考将会非常自然。

  PHP5 的对象模型

  PHP5有一个单重继承的,限制访问的,可以重载的对象模型. 本章稍后会详细讨论的”继承”,包含类间的父-子关系. 另外,PHP支持对属性和方法的限制性访问. 你可以声明成员为private,不允许外部类访问. 最后,PHP允许一个子类从它的父类中重载成员.

  PHP5的对象模型把对象看成与任何其它数据类型不同,通过引用来传递. PHP不要求你通过引用(reference)显性传递和返回对象. 在本章的最后将会详细阐述基于引用的对象模型. 它是PHP5中最重要的新特性.

  有了更直接的对象模型,就拥有了附加的优势: 效率提高, 占用内存少,并且具有更大的灵活性.

  在PHP的前几个版本中,脚本默认复制对象.现在PHP5只移动句柄,需要更少的时间. 脚本执行效率的提升是由于避免了不必要的复制. 在对象体系带来复杂性的同时,也带来了执行效率上的收益. 同时,减少复制意味着占用更少的内存,可以留出更多内存给其它操作,这也使效率提高.

  Zand引擎2具有更大的灵活性. 一个令人高兴的发展是允许析构--在对象销毁之前执行一个类方法. 这对于利用内存也很有好处,让PHP清楚地知道什么时候没有对象的引用,把空出的内存分配到其它用途.

  PHP5的内存管理

  对象传递

PHP5 uses Zend Engine II. Objects are stored in a separate structure Object Store, instead of being stored in Zval like other general variables (in PHP4, objects are stored in Zval like general variables). Only the pointer of the object is stored in Zval rather than the content (value). When we copy an object or pass an object as a parameter to a function, we do not need to copy the data. Just keep the same object pointer and notify the Object Store that this particular object now points to via another zval. Since the object itself is located in the Object Store, any changes we make to it will affect all zval structures holding pointers to the object - manifested in the program as any changes to the target object will affect the source object. .This makes PHP objects look like they are always passed by reference, so objects in PHP are passed by "reference" by default and you no longer need to use & to declare them like in PHP4.

Garbage collection mechanism

Some languages, most typically C, require you to explicitly request memory allocation when you create a data structure. Once you allocate memory, you can store information in variables. At the same time, you also need to release the memory when you are finished using the variable, which allows the machine to free up memory for other variables and avoid running out of memory.

PHP can automatically manage memory and clear objects that are no longer needed. PHP uses a simple garbage collection mechanism called reference counting. Each object contains a reference counter, and each reference connected to the object increases the counter by one. When reference leaves the living space or is set to NULL, the counter is decremented by 1. When an object's reference counter reaches zero, PHP knows that you no longer need to use this object and releases the memory space it occupies.

For example:

<?php
class Person{}
function sendEmailTo(){}

$haohappy = new Person( );
// Create a new object: Reference count = 1
$haohappy2 = $haohappy;
// Copy by reference: Reference count = 2
unset($haohappy);
// Deleting a reference: Reference count = 1
sendEmailTo($haohappy2);
// Passing an object by reference:
// During function execution:
// Reference count = 2
// After execution:
// Reference count = 1

unset($haohappy2);
//Delete reference: Reference count = 0 automatically release memory space

?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/508377.htmlTechArticleObject-oriented programming is designed to provide solutions for large-scale software projects, especially projects where multiple people collaborate. When the source When the code grows to 10,000 lines or more, every change may lead to...
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!