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...
Are you sure the first snippet is in PHP? In PHP or Operator
.
concatenation, not to access methods.– Woss
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.– gato
You can post the source of this exercise?
– gato
Yes, I am sure. It is within a class this Function with this excerpt. The source will also put in the edition.
– Lucas de Carvalho
Okay, I edited it.
– Lucas de Carvalho
@Lucascarvalho is typo I think.
– novic
It executes the code around the 15 minutes and error appears.
– Woss
I don’t understand why -1?
– gato
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!
– Lucas de Carvalho
Boy, better watch class with another teacher :)
– user60252
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.
– Lucas de Carvalho
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.
– user60252
@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.
– Jéf Bueno
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.
– Lucas de Carvalho