Symbol "->" in php

Asked

Viewed 6,934 times

5

When I used Phpmailer I used (without knowing why) the symbol "->" in $mail->AddAdress(..., for example. Now removing a doubt in W3schools (http://www.w3schools.com/php/php_mysql_connect.asp) I saw that little arrow "->" again in$conn->connect_error.

What exactly does this "->" symbol mean?

(I tried to put it on google but it doesn’t even read the symbol as a string, it seems that it incorporates as a special character like +)

  • The symbol -> is restricted to objects, to reference objects of a given instance. As the above user just said, this already has answers in a previously asked question.

3 answers

10


The symbol is used to call a method or variable from a class. First of all you need to understand what a class is, if you’ve heard of OOP or of Object-oriented then you will understand the use of ->

In several programming languages we have classes and most behave similarly, in the case of PHP you can create a class like this:

class Enrique
{
    public function nome()
    {
        return 'Enrique';
    }
}

And call it that:

$test = new Enrique;
echo $test->nome(); //Irá exibir "Enrique" (sem aspas)

See that to call the method nome of the Enrique class it was necessary to use ->, this way you call the inner functions of the class.

You can also call the method within another method using the $this

class Enrique
{
    public function nome()
    {
        return 'Enrique ' . $this->sobrenome(); //Junta o sobrenome ao nome e joga no return
    }

    public function sobrenome()
    {
        return 'René';
    }
}

Using:

$test = new Enrique;
echo $test->nome(); //Irá exibir "Enrique René" (sem aspas)

Starting from PHP 5 are the inclusion of visibility, classes and methods Abstract and final, magical methods, interfaces, cloning and type induction.

PHP treats objects in the same way as references or manipulators, meaning that each variable contains a reference to an object rather than a copy of the entire object. Behold Objects and References

I put the manual in English, because the Portuguese can contain some flaws (as I mentioned here: The use_include_path parameter has been replaced by the flags parameter?)

The -> has the job of accessing methods or properties of a class, just as in Java and C# the equivalent would be .. The "likely" reason PHP does not use p . is because it is already used for another task in PHP, each language has its typing, but is very likely to always find something equivalent.

Java class

class Enrique {
    public String nome()
    {
        return 'Enrique';
    }
}

class Programa {
  public static void main(String[] args) {
    Enrique test = new Enrique();

    System.out.println("Saldo atual: " + test.nome());
  }
}

Completion

So the basics of what a class is, PHPmailer is a class (or rather a set of classes and other functions) and the link you have set is an example of mysqli which is a native PHP class for connecting to mysql, you can use both structural and object-oriented (classes), see that here started with new, when you see it in some code it will probably be a class:

<?php
$servername = "localhost";
$username = "username";
$password = "password";

// Create connection
$conn = new mysqli($servername, $username, $password);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
echo "Connected successfully";
?>

Class visibility

This is a feature that exists in several languages that use classes (there are exceptions like Python), the visibility is a keyword that goes ahead of the method or variable in the class and defines who can access it:

  • public is when it is available to be accessed by "anyone", for example sobrenome is accessible outside the class and inside, as in the example:

    class Enrique
    {
        public function nome()
        {
            return 'Enrique ' . $this->sobrenome(); //Junta o sobrenome ao nome e joga no return
        }
    
        public function sobrenome()
        {
            return 'René';
        }
    }
    
    $test = new Enrique;
    $test->sobrenome();
    
  • private is when the method or variable are accessible only within the class, if trying to access outside will cause a Exception, for example:

    class Enrique
    {
        public function nome()
        {
            return 'Enrique ' . $this->sobrenome(); //Aqui você pode acessar sobrenome normalmente
        }
    
        private function sobrenome()
        {
            return 'René';
        }
    }
    
    $test = new Enrique;
    $test->sobrenome(); //Aqui causa uma Exception
    

    Will issue this error:

    PHP Fatal error: Call to private method Enrique::surname() from context '' in /home/zy4mrI/Prog.php on line 16

  • protected is similar to private and causes the same error if accessed outside the class, except that the method or variable is accessible in all classes extending current class, including the parent class.

Read more on Classes and Objects Visibility

  • I had not seen your answer.. No mobile appears.

  • @emanuelsn no problem, you reported something that in my no, the name of the operator :) +1

5

The official name is Object Operator, but I see people calling it 'arrow', 'arrow' and 'arrow''.

This is an operator used to access the methods of an object.

For example, when you use $conn->connect_error, you are accessing the connect_error method of the $Conn object.

3

(T_OBJECT_OPERATOR) -> is a token used to access "nonstatic" methods of a class:

$this->prop (where prop is the name of the property).

This "arrow" and also other symbols, are inherited from the C/C++ and are used by most programming languages, including PHP and his younger brother Hack/HHVM

For those who program in PHP, it is very important to know all parser tokens: http://php.net/manual/en/tokens.php

Browser other questions tagged

You are not signed in. Login or sign up in order to post.