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.
$this->data. Also isdanea typo?