I'm converting an old plugin from PHP5 -> PHP8 and I came across this check, which caused an error when executing:
Notice: Accessing static property PropertySearch::$listing_details_template_name as non static
I found online that this can be fixed, by using self
instead of $this
to access a static member. However, when I pass self
to the is_page
Wordpress function, it returns false
, where-as when I pass $this
, it returns true
.
From what I can tell, both of these contain the exact same string value.
self::$listing_details_template_name
$this->listing_details_template_name
One thing I noticed while debugging, is that upon entering the
is_page
function, the$page
variable is NULL when using$this->listing_details_template_name
.When entering the
is_page
function withself::$listing_details_template_name
, the$page
variable contains the name of the template.
Ultimately, I'm trying to find out why this is happening, and how I can pass the static member to is_page
to get the expected result of true
.
global $wp_query;
if (is_page(self::$listing_details_template_name)) {
echo nl2br ("[DEBUG] - self2 = " . self::$listing_details_template_name . " \r\n");
}
if (is_page($this->listing_details_template_name)) {
echo nl2br ("[DEBUG] - this2 = " . self::$listing_details_template_name . " \r\n");
}
===
For the time being, I've added this to the class-wp-query.php
file of WordPress.
if ($page = 'property-search-single-property-details-template.php') {
$page = NULL;
}
However, this is just a bandaid so I can continue debugging. I would love a solution that does NOT involve me modifying the core WordPress files. Or at the very least, some further understanding of why this is happening.
===
class PropertySearch {
private static $listing_details_template_name;
public function __construct() {
if (self::IsSingleProperty()) {
self::$listing_details_template_name = 'property-search-single-property-details-template.php';
} else {
self::$listing_details_template_name = 'property-search-details-template.php';
}
public function PropertySearchListingDetails() {
global $wp_query;
if (is_page(self::$listing_details_template_name)) {
if (!empty($wp_query->query_vars['building'])) {
return json_decode((string) $this->SyndicationAPI->getDetails($wp_query->query_vars['building']), null, 512, JSON_THROW_ON_ERROR);
} else {
return json_decode((string) $this->SyndicationAPI->getDetails(SINGLE_PROPERTY_ID), null, 512, JSON_THROW_ON_ERROR);
//return 'No listing found';
}
} else {
}
}
}
listing_details_template_name
property declared on the class, and how is its value set? Where and how is the class instantiated?is_page()
function with! is_page()
(not shown in the code example). It "works" but I would still like to know why there is a differnet result when passing$this
and orself
.self::$
instead of$this->
$listing_details_template_name
declared as static, and was passing$this
to theis_page()
function. Since I'm converting this code to PHP8, and am rather inexperienced with PHP, I assumed this was at least somewhat a correct method (at least back in PHP5ish when this code was written). I found online that I should be usingself
instead, hence this question. I'm still not 100% sure I understand why there are differnet output results fromis_page()
(per the video clip), but I'm getting the expected results now from the inversion which I made.self
and$this
, by the way. I guess theis_page()
function just handles static members and objects differently in PHP8, thus the differentiated results? I'm also guessing that this was not the case in older versions of PHP, which is why the code was originally written with an object identifier$this
instead.