The error occurs here:
In the Agenda class, you are passing the parameter $add
for an array type variable called $bancoDeDados
and not to a property called bancoDeDados
.
Your code simply works by assigning the $add variable to a property called bancoDeDados
in the Agenda class. Like this:
$this->bancoDeDados = $add;
Your code in the agenda class then stands:
class Agenda {
public function adicionar($add) {
$this->bancoDeDados = $add;
}
public function view() {
foreach ($this->bancoDeDados as $key) {
echo "voce escreveu:" . $key;
}
}
}
It is interesting that you define the property before using it, so:
class Agenda {
public $bancoDeDados;
public function adicionar($add) {
$this->bancoDeDados = $add;
}
public function view() {
foreach ($this->bancoDeDados as $key) {
echo "voce escreveu:" . $key;
}
}
}
If you want the property to be an array, set: public $bancoDeDados = array();
, soon:
class Agenda {
public $bancoDeDados = array();
public function adicionar($add) {
$this->bancoDeDados = $add;
}
public function view() {
foreach ($this->bancoDeDados as $key) {
echo "voce escreveu:" . $key;
}
}
}
You define the property bancoDeDados
as an array but passing a string. Since it is an array, you can also pass as an array parameter:
<?php
class Agenda {
public $bancoDeDados = array();
public function adicionar($add) {
$this->bancoDeDados = $add;
}
public function view() {
echo "Você escreveu:";
foreach ($this->bancoDeDados as $index => $dados) {
echo "<Br>" . $index . $dados;
}
}
}
class PessoaFisica extends Agenda {
function __construct() {
}
}
$pf = new PessoaFisica();
$pf->adicionar(array('Id: ' => 7, 'Nome: ' => "Luiz"));
$pf->View();
did not give. , did not work.
– Vitor
I edited the answer
– Costamilam
I’ll edit my code, complete with the inheritance
– Vitor
edited, follows the code.
– Vitor
Your editing didn’t help much, is that the complete code? Why isn’t it working? Any errors?
– Costamilam
I didn’t realize the mistake, I apologize, thanks for the tips.
– Vitor