zoukankan      html  css  js  c++  java
  • 第二十一节:类与对象对象的比较类型约束

    PHP5中的对象比较要比PHP4中复杂,也比其它一般的面向对象语言复杂。

    当使用对比操作符(==)比较两个对象变量时,比较的原则:如果两个对象的属性和属性值都相等。而且两个对象是一个同一个类的实例,那么这两个对象变量相等。而如果使用全等操作符(===),这两个对象变量一定要指向某一个类的同一个实例(即同一个对象)。$o=new O(); $p=$o  $p=&$o  都是全等。

     1 <?php 
     2 function bool2str($bool)
     3 {
     4     if($bool === false){
     5         return 'FALSE';
     6         }else{
     7             return 'TRUE';
     8             }
     9     }
    10 function compareobjects(&$o1,&$o2)
    11 {
    12     echo 'o1==o2:'.bool2str($o1==$o2)."<br/>";
    13     echo 'o1!=o2:'.bool2str($o1!=$o2)."<br/>";
    14     echo 'o1===o2'.bool2str($o1===$o2)."<br/>";
    15     echo 'o1!==o2'.bool2str($o1!==$o2)."<br/>";
    16     }    
    17 class Flag
    18 {
    19     public $Flag;
    20     function Flag($flag = true) {
    21         $this->flag = $flag;
    22     }
    23 }    
    24 class OtherFlag
    25 {
    26     public $flag;
    27     function otherFlag($flag=true){
    28         $this->flag=$flag;
    29         }
    30     }
    31 $o= new Flag();
    32 $p= new Flag();
    33 $q= $o;
    34 $x=&$o;
    35 $r= new OtherFlag();
    36 echo "Two instances of the same class <br/>";
    37 compareObjects($o,$p);    
    38 /*
    39 Two instances of the same class 
    40 o1==o2:TRUE
    41 o1!=o2:FALSE
    42 o1===o2FALSE
    43 o1!==o2TRUE
    44 */
    45 echo " Two references to the same instance <br/>";
    46 compareobjects($o,$q);
    47 /*
    48 Two references to the same instance 
    49 o1==o2:TRUE
    50 o1!=o2:FALSE
    51 o1===o2TRUE
    52 o1!==o2FALSE
    53 */
    54 echo "\nInstances of two different classes\n";
    55 compareObjects($o, $r);
    56 /*Instances of two different classes 
    57 o1==o2:FALSE
    58 o1!=o2:TRUE
    59 o1===o2FALSE
    60 o1!==o2TRUE
    61 */
    62 compareObjects($o, $x);
    63 /*
    64 o1==o2:TRUE
    65 o1!=o2:FALSE
    66 o1===o2TRUE
    67 o1!==o2FALSE
    68 */
    69 ?>

    PHP 5 可以使用类型约束。函数的参数可以指定只能为对象(在函数原型里面指定类的名字),php 5.1 之后也可以指定只能为数组。 注意,即使使用了类型约束,如果使用NULL作为参数的默认值,那么在调用函数的时候依然可以使用NULL作为实参。 

    他这个规范其实有点奇怪,类型约束能详细约束,使用哪个类的实例为args,但是对于数组,只能约束到,他是一个数组。

     1 <?php 
     2 class MyClass{
     3  public $var='MyClass :: var';
     4  function bor(array $row=NULL){
     5      echo 'I am bor()'.$row;
     6      }    
     7     }
     8 class otherClass{
     9     // $args 只能是一个MyClass的对象
    10     function foo(MyClass $args=NULL){
    11         var_dump($args->bor(array('a','b','c','d','f')));
    12         }
    13     }    
    14 $myclass=new MyClass();
    15 $otherClass=new otherClass();
    16 $otherClass->foo($myclass);
    17 //如果默认参数是NULL 可以穿个NULL
    18 //$otherClass->foo(NULL);    
    19 ?>
  • 相关阅读:
    Account group in ERP and its mapping relationship with CRM partner group
    错误消息Number not in interval XXX when downloading
    错误消息Form of address 0001 not designated for organization
    Algorithm类介绍(core)
    梯度下降与随机梯度下降
    反思
    绘图: matplotlib核心剖析
    ORB
    SIFT
    Harris角点
  • 原文地址:https://www.cnblogs.com/saw2012/p/2909517.html
Copyright © 2011-2022 走看看