Call an arraylist from another method, to print in the different method

Asked

Viewed 59 times

2

I want to get the information of the arraylist within a certain method, to print in another method.

You’re not printing anything, my logic is right ? class Agenda //Class Agenda var $bancodedados = array();

public function adicionar($add){
        $bancoDeDados[] = $add;

}

public function view(){
    foreach ($this->bancoDeDados as $key) {
        echo "voce escreveu:".$key;
    }
}

}

class pessoaFisica extends Agenda
{
	function __construct(){

	}
  }
  
  
  
  
  
$pf = new PessoaFisica();

$pf->adicionar("ola mundo");
$pf->View();

2 answers

1


In function adicionar you do not add an item in the array, only overwrite it with an array of only one item (the new one, which should be added), plus the function has a loop that does nothing

I imagine you want something like this:

class Teste {
    public $bancoDeDados = array();

    public function adicionar($add){
        $this->bancoDeDados[] = $add;
    }

    public function view(){
        foreach ($this->bancoDeDados as $key) {
            echo "\nvoce escreveu: ".$key;
        }
    }
}

$teste = new Teste();

$teste->adicionar("foo");
$teste->adicionar("bar");
$teste->view();

Returns:

voce escreveu: foo
voce escreveu: bar

But if the function only does it does not need it, it is much simpler to use the method $variavel[] = 'valor';

  • did not give. , did not work.

  • I edited the answer

  • I’ll edit my code, complete with the inheritance

  • edited, follows the code.

  • Your editing didn’t help much, is that the complete code? Why isn’t it working? Any errors?

  • I didn’t realize the mistake, I apologize, thanks for the tips.

Show 1 more comment

0

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();

  • 1

    Thanks for the tips.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.