What is the difference of. for - in PHP?

1

What's the difference between them? I took an "Aggregation Relationship" exercise that had the 2, and did not understand the use of the. instead of - >

In this case, what is the difference, for example, from this:

$varTeste.abrirFuncao(); 

and this:

$varTeste->abrirFuncao();


function lutar($l1, $l2){
        if($l1.getCategoria() == $l2.getCategoria()
                && ($l1 != $l2)){
                $this->aprovada = true;
                $this->desafiado = $l1;
                $this->desafiante = $l2;
        } else {

        }
    }

Source of the exercise: link

    
asked by anonymous 03.07.2017 / 22:20

2 answers

4

It was a typo. Turn the video over and you will see that when it runs the program, the error appears.

link

To access a method in PHP is only through the -> operator, or through :: for static methods. The . operator concatenates, then execute the code:

$l1.getCategoria() == $l2.getCategoria()

PHP will attempt to concatenate the $l1 variable with the return of the getCategoria() function and attempt to concatenate the variable $l2 also with the return of the getCategoria() function, verifying that the final values are equal.

Be very careful that this can generate a false positive. For example, let's consider two objects of class Foo :

$l1 = new Foo("lutador 1");
$l2 = new Foo("lutador 2");

var_dump($l1.getCategoria() == $l2.getCategoria());

Let's assume that the getCategoria function is defined in the program:

function getCategoria() {
    return "categoria";
}

If the Foo class has, for example, the __toString method:

class Foo {

    public function __construct($name) {
        $this->name = $name;
    }

    public function __toString() {
        return "classe Foo";
    }

}

When running the code:

var_dump($l1.getCategoria() == $l2.getCategoria());

The output will be bool(true) , since the comparison will be "classe Foocategoria" == "classe Foocategoria" . That is, instead of giving an error, its condition returned true in if . This can lead to unexpected results in the application and it will be very difficult to find the source of the error, because being a valid syntax, any interpreter will accept the code and will not show you a warning.

  

See working at Ideone .

Imagine if you do the following condition:

if ($l1.getCategoria() == $l2.getCategoria()) {
    mysqli_query($conn, "DROP DATABASE sopt");
}

The damage you would do because of the wrong operator ...

    
03.07.2017 / 22:39
4

The point (.) in PHP is used to concatenate strings as in the example below:

$nome = 'Meu nome';
$sobreNome = 'Meu sobrenome';
echo $nome.$sobreNome
// Saida: Meu NomeMeu sobrenome
// Note que não existem espaços entre os nome pois não são usados espaços nas strings.

The (->) arrow is used to call class functions, while other languages use ponto to call function PHP use these "setinhas", see example:

// Java, Ruby, Javascript e etc...
classe.funcao();

// PHP
classe->funcao();
    
03.07.2017 / 22:38