引用PHP对象键作为函数参数

时间:2015-04-17 15:51:10

标签: php

这可能是不可能的,但不管怎么说都很好。

我正在寻找一种方法来运行一个在我写的自定义类中重复的过程。

其中一个功能参数目标需要引用 $ values 中的对象键,因此在下面的代码段中,它与 $ value相关 - >目标

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 );
    }
}

我知道此代码目前已损坏......

可以这样做吗?

修改

参考$ value->目标,其中$ value是下面的对象示例,我正在尝试访问'评论',所以$ value->评论:

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 );
}

3 个答案:

答案 0 :(得分:2)

不完全确定你的问题是什么,但......就像这样?

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

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

foo($bar, 'baz');

请参阅variable variables in PHP

答案 1 :(得分: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 : '';
   }
}

答案 2 :(得分: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的条目中找到obejct那么你需要这样做(没有循环)。

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

或先检查:

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