Symbol "-" in php [duplicate]

4

When I used PHPmailer I used (without knowing why) the symbol "->" in $mail->AddAdress(... , for example. Now asking a question in W3schools ( http://www.w3schools.com/php/php_mysql_connect.asp ) I saw this little set "- >" again in $conn->connect_error .

What exactly does this mean - > " symbol?

(I tried to put it in google but it does not even read the symbol as a string, it seems to incorporate it as a special character like +)

    
asked by anonymous 12.10.2015 / 04:41

3 answers

7

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

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

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

And call it like this:

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

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

You can also call the method inside another method using $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)

PHP 5 includes visibility , classes and methods abstract and end , magic methods , interfaces , cloning and type induction .

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

  

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

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

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());
  }
}

Conclusion

So the basics of what a class is, I tell you that . is a class (or rather a set of classes and other functions) and the link you put is an example of PHPmailer which is a class (classes), see here started with mysqli , when seeing it in some code will probably be a class: / p>
<?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";
?>

Visibility into classes

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

  • keyword is when it is available to be accessed by "any", for example public 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();
    
  • sobrenome is when the method or variable is accessible only within the class, if it tries to access outside it will cause a private , 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
    

    You will issue this error:

      

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

  • Exception 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 that extend the current class, including the parent class.

Read more on Classes and Objects Visibility

    
12.10.2015 / 05:11
5

The official name is Object operator , but, I see around the staff calling it 'setinha' , 'arrow' and 'arrow'.

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

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

    
12.10.2015 / 05:19
3

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

  

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

This "arrow" and other symbols are inherited from C / C ++ and are used by most programming languages, including PHP and your youngest brother Hack / HHVM

For those who program in PHP, it is very important to know all parser tokens : link

    
12.10.2015 / 06:42