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