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