2

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.

3
  • Try $this->data. Also is dane a typo? Commented Feb 2, 2016 at 20:08
  • Im not a PHP person either but here are my thoughts: $this->$data['name'] = $name is assigning a string to $data['name']. however you declared $data['name'] to point to an array, not a string Commented Feb 2, 2016 at 20:12
  • @chris85 yes, it was a typo, I edited it. Commented Feb 2, 2016 at 20:13

6 Answers 6

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

5 Comments

@RomanPerekherst Thanks man! It works, but still, before the result there's a warning: "array_push() expects at least 2 parameters, 1 given" and points at those array_push lines. How do I get rid of it?
welcome! are you sure you have passed two parameters into each array_push call ? can you show me one of those array_push lines ?
@RomanPerekherst Here it is: array_push($this->data['name'] = $name); - I have to use "=" instead of "," because it doesn't show the result properly. With "," there's "array_push() expects parameter 1 to be array, string given" warning.
I suppose that you have changed $data property content. If you are going to use array_push then your $data property should have the following structure: private $data = Array( 'name' => array(), 'surname' => array(), 'age' => array() );. If you have changed nested arrays with strings, such as 'name' => '' then change your addPerson method content to this: $this->data['name'] = $name; $this->data['surname'] = $surname; $this->data['age'] = $age;
Yes, you're right. I'm sorry I caused some problems here. Thanks to you, I may be one step closer to finish my tasks :)
0

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

Comments

0

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

Comments

0

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.

1 Comment

You needed to change "private" to "public" Serioulsy? Why?
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.