What does "->" mean/refer to in PHP?

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 ) : ?>   

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

-> accesses a member of an object. So $wp_query->max_num_pages is accessing the field max_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 to myObject.myField

Solution 2

Firstly you should understand the following. In PHP and many other languages we have the following types of entites:

  • Variables
  • Arrays
  • Objects

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.


The operator -> will allow you to execute the method from the variable $box.

Solution 3

It’s like the period (.) in JavaScript and Java. It is just a simple access operator.

Solution 4

-> is the used to access methods and attributes of an object. See the PHP manual on classes and objects.

Solution 5

It accesses a member of the object on the left with the name on the right.

Solution 6

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.

Solution 7

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

Solution 8

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.

Solution 9

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.

Solution 10

-> is basically used to access a filed of an object. Analogioous to attributes in Java.

for eg.

class Student {
String name;
int rollno;

Student.name accesses the name of a given student object.

Solution 11

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.

Solution 12

$object->property is used to access the property of any object.

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply