4
I’m trying to create a class that saves objects like Conta
in a json file, however, when saving to the file, the information of the object is not saved, it is just this:
[[],{}]
My class reading and saving in json is:
class JSONCustom {
private $list = null;
private $jsonFile = "teste.json";
public function __construct() {
if (file_exists($this->jsonFile)) {
$string = file_get_contents($this->jsonFile);
$json = json_decode($string, true);
$this->list = $json;
} else {
$this->list = Array();
}
}
public function getList() {
return $this->list;
}
public function add($item) {
//$jsonObject = get_object_vars($item);
$this->list[] = $item;
$this->save();
}
private function save() {
if ($this->list != null) {
$string = json_encode($this->list);
file_put_contents($this->jsonFile, $string);
}
}
}
$agencia = new JSONCustom();
$conta = new Corrente(123, "teste", 3.10);
$agencia->add($conta);
var_dump($agencia->getList());
The Chain class:
class Corrente extends Conta{
private $tarifaManutencao = 12.5;
public function __construct($num, $prop, $saldo){
parent::__construct($num, $prop, $saldo);
}
public function getTipoconta(){
return "Corrente";
}
public function listarDados(){
return parent::listarDados().
"\nTipo de Conta: ".$this->getTipoconta().
/* "\nTarifa de Manutencao: ".$this->tarifaManutencao; */
"\nTarifa de Manutencao: ".CalcularFloat::formatarMoeda($this->tarifaManutencao);
}
public function cobrarTarifa(){
if($this->getSaldo() < $this->tarifaManutencao){
$this->habilitarPermissoesEspeciais();
$this->sacar($this->tarifaManutencao);
$this->desabilitarPermissoesEspeciais();
}else{
$this->sacar($this->tarifaManutencao);
}
}
protected function setTarifaManutencao($novaTarifa){
$this->tarifaManutencao = $novaTarifa;
}
}
And my abstract class counts:
abstract class Conta{
private $numero;
private $proprietario;
private $saldo;
private $permissoesEspeciaisHabilitadas = false;
public function __construct($num, $prop,$saldo){
if($num < 0){
throw new ExcecaoNumeroInvalido("Numero da conta nao pode ser negativo.");
}else if($saldo < 0){
throw new ExcecaoValorNegativo("Saldo nao pode ser negativo.");
}
$this->numero = $num;
$this->proprietario = $prop;
$this->saldo = $saldo;
}
public function getNumero(){
return $this->numero;
}
public function getProprietario(){
return $this->proprietario;
}
public function getSaldo(){
return $this->saldo;
}
public function listarDados(){
return "Numero: ".$this->numero.
"\nNome: ".$this->proprietario.
/* "\nSaldo: ".$this->getSaldo(); */
"\nSaldo: ".CalcularFloat::formatarMoeda($this->getSaldo());
}
protected function permissoesEspeciaisHabilitadas(){
return $this->permissoesEspeciaisHabilitadas;
}
public function habilitarPermissoesEspeciais(){
$this->permissoesEspeciaisHabilitadas = true;
}
public function desabilitarPermissoesEspeciais(){
$this->permissoesEspeciaisHabilitadas = false;
}
protected function verificarCondicoesParaSaque($valor){
if ($valor < 0){
throw new ExcecaoValorNegativo("Valor de saque nao pode ser negativo.");
}else if($valor > $this->saldo && !$this->permissoesEspeciaisHabilitadas()){
throw new ExcecaoSaqueInvalido("Sem saldo suficiente para este valor saque.");
}
}
public function sacar($valor){
$this->verificarCondicoesParaSaque($valor);
$this->saldo = CalcularFloat::subtrair($this->saldo, $valor);
}
public function depositar($valor){
if($valor < 0){
throw new ExcecaoValorNegativo("Valor de deposito nao pode ser negativo.");
}
$this->saldo = CalcularFloat::somar($this->saldo, $valor);
}
public abstract function getTipoconta();
}
When executing a var_dump($agencia->getList());
the result returns the object normally:
array (size=3)
0 =>
array (size=0)
empty
1 =>
array (size=0)
empty
2 =>
object(Corrente)[2]
private 'tarifaManutencao' => float 12.5
private 'numero' (Conta) => int 123
private 'proprietario' (Conta) => string 'teste' (length=5)
private 'saldo' (Conta) => float 3.1
private 'permissoesEspeciaisHabilitadas' (Conta) => boolean false
But when converting the json array to $string = json_encode($this->list);
the array is not converted and saves only the quoted at the beginning of the question.
Have you used the
print_r
to see if the array is displayed correctly?– GilCarvalhoDev
@Gilsones yes, look at the result of
var_dump
at the end of the question. The array is like this.– user28595
It’s just that on a you used
$agencia->getList()
and the other$this->list
. I think it’s important to var_dump where you are trying to use json_encode. By the way, useprint_r
where json_encode is. At least for me it’s friendlier. then put also at the end of the post.– GilCarvalhoDev
this happens because they are private objects. place them as "public"
– Ivan Ferrer