douping3860 2018-07-05 14:09
浏览 56
已采纳

类范围可访问的静态数组,试图推送它,但一直空着

class UpcomingEvents {

//Variable I'm trying to make accessible and modify throughout the class methods
private static $postObjArr = array();
private static $postIdArr = array(); 

private static $pinnedPost;  

//My attempt at a get method to solve this issue, it did not
private static function getPostObjArr() {
    $postObjArr = static::$postObjArr;
    return $postObjArr;
}

private static function sortByDateProp($a, $b) {
    $Adate = strtotime(get_field('event_date',$a->ID));
    $Bdate = strtotime(get_field('event_date',$b->ID));
    if ($Adate == $Bdate) {
        return 0;
    }
    return ($Adate < $Bdate) ? -1 : 1;
}

private static function queryDatesAndSort($args) {

    $postQuery = new WP_Query( $args );

    if( $postQuery->have_posts() ) {
        while( $postQuery->have_posts() ) {
            $postQuery->the_post();

            //Trying to push to the array, to no avail
            array_push(static::getPostObjArr(), get_post());
        }
    }

    //Trying to return the array after pushing to it, comes back empty
    return(var_dump(static::getPostObjArr()));

    //Trying to sort it
    usort(static::getPostObjArr(), array(self,'sortByDateProp'));  

    foreach (static::getPostObjArr() as $key => $value) {
        array_push(static::$postIdArr, $value->ID);
    }  

}
}

I'm trying to access $postObjArr within the class, and push to it with the queryDatesAndSort(); method. I've tried a couple of things, most recent being to use a get method for the variable. I don't want to make it global as it's bad practice I've heard. I've also tried passing by reference I.E

&static::$postObjArr;

But when it hits the vardump, it spits out an empty array. What would be the solution and best practice here? To allow the class methods to access and modify a single static array variable.

  • 写回答

1条回答 默认 最新

  • doupai6875 2018-07-05 14:51
    关注
    static::$postObjArr[] = get_post()
    

    I didn't think it would of made a difference, but it worked. Can you explain to me why that worked but array.push(); Did not?

    Arrays are always copy-on-write in PHP. If you assign an array to another variable, pass it into a function, or return it from a function, it's for all intents and purposes a different, new array. Modifying it does not modify the "original" array. If you want to pass an array around and continue to modify the original array, you'll have to use pass-by-reference everywhere. Meaning you will have to add a & everywhere you assign it to a different variable, pass it into a function, or return it from a function. If you forget your & anywhere, the reference is broken.

    Since that's rather annoying to work with, you rarely use references in PHP and you either modify your arrays directly (static::$postObjArr), or you use objects (stdClass or a custom class) instead which can be passed around without breaking reference.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!