Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

syntax

oop

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 ) : ?>   
like image 693
saltcod Avatar asked Dec 21 '10 18:12

saltcod


People also ask

What is -> mean in PHP?

The object operator, -> , is used in object scope to access methods and properties of an object. It's meaning is to say that what is on the right of the operator is a member of the object instantiated into the variable on the left side of the operator.

What does == and === mean in PHP?

== Operator: This operator is used to check the given values are equal or not. If yes, it returns true, otherwise it returns false. Syntax: operand1 == operand2. === Operator: This operator is used to check the given values and its data type are equal or not. If yes, then it returns true, otherwise it returns false.

What is :: VS --> in PHP?

Simply put, :: is for class-level properties, and -> is for object-level properties.


4 Answers

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

like image 154
Rafe Kettler Avatar answered Oct 19 '22 05:10

Rafe Kettler


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.

like image 45
RobertPitt Avatar answered Oct 19 '22 04:10

RobertPitt


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

like image 24
dkinzer Avatar answered Oct 19 '22 04:10

dkinzer


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

like image 33
Andrew Sledge Avatar answered Oct 19 '22 03:10

Andrew Sledge