Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "->" mean? [duplicate]

Tags:

syntax

php

Possible Duplicates:
Reference - What does this symbol mean in PHP?
In, PHP, what is the “->” operator called and how do you say it when reading code out loud?

This is a really newbie question, so apologies in advance, but I've seen -> used several times in example code, but I can't seem to find any explanation in online tutorials for what it does. (Mainly because Google ignores it as a search term - doh!)

Here's an example that confuses me:

<?php
class customException extends Exception
{
public function errorMessage()
{
//error message
$errorMsg = 'Error on line '.$this->getLine().' in '.$this->getFile()
.': <b>'.$this->getMessage().'</b> is not a valid E-Mail address';
return $errorMsg;
}
}

$email = "[email protected]";

try
  {
  //check if
  if(filter_var($email, FILTER_VALIDATE_EMAIL) === FALSE)
    {
    //throw exception if email is not valid
    throw new customException($email);
    }
  //check for "example" in mail address
  if(strpos($email, "example") !== FALSE)
    {
    throw new Exception("$email is an example e-mail");
    }
  }

catch (customException $e)
  {
  echo $e->errorMessage();
  }

catch(Exception $e)
  {
  echo $e->getMessage();
  }
?> 

What is going on in lines such as echo $e->errorMessage();? It looks like its passing the variable $e to the function errorMessage(), but if so, why not just do it in the more traditional way?

Thanks for any help.

like image 557
Chuck Le Butt Avatar asked Apr 04 '11 20:04

Chuck Le Butt


People also ask

Does duplicate mean 2?

consisting of or existing in two identical or corresponding parts; double.

What does form in duplicate mean?

If a form is in duplicate, an exact copy has been made of it: The application has to be completed in duplicate.

What does it mean to send in duplicate?

1 : so that there are two copies We were required to fill out the paperwork in duplicate. 2 : with an exact copy Please send the contract in duplicate.

Does duplicate mean copy?

Duplicate creates a copy of an item in the same location as the original. Copying (or “Copy To”) creates a copy of an item in a different location that you specify.


2 Answers

It's used in object oriented programming to denote object->property

echo "$foo->bar" would echo the bar property of $foo

like image 96
Levi Hackwith Avatar answered Oct 22 '22 06:10

Levi Hackwith


No, it is not a scope resolution operator. :: (also called Paamayim Nekudotayim) is the scope resolution operator, see the manual.

No, it is not a function. This is object oriented programming, so the correct term is method.

No, it is not a property. Again, it's a method.

I am not aware of any terminology for the -> construct. It is used to either call methods or to access properties on an instance of a class. On an object. I suppose you could refer to it as the "instance operator".

In your specific case it's a method call. The errorMessage method is being called on your $e object, which is an instance of the customException class.

like image 45
igorw Avatar answered Oct 22 '22 08:10

igorw