This question already has an answer here:
What does ->
mean/refer to in PHP?
In the following from WordPress, I know what the if
statement does, for example, but what does the ->
do?
<?php if ( $wp_query->max_num_pages > 1 ) : ?>
This question already has an answer here:
What does ->
mean/refer to in PHP?
In the following from WordPress, I know what the if
statement does, for example, but what does the ->
do?
<?php if ( $wp_query->max_num_pages > 1 ) : ?>
From the example above, we can not call a function which is a private print_name method. In addition, we also can not call the name variable which is set as private property.
This is very simple to understand.
In PHP we use -> to access a method/property defined inside a class.
So in your case ($wp_query->max_num_pages), you are trying the get the value of max_num_pages which is a variable of $wp_query class.
$wp_query object information defining the current request, and then $wp_query determines what type of query it's dealing with (possibly a category archive, dated archive, feed, or search), and fetches the requested posts. It retains a lot of information on the request, which can be pulled at a later date.
->
accesses a member of an object. So$wp_query->max_num_pages
is accessing the fieldmax_num_pages
in the object$wp_query
. It can be used to access either a method or a field belonging to an object, and if you're familiar with C++ or Java, it's equivalent tomyObject.myField
Use
->
to access fields, methods in an object, it is parallel to[]
in array variables ($array['field']
is$object->field
). In WP you'll use it on$post
for example, since it is an object.It accesses the member of the object; $obj->prop accesses the "prop" property of whatever object is in the $obj variable.
In many other programming languages, a period is used for this purpose: obj.prop or obj.method(), for example.
$object->property
is used to access the property of any object.