36
votes

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 ) : ?>   
12
@Jimmy the funniest thing is that your Google query returns nothing useful. You should probably work on your Google skillz before you pull this one againRafe Kettler
yeah, i realized that... but it is possible to get around getting NO resultsJimmy
@Jimmy LMGTFY is discouraged on SOGordon
$object->property, $object->method() ... properties are basically class/object variables, whilst methods are class/object functions.t31os
The "duplicate question" reference points to a page which links to this question. That's a bit awkward.Craig McQueen

12 Answers

33
votes

-> 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

26
votes

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.

$box->firstItem();

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

6
votes

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

5
votes

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

3
votes

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

3
votes

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.

2
votes

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

1
votes

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.

0
votes
<?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.

0
votes

-> 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.

0
votes

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.

0
votes

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