2011-12-12 70 views

回答

1

如果你正在谈论的对象抓空值,在实体写一个方法

<?php 
// Entities/SomeEntity.php 

class Foo 
{ 
    private $a; 

    private $b; 

    // ... 
    // Your getters and setters are here 
    // ... 

    public function myNullFunction() 
    { 
     if($this->a === null AND $this->b !== null) 
     { 
      return $this->b; 
     } 
     elseif($this->b === null && $this->a !== null) 
     { 
      return $this->a; 
     } 
     else 
     { 
      // ... Do something if both are null 
     } 
    } 
} 

然后,您可以使用此功能时,你已加载的对象(S)

$foo = $some_repository->getFooObject(); 

// The function returning a value that is a or b 
$bar = $foo->myNullFunction();