I have two classes, Emp and Dep. I want to get a result something familiar to this:
$objDep = new Dep();
$objDep->SetName('Sales');
$objEmp = new Emp();
$objEmp->SetDep($objDep);
$objDep->GetEmps()->Add($objEmp);
$objDep->GetEmps(0)->GetDep()->GetName(); //result 'sales'
I have written this in Dep class:
...
public $_emps = array();
...
...
public function GetEmps() {
$params = func_get_args();
$numargs = func_num_args();
if (func_num_args()) {
return $this->_emps[$params[0]];
}
function Add($new_emp)
{
array_push($this->_emps,$new_emp);
}
}
...
and a I'm having an error:
Fatal error: Call to a member function Add() on a non-object.
What is wrong with this code?
Maybe this is simple but I'm new in PHP and I want to complete my exercise for classes.
$objDep->GetEmps(0)->SetDep()->GetName();? I don't see any other reference toGetDep()Addmethod outside theGetEmpsmethod, so its just another method in the class. Emp's and Dep's makes no sense, a good naming convention and class entity would make it clearer to yourselfGetName()