2015-04-17 83 views
0

這可能是不可能的,但是知道這兩種方法都是很好的。引用PHP對象鍵作爲函數參數

我正在尋找使用一種方法來運行一個在我寫的自定義類中重複的過程。

一個函數的參數的,目標,需要參照的對象鍵在$值,所以在下面的代碼段,其涉及$值 - >目標

public function format_array($post_id, $object, $values, Target, $meta_value) { 
    $array = (array) $object; 
    $feed_values = array(); 

    if (!empty($array)) { 
     foreach ($values as $value) { 
      $feed_values[] = $value->Target; 
     } 
    } 

    unset($array); 

    $db_values = get_post_meta($post_id, $meta_value, true); 

    $result = array_diff($feed_values, $db_values); 

    if (!empty($result)) { 
     $this->update_post_meta($post_id, $meta_value, $feed_values); 
    } 
} 

我知道這個代碼是目前破...

可以這樣做?

編輯

參考值 - $>目標,其中$值低於對象的實例,我試圖訪問 '評論',所以$值 - >註釋:

object(stdClass)#411 (33) { 
    ["Facilities"]=> 
    object(stdClass)#413 (1) { 
    ["FacilityInfo"]=> 
    array(6) { 
     [0]=> 
     object(stdClass)#414 (4) { 
     ["ID"]=> 
     string(21) "xxxxx" 
     ["PropertyID"]=> 
     string(21) "xxxxx" 
     ["Name"]=> 
     string(5) "xxxxx" 
     ["Comment"]=> 
     string(9) "xxxxx" 
     } 
     [1]=> 
     object(stdClass)#415 (4) { 
     ["ID"]=> 
     string(21) "xxxxx" 
     ["PropertyID"]=> 
     string(21) "xxxxx" 
     ["Name"]=> 
     string(15) "xxxxx" 
     ["Comment"]=> 
     string(20) "xxxxx" 
     } 
    } 
    } 
    ["Photos"]=> 
    object(stdClass)#420 (1) { 
    ["PhotoInfo"]=> 
    array(5) { 
     [0]=> 
     object(stdClass)#421 (6) { 
     ["ID"]=> 
     string(21) "xxxxx" 
     ["PropertyID"]=> 
     string(21) "xxxxx" 
     ["MainPhoto"]=> 
     bool(true) 
     ["Name"]=> 
     string(8) "xxxxx" 
     ["Type"]=> 
     string(5) "Photo" 
     ["PhotoUrl"]=> 
     string(94) "xxxxxx" 
     } 
     [1]=> 
     object(stdClass)#422 (6) { 
     ["ID"]=> 
     string(21) "xxxxx" 
     ["PropertyID"]=> 
     string(21) "xxxxx" 
     ["MainPhoto"]=> 
     bool(false) 
     ["Name"]=> 
     string(2) "xxxxx" 
     ["Type"]=> 
     string(5) "Photo" 
     ["PhotoUrl"]=> 
     string(94) "xxxxxxx" 
     } 
    } 
    } 
} 
我的工作方法,目前的樣子

例子,但我想,以防止其重新使用此爲3個不同的值:

$array = (array) $letmc->Facilities; 
$comments = array(); 

if (!empty($array)) { 
    foreach ($letmc->Facilities->FacilityInfo as $Facility) { 
     $comments[] = $Facility->Comment; 
    } 
} 

unset($array); 

$property_comments = get_post_meta($post_id, 'property_comments', true); 

$result = array_diff($comments, $property_comments); 

if (!empty($result)) { 
    $this->update_post_meta($post_id, 'property_comments', $comments); 
} 

$array = (array) $letmc->Photos; 
$photos = array(); 

if (!empty($array)) { 
    foreach ($letmc->Photos->PhotoInfo as $Photo) { 
     $photos[] = $Photo->PhotoUrl; 
    } 
} 

unset($array); 

$property_photos = get_post_meta($post_id, 'property_photos', true); 

$result = array_diff($photos, $property_photos); 

if (!empty($result)) { 
    $this->update_post_meta($post_id, 'property_photos', $photos); 
} 
+0

對象的'Target'酒店有'public'接入,讓你從它的類定義之外訪問它。一個更好的選擇是創建一個訪問器公共方法來獲得該值,所以,你會有這樣的東西:'$ value-> getTarget()'; – NaijaProgrammer

+0

你可以使用'__get()'魔術方法來檢索變量類成員:http://stackoverflow.com/questions/4713680/php-get-and-set-magic-methods – CD001

+0

@NaijaProgrammer你們中的任何一個都可以實現你的答案,我會標記正確...我不完全知道如何解決方案 –

回答

2

不完全確定你的問題是什麼,但是......像這樣?

function foo($obj, $property) { 
    echo $obj->$property; 
} 

$bar = new stdClass; 
$bar->baz = 42; 

foo($bar, 'baz'); 

請參閱variable variables in PHP

+0

我無法得到這個工作,對不起 –

+0

在我之前投票給@deceze,並且是正確的。 – DanielM

+0

@SamHolguin如果這不起作用(應該是),你可以嘗試在函數foo()中調用屬性名時使用大括號括起來......它不應該是必須的,但它可以確保' $ property'在被調用之前被評估:ie'echo $ obj - > {$ property};' – CD001

0

AS CD001,建議您可以使用神奇的方法__get()。例如,在其屬性您試圖訪問其中一個類,你可以這樣做:

class Foo 
{ 
    protected $prop; //property you wish to access outside of the class 

    //magic method 
    public function __get($val) 
    { 
     return isset($this->$val) ? $this->$val : ''; 
    } 
} 
+0

爲什麼這個被拒絕? –

+0

這不行,'_get'接受一個參數。爲什麼不只是做一個實際的獲取方法 – DanielM

+0

你能擴展這個@DanielM嗎? –

0

更新的問題,使這個漂亮的和明確的:)

public function format_array($post_id, $object, $values, $target, $meta_value) { 

    ... 

    if (!empty($array)) { 
     foreach ($values as $value) { 
      $feed_values[] = $value->$target; 
     } 
    } 

    ... 
} 

你不妨檢查在$target存在雖然第一:

public function format_array($post_id, $object, $values, $target, $meta_value) { 

    ... 

    if (!empty($array)) { 
     foreach ($values as $value) { 
      if(property_exists($value, $target) { 
       $feed_values[] = $value->$target; 
      } 
     } 
    } 

    ... 
} 

潛力編輯:

我想也許我們看着錯誤的東西,如果你想在FacilityInfo的輸入處輸入對象,那麼你需要這樣做(沒有循環)。

if (!empty($array)) { 
    $feed_values[] = $values->$target; 
} 

或首先要檢查:

if (!empty($array) && property_exists($values , $target)) { 
    $feed_values[] = $values->$target; 
} 
+0

但是,然後調用函數,我必須使用:format_array($ post_id,$ letmc-> Facilities,$ letmc-> Facilities-> FacilityInfo,'Comment','property_comments');吐出一個錯誤?對不起,不知道如何格式化代碼在評論部分 –

+0

我已經添加了我的工作代碼的外觀,代碼我想創建一個函數:) –

+0

什麼是錯誤? – DanielM