Haseoh
Haseoh

Reputation: 930

PHP - using methods to input data into Array

This is my first post here, so forgive me if I do a mistake or two :)

Few days ago I had an interview (the job offer is even not related to PHP), the employer gave me few excercises to show him my skill. Currently I'm stuck at PHP, this is my 1st time doing anything in PHP, I'm totally new to this one. I really want this job, so I hope you can help me out :)

The task: Create class "Person", inside Person create an Array variable named "data" with keys: "name", "surname" and "age", then create method addPerson(), which will fill the array with data. Create another method - showPerson() to show the gathered data (you can use print_r();).

So far, I've made this, but this is totally mess, it doesn't work. I have no knowledge about PHP, the code comes strictly from here and other websites.

<?php

class Person
{

    private $data = Array(
        'name'    => array(),
        'surname' => array(),
        'age'     => array()
    );

    function addPerson($name, $surname, $age)
    {
        array_push($this->$data['name']    = $name);
        array_push($this->$data['surname'] = $surname);
        array_push($this->$data['age']     = $age);
    }

    function showPerson()
    {
        echo json_encode($data);
        //print_r($data);
    }

}

$human = new Person;
$man->addPerson("John", "Johnes", 50);
$woman->addPerson("Maria", "Johnes", 45);
$human->showPerson();

Notice: Undefined variable: data; Fatal error: Cannot access empty property

The error marks first array_push in addPerson method. I don't know how to repair the code to make it work. Thank you in advance.

Upvotes: 2

Views: 826

Answers (6)

LuanComputacao
LuanComputacao

Reputation: 21

According to the task you need create an array called 'data' in your Person object using the below format

Array
(
    [name] => Maria
    [surname] => Johnes
    [age] => 45
)

To this you can use this class

<?php

class Person
{
    private $data = Array();

    function addPerson($name, $surname, $age)
    {
        $this->data = ['name' => $name, 'surname' => $surname, 'age' => $age];
    }

    function showPerson()
    {
        print_r($this->data);
    }

}

//Using to create a woman
$woman = new Person;
$woman->addPerson("Maria", "Johnes", 45);
$woman->showPerson();


//Using to create a man
$man = new Person;
$man->addPerson("John", "Johnes", 50);
$man->showPerson();

Use this link to view this running https://eval.in/512101

Upvotes: 0

David D
David D

Reputation: 1295

for multiple people:

class Person {
    public $data = array();

    public function addPerson($name, $surname, $age){
        $this->data[] = array(
            'name'=>$name,
            'surname'=>$surname,
            'age'=>$age
        );
    }

    public function showPeople(){
        // echo json_encode($data);
        print_r($this->data);
    }   

} 

$Person = new Person;
$Person->addPerson("John", "Johnes", 50);
$Person ->addPerson("Maria", "Johnes", 45);
$Person->showPeople();

for single person:

class Person {
    public $data = array();

    public function addPerson($name, $surname, $age){
        $this->data = array(
            'name'=>$name,
            'surname'=>$surname,
            'age'=>$age
        );
    }

    public function showPerson(){
        // echo json_encode($data);
        print_r($this->data);
    }   

} 

$Man = new Person;
$Man->addPerson("John", "Johnes", 50);
$Man->showPerson();

$Woman = new Person;
$Woman->addPerson("Maria", "Johnes", 45);
$Woman->showPerson();

It's clear you should read some PHP OOP beginner guides to understand better.

Upvotes: 0

Mdalz
Mdalz

Reputation: 154

Not quite sure why you have to predefine the array. This example works, similar to the answer by @RomanPerekhrest except without all the pushing business.

class Person {

    private $data = array();

    function addPerson($name, $surname, $age){
        $this->data['name'] = $name;
        $this->data['surname'] = $surname;
        $this->data['age'] = $age;
    }

    function showPerson(){
        echo json_encode($this->data);
        //print_r($data);
    }

}
$human = new Person;
$human -> addPerson("John", "Johnes", 50);
$human -> showPerson();

Upvotes: 0

Nazmul Alam
Nazmul Alam

Reputation: 109

Try this.

    <?php
    class Person {
    private $data = Array(
    'name' => array(),
    'surname' => array(),
    'age' => array()
    );

    function addPerson($name, $surname, $age){
        $this->data['name'] = $name;
        $this->data['surname'] = $surname;
        $this->data['age'] = $age;
    }
    function showPerson(){
        echo json_encode($this->data);
        //print_r($this->data);
    }   
    } 
    $man = new Person;
    $man -> addPerson("John", "Johnes", 50);
    $man -> showPerson();
    $woman = new Person;
    $woman -> addPerson("Maria", "Johnes", 45);
    $woman -> showPerson();
    ?>

Upvotes: 0

cgalev
cgalev

Reputation: 431

Use: $this->data['name'] instead of $this->$data['name'] (no dollar sign in front of data).

Also, No need to initialize name, surname and age as arrays. You could try something like :

private $data = Array(
'name' => '',
'surname' => '',
'age' => 0
);

Then you could try the addPerson method suggested by Nechemya-Kanelsky

Upvotes: 1

RomanPerekhrest
RomanPerekhrest

Reputation: 92854

Change your code as shown below:

class Person {

    private $data = Array(
        'name' => array(),
        'surname' => array(),
        'age' => array()
    );

    function addPerson($name, $surname, $age) {
        array_push($this->data['name'], $name);
        array_push($this->data['surname'], $surname);
        array_push($this->data['age'], $age);
    }

    function showPerson() {
        echo json_encode($this->data);
    }

}

$human = new Person;
$human->addPerson("John", "Johnes", 50);
$human->showPerson();

http://php.net/manual/en/function.array-push.php
array_push accepts two parameters: the array and the first value to push onto the end of the array

Upvotes: 0

Related Questions