属性OOP的值错误

时间:2015-02-01 18:01:25

标签: php wordpress oop

我无法从以下OOP类

中设置的属性中获取正确的值
class person {

    private $defaults = [
        'post_types'        => [],
        'number'            => 1,
        'use_referrers'     => true,
        'in_same_term'      => false,
        'include'           => [],
        'exclude'           => [],
        'taxonomy'          => 'category',
        'include_children'  => true,
        'previous'          => true,
        'boundary_posts'    => false,
        'anchor_text'       => '%anchor',
        'post_link_text'    => '%text',
        'separator'         => '</br>',
        'span_separator'    => '</br>',
        'span_text_prev'    => 'Older post: ',
        'span_text_next'    => 'Newer post: ',
        'span_text_oldest'  => 'Oldest post: ',
        'span_text_newest'  => 'Newest post: ',

    ];

    public $post;
    public $is_referred_post    = false;
    public $is_author_referrer  = false;
    public $is_search_referrer  = false;
    public $is_tax_referrer     = false;


    public function post( $post ) {

        $this->post = get_queried_object();
        return $this->post;

    }

    public function referrers_defaults() {

        //Set is_author_referrer if query_var aq is set
        if( isset( $_GET['aq'] ) ) {

            $this->is_author_referrer = true;

        }

        //Set is_search_referrer if query_var sq is set
        if( isset( $_GET['sq'] ) ) {

            $this->is_search_referrer = true;

        }

        //Set is_tax_referrer if query_var tq is set
        if( isset( $_GET['tq'] ) ) {

            $this->is_tax_referrer = true;

        }

        //Set is_referred_post if the current single post has one of the query_vars in the URL
        if( $this->is_author_referrer || $this->is_search_referrer || $this->is_tax_referrer ) {

            $this->is_referred_post = true;

        }
        return $this;
    }

}
$a = new person();
?><pre><?php var_dump($a); ?></pre><?php    

这是我从var_dump( $a );

获得的
object(person)#569 (6) {
  ["defaults":"person":private]=>
  array(18) {
    ["post_types"]=>
    array(0) {
    }
    ["number"]=>
    int(1)
    ["use_referrers"]=>
    bool(true)
    ["in_same_term"]=>
    bool(false)
    ["include"]=>
    array(0) {
    }
    ["exclude"]=>
    array(0) {
    }
    ["taxonomy"]=>
    string(8) "category"
    ["include_children"]=>
    bool(true)
    ["previous"]=>
    bool(true)
    ["boundary_posts"]=>
    bool(false)
    ["anchor_text"]=>
    string(7) "%anchor"
    ["post_link_text"]=>
    string(5) "%text"
    ["separator"]=>
    string(5) "
"
    ["span_separator"]=>
    string(5) "
"
    ["span_text_prev"]=>
    string(12) "Older post: "
    ["span_text_next"]=>
    string(12) "Newer post: "
    ["span_text_oldest"]=>
    string(13) "Oldest post: "
    ["span_text_newest"]=>
    string(13) "Newest post: "
  }
  ["post"]=>
  NULL
  ["is_referred_post"]=>
  bool(false)
  ["is_author_referrer"]=>
  bool(false)
  ["is_search_referrer"]=>
  bool(false)
  ["is_tax_referrer"]=>
  bool(false)
}

我的问题是本节

  ["post"]=>
  NULL
  ["is_referred_post"]=>
  bool(false)
  ["is_author_referrer"]=>
  bool(false)
  ["is_search_referrer"]=>
  bool(false)
  ["is_tax_referrer"]=>
  bool(false)

['post']不应该是NULL,它应该提供当前帖子的值。如果我做var_dump($a->post());我确实得到了post对象。

另外,假设在网址?aq=2中设置了以下变量,["is_author_referrer"]["is_referred_post"]应该返回true,但他们会尽可能返回false请参阅var_dump()

为什么没有为这些属性设置正确的值。我在OOP中缺少什么

2 个答案:

答案 0 :(得分:2)

$a->post()调用post 方法,这不是属性。当您调用此方法时,该方法将从某处获取值(get_queried_object)。返回此值,但也存储在post属性中。我认为这就是你认为应该填补财产的原因。但是在调用该方法之前,该属性将保持为空。

我不知道最好的解决方案,因为我不知道你的应用程序的其余部分(比如,这个帖子对象是如何构建的,什么是get_queried_object以及从哪个点开始可用,等等)。不过,我希望我能为您找出问题,这将有助于您找到正确的解决方案。

一个可能的想法是从构造函数中设置$post,但就像我说的那样,这取决于其他因素是否可行:

public function __construct() {
    $this->post = get_queried_object();
}

创建新对象时会调用构造函数__construct

答案 1 :(得分:0)

我对构造函数有误解,感谢@GolezTrol我的工作正常

这是现阶段的最终工作版本

class person {

    private $defaults = [
        'post_types'        => [],
        'number'            => 1,
        'use_referrers'     => true,
        'in_same_term'      => false,
        'include'           => [],
        'exclude'           => [],
        'taxonomy'          => 'category',
        'include_children'  => true,
        'previous'          => true,
        'boundary_posts'    => false,
        'anchor_text'       => '%anchor',
        'post_link_text'    => '%text',
        'separator'         => '</br>',
        'span_separator'    => '</br>',
        'span_text_prev'    => 'Older post: ',
        'span_text_next'    => 'Newer post: ',
        'span_text_oldest'  => 'Oldest post: ',
        'span_text_newest'  => 'Newest post: ',

    ];

    public $current_post;
    public $is_referred_post    = false;
    public $is_author_referrer  = false;
    public $is_search_referrer  = false;
    public $is_tax_referrer     = false;

    public function __construct() {

        $this->init();

    }

    public function init() {

        $this->getCurrentpost();
        $this->referrers();

    }

    public function getCurrentpost() {

        return $this->current_post = get_queried_object();

    }

    public function referrers() {

        //Set is_author_referrer if query_var aq is set
        if( isset( $_GET['aq'] ) ) {

            $this->is_author_referrer = true;

        }

        //Set is_search_referrer if query_var sq is set
        if( isset( $_GET['sq'] ) ) {

            $this->is_search_referrer = true;

        }

        //Set is_tax_referrer if query_var tq is set
        if( isset( $_GET['tq'] ) ) {

            $this->is_tax_referrer = true;

        }

        //Set is_referred_post if the current single post has one of the query_vars in the URL
        if( $this->is_author_referrer || $this->is_search_referrer || $this->is_tax_referrer ) {

            $this->is_referred_post = true;

        }

    }

}
$a = new person();