Php OOP属性的值错误
我无法从以下OOP类的属性集获取正确的值Php OOP属性的值错误,php,wordpress,oop,Php,Wordpress,Oop,我无法从以下OOP类的属性集获取正确的值 class person { private $defaults = [ 'post_types' => [], 'number' => 1, 'use_referrers' => true, 'in_same_term' => false, 'include' =&g
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
我的问题是这个部分
public function __construct() {
$this->post = get_queried_object();
}
['post']
不应为NULL
,它应给出当前post中的值。如果我做了var_dump($a->post())代码>我确实得到了post对象
另外,假设在URL?aq=2
、[“is_author\u referer”]
和[“is_refered\u post”]
中设置了以下变量,它们应该返回true
,但它们返回false
,正如您从var\u dump()
中可以看到的那样
为什么没有为这些属性设置正确的值。我在OOP中缺少什么?$a->post()
调用post
方法,该方法不是属性。调用此方法时,该方法将从某处获取值(get\u queryed\u object
)。返回此值,但也存储在post
属性中。我想这就是为什么你认为该房产应该被填满的原因。但在调用该方法之前,该属性将保持null
我不知道最好的解决方案,因为我不知道应用程序的其他部分(比如,这个post对象是如何构造的,什么是get\u queryed\u对象
以及它从什么地方可用,等等)。尽管如此,我希望我为您指出了问题所在,这将帮助您找到正确的解决方案
一个可能的想法是从构造函数中设置$post
,但正如我所说的,这取决于其他因素是否有效:
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();
构造函数\uuu construct
在创建新对象时被调用。我对构造函数有一个误解,多亏了@GolezTrol,我才使事情正常运行
这是本阶段的最终工作版本
班级人员{
私人$defaults=[
“post_类型”=>[],
'编号'=>1,
'use_referers'=>true,
“在同一术语中”=>false,
'包括'=>[],
“排除”=>[],
“分类法”=>“类别”,
“包含子项”=>正确,
'previous'=>true,
“边界_帖子”=>错误,
“锚定文本”=>“%anchor”,
'post_link_text'=>'%text',
“分隔符”=>“”,
“span_分隔符”=>“”,
“span_text_prev'=>“旧帖子:”,
“span_text_next'=>“更新的帖子:”,
“span_text_oldest'=>“最早的帖子:”,
“span_text_latest”=>“最新帖子:”,
];
公帑$现职;;
公共$is\u refered\u post=false;
public$is\u author\u referer=false;
public$is\u search\u referer=false;
公共$is\u tax\u referer=false;
公共函数构造(){
$this->init();
}
公共函数init(){
$this->getCurrentpost();
$this->referers();
}
公共函数getCurrentpost(){
返回$this->current_post=get_queryed_object();
}
公共函数引用器(){
//如果设置了查询变量aq,则Set是\u作者\u参考者
如果(isset($_GET['aq'])){
$this->is\u author\u referer=true;
}
//如果设置了查询变量sq,则设置为搜索引用
如果(isset($_GET['sq'])){
$this->is\u search\u referer=true;
}
//如果设置了查询变量tq,则设置为\u tax\u referer
如果(isset($_GET['tq'])){
$this->is\u tax\u referer=true;
}
//如果当前单个帖子在URL中有一个查询变量,则Set为\u refered\u post
如果($this->is_author_referer | |$this->is_search_referer | |$this->is_tax_referer){
$this->is\u refered\u post=true;
}
}
}
$a=新人();
a->post()
是一种方法。public$post对象中的code>是一个包含空值的属性,因为尚未使用/分配实际值对其进行初始化。这是一个很好的教训,告诉我们不要将属性命名为与方法相同的东西。将方法命名为类似于getPost()
,或将属性命名为类似于$personPost
,以避免混淆并使其更具可读性。我建议用前者来解释语义,我想我明白你的意思了。我会继续玩这个游戏
public function __construct() {
$this->post = get_queried_object();
}
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();