2010-08-04 198 views
1

我有一個獲取String的對象。我想比較它與一個字符串,我不知道該怎麼做。 我的來源是類似的東西:將對象轉換爲字符串

$field = $this->form->getFieldset('profile'); 
$website = "Web site"; 

if ($field->label == $website){ 
    echo "good"; 
}else{ 
    echo "bad"; 
} 
+0

你得到一個錯誤?問題是什麼? – 2010-08-04 13:02:23

+0

使用strcmp(string str1,string str2)?! – 2010-08-04 13:07:24

+0

我可以使用strcmp,但$ field-> label是一個對象,我需要兩個字符串來使用這個方法,那麼我怎樣才能將它轉換成一個字符串? – juanasecas 2010-08-05 06:27:11

回答

1

真的不明白的問題,但你可以將其轉換爲使用get_object_vars數組。

這是假設$ field-> label實際上是一個屬性而不是對象?

$field = $this->form->getFieldset('profile'); 
$website = "Web site"; 
$array = get_object_vars($field); 

if ($array['label'] == $website) { 
    echo 'Hoorah!'; 
} 
0

如果將gettype($現場>標籤)的結果是一個對象,你必須實現魔術方法__toString()