2014-01-10 40 views
0

我有一個樣品類這樣有效的方式

class foo{ 
    private $a = null; 
    private $b = null; 
    private $c = null; 
    private $d = null; 

function a(){$a = ''; return $this; } 
function b(){$b = '0'; return $this; } 
function c(){$c = 'x'; return $this; } 
function d(){$d = 'false'; return $this; } 

function z(){ 
    //I would like to only echo properties that are not null. 
    //and I am looking for better alternative than using if/else approach. 
    } 
} 

這是我想調用的方法。

$o = new foo(); 
$o->a() 
    ->b() 
    ->c() 
    ->d() 
    -z(); 

因此,現在方法z()應該回顯非空的屬性。由於我有很多屬性,使用if/else將被證明是困難/冗長的。

+1

一種方法是將您的屬性轉換爲該類內的本地數組。好處是可以使用array_filter(),它將返回一個數組減去原始數組中的空鍵。 http://us2.php.net/manual/en/function.array-filter.php – Rottingham

+0

@kuroineko http:// stackoverflow。com/questions/21050862 /高效的方法來檢查堆棧的非空屬性#comment31651251_21050976 – user2679413

回答

0

在你的類,你可以使用get_object_vars()獲得當前範圍內可用的屬性:

function z() 
{ 
    foreach (get_object_vars($this) as $key => $value) 
    { 
    if (is_null($value)) 
    { 

    } 
    else 
    { 

    } 
    } 
} 
+0

這是一個非常好的方法。謝謝。但是,正如我試過的那樣,它考慮到了在構造函數中做出的任何賦值屬性。換句話說,在這種情況下,'$ this-> a'作爲一個屬性需要'function __construct($ foo){$ this-> a = $ foo;}'。 – user2679413

+0

@ user2679413我不確定你想要什麼,如果你需要檢查默認值,你可以使用'get_class_vars(foo)'代替。 – jeroen

0

您可以通過$this迭代,這將給你通過foreach ($this as $key=>$value)屬性名稱和值 - 在那裏你可以輕鬆使用empty()

請參閱this working example

public function echoAllProperties() { 
    foreach ($this as $key=>$value) { 
     if (!empty($value)) 
      echo $key." => ".$value."\n"; 
    } 
} 
0
$o = new foo(); 

    $class_vars = get_class_vars(get_class($o)); 

    foreach ($class_vars as $name => $value) { 
     echo "$name : $value\n"; 
    } 
0

編輯:

看來你使用的是自己的PHP對象的屬性是什麼定義,所以讓我給你我的。

PHP文檔對於主題有些模糊,有些功能如get_object_vars有點誤導,因爲它們將獲取對象屬性而不是變量。

據我的理解,是什麼它歸結爲是:PHP使得性能之間的區別(在編譯時已知),並動態特性(即設置沒有那些已被宣佈)。

它與設置屬性有關(因爲它會創建一個動態的屬性),但重點是要知道屬性是否在類定義內部聲明。

如果實際上你感興趣的類和對象屬性之間的這種差異,你可能會發現API的快樂,儘管如果你問我,我發現它使用起來極其尷尬。

此API的主要優點是,您可以列出將從當前範圍(通常爲受保護或私有成員)無法訪問的屬性。
如果您只是使用get_object_varsget_class_vars函數,則會忽略所有私有/受保護屬性,除非您在對象本身內定義一個方法(繼承調用get_class_vars的類將允許您獲取受保護的屬性,但不能訪問私有那些)。

function list_properties($obj, $filter = null) 
{ 
    if ($filter === null) $filter = ReflectionProperty::IS_STATIC 
            | ReflectionProperty::IS_PUBLIC 
            | ReflectionProperty::IS_PROTECTED 
            | ReflectionProperty::IS_PRIVATE; 

    $spy = new ReflectionClass ($obj); 
    $class_properties = $spy->getProperties($filter); 

    echo "class:<br />"; 
    foreach ($class_properties as $prop) 
    { 
     $name = $prop->getName(); 
     $p = $spy->getProperty ($name); 
     $p->setAccessible(true); 
     $value = $p->getValue($obj); 
     echo "$name ("; var_dump($value); echo ")<br />"; 
    } 
    echo "<br />object:<br />"; 
    foreach ($obj as $property => $value) 
     { echo "$property ("; var_dump($value); echo ")<br />"; } 
} 

class foo { 
    protected $class_protected; 
    private $class_private; 
    public $class_public; 
    private static $static_protected = "azerty"; 
    private static $static_private = "qsdfgh"; 
    public static $static_public = "wxcvbn"; 
} 


$v = new foo(); 
$v->dynamic = "xxx"; 
list_properties($v); 

// the ReflectionClass filter flags semantic is broken, 
// so filtering becomes soon a cruel pain in the *ss 
/* 
* I leave it to the reader as an execrise to coax the bloody thing 
* into giving you only the static public class properties, for instance. 
*/ 
list_properties($v, ReflectionProperty::IS_STATIC|ReflectionProperty::IS_PUBLIC); 
+0

問題是在構造函數中初始化了vars。 – user2679413

+0

???它需要屬性和當前值。除了$ v,$ property和$ value之外,此代碼中沒有變量。 –

+0

我在說,無論在構造函數裏面定義的是什麼屬性,我都不想在函數中使用 – user2679413