2
I created a function within the class that takes a parameter and changes the value of a class variable, but it doesn’t work. Can anyone explain to me why? The result is NULL
<?php
class book{
public $nome;
function nao($x){
$nome = $x;
}
}
$pirata = new book();
$pirata->nao("caribe");
echo $pirata->nome;
var_dump($pirata);
You are not setting the field value
$nome
. The methodnao
is only creating a local variable (which cannot be accessed outside of it). To change a global field, use the$this
, for example:$this->nome = $x
– Valdeir Psr
@Valdeirpsr It would be interesting if you put as answer and if possible exemplify, so that the question can be marked as answered.
– Vinicius De Jesus
yaaay gratiluz!!! worked perfectly =DDD
– Cesar Bueno