What’s the difference from . to -> in PHP?

Asked

Viewed 55 times

1

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

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

$varTeste.abrirFuncao(); 

and from that:

$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 financial year: https://youtu.be/lOOYBUQSRWU?t=8m1s

  • Are you sure the first snippet is in PHP? In PHP or Operator . concatenation, not to access methods.

  • 1

    I didn’t know that the . in PHP was used to call a function, here from the error. I think it does not have this purpose.

  • You can post the source of this exercise?

  • Yes, I am sure. It is within a class this Function with this excerpt. The source will also put in the edition.

  • Okay, I edited it.

  • @Lucascarvalho is typo I think.

  • 2

    It executes the code around the 15 minutes and error appears.

  • I don’t understand why -1?

  • Got it, I haven’t seen the video everyone, put is a course, I’m pausing and doing with it. I had a question, and I came to ask. Thanks Anderson for the comment!

  • 3

    Boy, better watch class with another teacher :)

  • 1

    I don’t think so Leo, I bet that Guanabara is one of the best teachers to learn to program in Brazil, because of his teaching.

  • Yes, it can be, but to err in the basics is a dose. For me, who do not know him, it was a bad calling card.

  • 1

    @Lucascarvalho You can be sure you don’t. He is one of the people who teaches the most wrong things. He teaches everything crooked, Leocaracciolo is absolutely right.

  • I don’t think @LINQ likes his didactics very much. And the Video Course project. Maybe for you who are advanced, it is bad, but for us, who are learning, a PROJECT that he and Hostnet made, helped THOUSANDS of people. I, for one, am working because of him. Obviously, it’s not just him, I’ve bought books, right here at Sopt, among other places, but the starting point, which made me fall in love with programming, was because of him.

Show 9 more comments

2 answers

4

The point (.) in the PHP is used to perform concatetion of 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 the PHP uses these "little arrows", see the example:

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

// PHP
classe->funcao();
  • It wasn’t the case, it was the teacher’s typo, but thank you for the information!

4


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

https://youtu.be/lOOYBUQSRWU?t=15m26s

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

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

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

Take great care that this can generate a false positive. For example, let’s consider two objects in the class Foo:

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

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

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

function getCategoria() {
    return "categoria";
}

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

class Foo {

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

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

}

When executing the code:

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

The exit will be bool(true), for the comparison will be "classe Foocategoria" == "classe Foocategoria". That is, instead of giving an error, your condition returned true in a if. This can generate 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 even a warning.

See working on Ideone.

Imagine if you make the condition below:

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

The damage it would cause because of a wrong operator...

Browser other questions tagged

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