php4和php5区别

PHP5 中的对象已经进行了较系统、较全面的调整,现在的样子可能看起来会有些类似于 Java。本小节着重讲述 PHP5 中新的对象模式,并举了一些较简易的例子来说明。就让本节成为你的 PHP5 之旅的一个新起点吧。:)

* 构造函数和析构函数

* 对象的引用

* 对象的克隆

* 对象中的私有、公共及受保护模式

* 接口 (Interfaces)

* 抽象类

* __call

* __set 和 __get

* 静态成员

构造函数和析构函数

在 PHP4 中,当函数与对象同名时,这个函数将成为该对象的构造函数,并且在 PHP4 中没有析构函数的概念。

在 PHP5 中,构造函数被统一命名为 __construct,并且引入了析构函数的概念,被统一命名为 __destruct。

例一:构造函数和析构函数

  1.  
  2. class foo {  
  3. var $x;  
  4. function __construct($x) {  
  5. $this->x = $x;  
  6. }  
  7. function display() {  
  8. print($this->x);  
  9. }  
  10. function __destruct() {  
  11. print("bye bye");  
  12. }  
  13. }  
  14. $o1 = new foo(4);  
  15. $o1->display();  
  16. ?> 

在上面的例子中,当你终止调用 foo 类的时候,其析构函数将会被调用,上例中会输出 “bye bye”。
对象的引用

众所周知,在PHP4 中,传递变量给一个函数或方法,实际是把这个变量做了一次复制,也就意味着你传给函数或方法的是这个变量的一个副本,除非你使用了引用符号 “&” 来声明是要做一个引用,而不是一个 Copy。在 PHP5 中,对象总是以引用的形式存在的,对象中的赋值操作同样也都是一个引用操作。

例二:对象的引用

  1.  
  2. class foo {  
  3. var $x;  
  4. function setX($x) {  
  5. $this->x = $x;  
  6. }  
  7. function getX() {  
  8. return $this->x;  
  9. }  
  10. }  
  11. $o1 = new foo;  
  12. $o1->setX(4);  
  13. $o2 = $o1;  
  14. $o1->setX(5);  
  15. if($o1->getX() == $o2->getX()) print("Oh my god!");  
  16. ?> 

对象的克隆

如上所述,当一个对象始终以引用的形式来被调用时,如果我想得到该对象的一个副本,该怎么办呢?PHP5 提供了一个新的功能,就是对象的克隆,语法为 __clone。

例三:对象的克隆

  1.  
  2. class foo {  
  3. var $x;  
  4. function setX($x) {  
  5. $this->x = $x;  
  6. }  
  7. function getX() {  
  8. return $this->x;  
  9. }  
  10. }  
  11. $o1 = new foo;  
  12. $o1->setX(4);  
  13. $o2 = $o1->__clone();  
  14. $o1->setX(5); if($o1->getX() != $o2->getX()) print("Copies are independant");  
  15. ?> 

对象克隆的方法在其它很多应用程序语言中都是存在的,所以你不必担心它的稳定性。:)

对象中的私有、公共及保护模式

PHP4 中,一个对象的所有方法和变量都是公共的,这意味着你可以在一个对象的外部操作其中的任意一个变量和方法。PHP5 引入了三种新的用来控制这种存取权限的模式,它们是:公共的(Public)、受保护的(Protected)及私有的(Private)。

公共模式(Public):允许在对象外部进行操作控制。

私有模式(Private):只允许本对象内的方法对其进行操作控制。

受保护模式(Protected):允许本对象及其父对象对其进行操作控制。

波比源码 – 精品源码模版分享 | www.bobi11.com
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 本站源码并不保证全部能正常使用,仅供有技术基础的人学习研究,请谨慎下载
8. 如遇到加密压缩包,请使用WINRAR解压,如遇到无法解压的请联系管理员!

波比源码 » php4和php5区别

270 评论

  1. avodart over the counter flomax cheap buy zofran 4mg generic

  2. oral suhagra 50mg sildalis cost estrace 1mg price

  3. purchase modafinil without prescription modafinil 200mg us buy diamox sale

  4. order altace 5mg online ramipril 5mg drug buy astelin 10 ml generic

  5. buy buspar 10mg without prescription order buspar pill ditropan tablet

  6. tadalafil 5mg cost brand cialis female viagra cvs

  7. order itraconazole for sale prometrium usa order tinidazole 500mg sale

  8. allopurinol 300mg sale buy ezetimibe cheap zetia

  9. order sildenafil 50mg pill sildenafil 200mg buy generic cialis 5mg

  10. buy sildenafil 100mg generic nolvadex canada budesonide pill

  11. tadalafil 20mg usa buy voltaren purchase indomethacin generic

  12. purchase clarithromycin without prescription meclizine 25mg oral order antivert 25 mg for sale

  13. online blackjack live dealer online pharmacy professional research paper writers

  14. buy generic indomethacin 75mg tamsulosin cheap oral cenforce 100mg

  15. buy famotidine 40mg generic prograf 1mg oral mirtazapine 30mg price

  16. fildena 50mg usa brand propecia order propecia for sale

  17. restate thesis generator open thesis example cause and effect thesis statement examples

  18. sildenafil citrate 50mg viagra brand overnight cialis delivery

  19. levaquin 250mg without prescription brand levaquin 500mg order bupropion 150mg for sale

  20. amoxil over the counter zithromax price generic prednisolone 10mg

  21. order pregabalin online cheap loratadine 10mg ca buy dapoxetine 60mg online

  22. buy generic orlistat 60mg buy zovirax cheap buy allopurinol 300mg for sale

  23. buy generic nebivolol nebivolol uk order clozaril without prescription

  24. simvastatin oral brand zocor viagra 100mg over the counter

  25. buy fluconazole 100mg generic oral cipro buy ciprofloxacin cheap

  26. fluoxetine seizure threshold prozac ocd natural alternative to prozac

  27. order furosemide without prescription vibra-tabs generic buy ventolin 4mg pills

  28. purchase vardenafil without prescription vardenafil price order hydroxychloroquine 400mg for sale

  29. rosuvastatin 20mg tablet generic motilium buy domperidone without a prescription

  30. purchase rhinocort online buy budesonide cheap buy bimatoprost without prescription

  31. tretinoin cream tablet buy avana avana 200mg generic

  32. the gentle rustling of leaves in the breeze creates a peaceful ambiance that soothes the soul crawling google to crack down on office attendance, asks remote workers to reconside

  33. buy lamisil 250mg without prescription trimox 250mg pill order trimox online

  34. buy anastrozole 1mg online cheap how to buy arimidex buy generic catapres online

  35. over the counter ed pills that work sildenafil viagra buy viagra 100mg sale

  36. avlosulfon online order nifedipine cheap order aceon online cheap

  37. buy allegra 180mg generic buy fexofenadine buy glimepiride generic

  38. buy albenza medication abilify online buy provera 5mg pills

  39. buy nitrofurantoin generic nortriptyline uk buy pamelor 25mg pill

  40. buy acetaminophen generic pepcid 20mg brand famotidine order online

  41. augmentin for dogs augmentin jr augmentin et loperamide

  42. can you drink alcohol if your taking ciprofloxacin is ciprofloxacin safe ciprofloxacin sulfa allergy

  43. where to buy seroquel without a prescription zoloft for sale order escitalopram 20mg pill

评论已关闭

Hi, 如果你对这款模板有疑问,可以跟我联系哦!

联系站长
赞助VIP 享更多特权,建议使用 QQ 登录
喜欢我嘛?喜欢就按“ctrl+D”收藏我吧!♡