0

Most of (if not all) the is_* methods in the WP_Query class have a matching public property and the methods appear to just return the property e.g:

public function is_search() {
    return (bool) $this->is_search;
}

What is the purpose of the method when the property is already set? Are there use cases when you would use one over the other? Surely it's more efficient to check $wp_query->is_search over $wp_query->is_search()?

Edit: it would be nice to get an answer relating to the WP_Query methods. Whilst I appreciate the answers given they both refer to the global is_* functions rather than the class methods which is what I am referring to

1

While your example of is_search() is a rather trivial one and harder to justify, it's generally recommended in OOP styles to use getters and setters for things like this instead of public properties, for a number of reasons.

  • Encapsulation. The behavior of is_search() returns a boolean, but the method by which it determines whether or not a search is taking place could change in the future. Right now, the is_search variable is set earlier in the process and then this function returns it. A later version might do an immediate check to see if the current URL is a search one instead, depending on how the template system changes. For example, the property, though public, might change later. Right now it's stored as a boolean, but maybe something like "search_string" could be checked instead and a boolean then returned by is_search(). The output by the function hides the internal state of the variables being used, because the outside doesn't need to know those necessarily.

  • Public methods like is_search() are your interface, and the interface can thus remain more constant over time. Better to use something that's less likely to change (is_search() is only ever going to return a boolean, realistically).

  • Lots of other libraries and development systems know about getters and setters and work better when they exist. It's a common style choice, best to fit the mold.

  • Classes that inherit from your class can change how the fundamental basics work, and these functions allow them to use these to override behavior. Having to keep variables public and modify them internally is more difficult and error prone.

  • Functions, even object methods, can be passed around as callbacks to things like filter functions and such. Variables can't.

So, the real answer to your question is that the is_search variable probably should not be public. The method existing is relatively normal.

When coding, code defensively. Even if you can access the is_search variable directly, use the is_search() function instead. If the variable changes at all in the future, the function will change to accommodate it and still give you the correct output.

| improve this answer | |
0

Makes it easier for people getting started with development who are only used to using function template tags, i.e. is_single(), is_archive() etc without forcing them to take a crash course on OOP. That and it avoids another global declaration for $wp_query I suppose. On a semantic level, the code does look cleaner imo if you're using template tags along with have_posts(), the_post etc in templates rather than mixing in checks against $wp_query.

Edit: Regarding efficiency, my C is pretty terrible so I can't definitively look at the interpreter and see how a function call compares to accessing a public property but I bet if you benchmarked it that the results would be negligible.

| improve this answer | |
  • You'd still need to declare $wp_query as a global to use $wp_query->is_search() - I'm thinking more about in hooks i.e parse_query when the query is passed through by reference anyway – Andy Jan 8 '15 at 17:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.