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 ) : ?>
Firstly you should understand the following. In PHP and many other languages we have the following types of entites:
The ->
allows you to access a method or value within an object, the same way that []
allows you to access values within an array.
A class is like a box, and within that box there is a lot of items, and each item can interact with each other as they are within the same box.
For example:
class Box
{
function firstItem()
{
}
function secondItem()
{
}
}
The above is what we call a class. It's basically a structured piece of code that does not really do anything until it becomes an object.
The object is created by using the new
keyword, which instantiates a class and creates an objects from it.
$box = new Box;
Now the above $box
, which is an object created from the Box class, has methods inside, such as firstItem()
.
These are just like functions apart from within them we have another variable called $this
and this is used to access other methods within that object.
Now to access the methods from outside the objects you have to use the operator described in your question.
$box->firstItem();
The operator ->
will allow you to execute the method from the variable $box
.
-> is the used to access methods and attributes of an object. See the PHP manual on classes and objects.
It is how PHP handles objects. Essentially, $wp_query
is an object that has methods (functions) and attributes that can be accessed through the ->
characters.
PHP didn't start with objects so you see it now as sort of an afterthought. I find ->
to be a messy way to handle it, compared to say Ruby, which was built with objects from the foundation.
You can find more at: http://php.net/manual/en/language.oop5.php
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.
<?php
class Main{
private $name = 'My Name is Febri.<br/>';
private function print_name(){
echo $this -> name;
}
}
class Descend extends Main{
function print(){
$this -> print_name();
}
}
$try = new Descend;
$try -> print();
echo $try -> name;
?>
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.
$object->property
,$object->method()
... properties are basically class/object variables, whilst methods are class/object functions. – t31os