PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
<?php
class Woman{
private $id; // string
private $hair;
function getId(){return $this->id;}
}
class Man{
private $id; // string
private $car;
function getId(){return $this->id;}
}
class Group {
private $name;
private $members = array(); // array of classes woman / man
public function addMember ($member){
// how to add a new member with her - his id ?
$this->members[$member->id] = $member; // does not work
}
}
class World {
private $group; // Typ class Group
private $id;
private $test_id;
private $test_member; // woman - man
public function doit(){
$this->test_member = $this->group->members[$this->id]; // how to access a member ?
$this->test_id= $this->group->members[$this->id]->getId(); // how to access their methods ?
}
}
?>
$member->id; is a 'private' property, which means only the methods within that object can access that property.
For it to work in that fashion, you would require the property to be 'public'
Though it is better practice to have a 'get' function to get the ID instead of directly accessing the property, which you already have. So change to this: