免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2071 | 回复: 3
打印 上一主题 下一主题

面向对象的Php5和新的Zend2.0引擎 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2004-07-03 00:05 |只看该作者 |倒序浏览
PHP.net发布了新的版本PHP5,称为是一个真正面向对象的版本。有谁用过,请发表高见。
下面是从php.net摘录的介绍:
  1. 1。全新的对象模型
  2. 2。引入Private 和Protected 成员变量
  3. Example
  4. <?php
  5. class MyClass {
  6.    private $Hello = "Hello, World!\n";
  7.    protected $Bar = "Hello, Foo!\n";
  8.    protected $Foo = "Hello, Bar!\n";

  9.    function printHello() {
  10.        print "MyClass::printHello() " . $this->;Hello;
  11.        print "MyClass::printHello() " . $this->;Bar;
  12.        print "MyClass::printHello() " . $this->;Foo;
  13.    }
  14. }

  15. class MyClass2 extends MyClass {
  16.    protected $Foo;
  17.            
  18.    function printHello() {
  19.        MyClass::printHello();                          /* Should print */
  20.        print "MyClass2::printHello() " . $this->;Hello; /* Shouldn't print out anything */
  21.        print "MyClass2::printHello() " . $this->;Bar;  /* Shouldn't print (not declared)*/
  22.        print "MyClass2::printHello() " . $this->;Foo;  /* Should print */
  23.    }
  24. }

  25. $obj = new MyClass();
  26. print $obj->;Hello;  /* Shouldn't print out anything */
  27. print $obj->;Bar;    /* Shouldn't print out anything */
  28. print $obj->;Foo;    /* Shouldn't print out anything */
  29. $obj->;printHello(); /* Should print */

  30. $obj = new MyClass2();
  31. print $obj->;Hello;  /* Shouldn't print out anything */
  32. print $obj->;Bar;    /* Shouldn't print out anything */
  33. print $obj->;Foo;    /* Shouldn't print out anything */
  34. $obj->;printHello();
  35. ?>;
  36. 3。引入Private和 protected 方法
  37. Example
  38. <?php
  39. class Foo {
  40.    private function aPrivateMethod() {
  41.        echo "Foo::aPrivateMethod() called.\n";
  42.    }

  43.    protected function aProtectedMethod() {
  44.        echo "Foo::aProtectedMethod() called.\n";
  45.        $this->;aPrivateMethod();
  46.    }
  47. }

  48. class Bar extends Foo {
  49.    public function aPublicMethod() {
  50.        echo "Bar::aPublicMethod() called.\n";
  51.        $this->;aProtectedMethod();
  52.    }
  53. }

  54. $o = new Bar;
  55. $o->;aPublicMethod();
  56. ?>;
  57. 4。引入抽象 Classes 和 Methods

  58. Example
  59. <?php
  60. abstract class AbstractClass {
  61.    abstract public function test();
  62. }

  63. class ImplementedClass extends AbstractClass {
  64.    public function test() {
  65.        echo "ImplementedClass::test() called.\n";
  66.    }
  67. }

  68. $o = new ImplementedClass;
  69. $o->;test();
  70. ?>;
  71. 5。接口(Interfaces)

  72. Example
  73. <?php
  74. interface Throwable {
  75.    public function getMessage();
  76. }

  77. class MyException implements Throwable {
  78.    public function getMessage() {
  79.        // ...
  80.    }
  81. }
  82. ?>;
  83. 6。 Class Type Hints
  84. 7。Final 成员和方法
  85. 8。Object Cloning
  86. 9。Destructors (类似JAVA,对操作错误后内存释放很有用)
  87. Example
  88. <?php
  89. class MyDestructableClass {
  90.    function __construct() {
  91.        print "In constructor\n";
  92.        $this->;name = "MyDestructableClass";
  93.    }

  94.    function __destruct() {
  95.        print "Destroying " . $this->;name . "\n";
  96.    }
  97. }

  98. $obj = new MyDestructableClass();
  99. ?>;
  100. 10。常量定义Constants
  101. <?php
  102. class Foo {
  103.    const constant = "constant";
  104. }

  105. echo "Foo::constant = " . Foo::constant . "\n";
  106. ?>;
  107. 11。抛出例外Exceptions (类似java,错误处理很方便)
  108. Example
  109. <?php
  110. class MyException {
  111.    function __construct($exception) {
  112.        $this->;exception = $exception;
  113.    }

  114.    function Display() {
  115.        print "MyException: $this->;exception\n";
  116.    }
  117. }

  118. class MyExceptionFoo extends MyException {
  119.    function __construct($exception) {
  120.        $this->;exception = $exception;
  121.    }

  122.    function Display() {
  123.        print "MyException: $this->;exception\n";
  124.    }
  125. }

  126. try {
  127.    throw new MyExceptionFoo('Hello');
  128. }
  129. catch (MyException $exception) {
  130.    $exception->;Display();
  131. }
  132. catch (Exception $exception) {
  133.    echo $exception;
  134. }
  135. ?>;
  136. Example
  137. <?php
  138. class Exception {
  139.    function __construct(string $message=NULL, int code=0) {
  140.        if (func_num_args()) {
  141.            $this->;message = $message;
  142.        }
  143.        $this->;code = $code;
  144.        $this->;file = __FILE__; // of throw clause
  145.        $this->;line = __LINE__; // of throw clause
  146.        $this->;trace = debug_backtrace();
  147.        $this->;string = StringFormat($this);
  148.    }

  149.    protected $message = 'Unknown exception';  // exception message
  150.    protected $code = 0; // user defined exception code
  151.    protected $file;    // source filename of exception
  152.    protected $line;    // source line of exception

  153.    private $trace;      // backtrace of exception
  154.    private $string;    // internal only!!

  155.    final function getMessage() {
  156.        return $this->;message;
  157.    }
  158.    final function getCode() {
  159.        return $this->;code;
  160.    }
  161.    final function getFile() {
  162.        return $this->;file;
  163.    }
  164.    final function getTrace() {
  165.        return $this->;trace;
  166.    }
  167.    final function getTraceAsString() {
  168.        return self::TraceFormat($this);
  169.    }
  170.    function _toString() {
  171.        return $this->;string;
  172.    }
  173.    static private function StringFormat(Exception $exception) {
  174.        // ... a function not available in PHP scripts
  175.        // that returns all relevant information as a string
  176.    }
  177.    static private function TraceFormat(Exception $exception) {
  178.        // ... a function not available in PHP scripts
  179.        // that returns the backtrace as a string
  180.    }
  181. }
  182. ?>;
  183. 12。允许静态成员变量和静态方法的初始化
  184. Example
  185. <?php
  186. class foo {
  187.    static $my_static = 5;
  188.    public $my_prop = 'bla';
  189. }

  190. print foo::$my_static;
  191. $obj = new foo;
  192. print $obj->;my_prop;
  193. ?>;
  194. Static Methods
  195. Example
  196. <?php
  197. class Foo {
  198.    public static function aStaticMethod() {
  199.        // ...
  200.    }
  201. }

  202. Foo::aStaticMethod();
  203. ?>;
  204. 13。Instanceof 关键字
  205. Example
  206. <?php
  207. class baseClass { }

  208. $a = new baseClass;

  209. if ($a instanceof baseClass) {
  210.    echo "Hello World";
  211. }
  212. ?>;
  213. 14。Parameters that are passed by reference to a function may now have default values
  214. Example
  215. <?php
  216. function my_function(&$var = null) {
  217.    if ($var === null) {
  218.        die("$var needs to have a value");
  219.    }
  220. }
  221. ?>;

复制代码

摘自http://www.php.net/zend-engine-2.php

论坛徽章:
0
2 [报告]
发表于 2004-07-03 00:19 |只看该作者

面向对象的Php5和新的Zend2.0引擎

先看看phpe.net上的文章把

论坛徽章:
0
3 [报告]
发表于 2004-07-03 00:22 |只看该作者

面向对象的Php5和新的Zend2.0引擎

变得像个小java一样,感觉上private和Protected设置了以后调试程序就比较麻烦了

论坛徽章:
0
4 [报告]
发表于 2004-07-03 13:02 |只看该作者

面向对象的Php5和新的Zend2.0引擎

php是不是和java重复了?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP