dsideal2015 2016-01-21 18:31
浏览 34

如何将一个方法的结果存储在一个变量中,然后我可以在PHP的类外部使用它?

The structure in page-bulletins.php:

$args = array(
    'post_type' => 'post',
    'posts_per_page' => 3,
    'paged' => $paged
);

 $query = new WP_Query($args); if( $query->have_posts() ) :
     while( $query->have_posts() ) : $query->the_post();
         <!-- some code -->
     endwhile;
     // call the function 'posts_per_page' in the class 'cn_pages' using the value defined with the 'posts_per_page' key in '$args' above
     cn_pages::posts_per_page($args['posts_per_page']);
     // then call this function
     pass_page_info();
 endif;

The code in functions.php:

class cn_pages {
    function posts_per_page($postsperpage) {
        return $postsperpage;
    }
}

I have another function defined outside of the class cn_pages that needs the result of the posts_per_page method in order to calculate how many total pages there are.

How can I store the result of this method into a variable that I can access outside of the class where the method is defined?

  • 写回答

3条回答 默认 最新

  • dongshadu2546 2016-01-21 18:36
    关注

    What you can do is the following:

    $cnpages = new cn_pages();
    $cnpages->posts_per_page($args['posts_per_page']);
    

    And this should return you the value of the function.

    评论

报告相同问题?

悬赏问题

  • ¥15 对于知识的学以致用的解释
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败