post = $post;
}
public function showPost(){
echo $this->post. "
";
}
}
class User{
private $name = "";
private $posts = [];
public function __construct($name){
$this->name = $name;
}
public function showName (){
echo "Last post from ". $this->name .":
";
}
private function readPosts(){
//reset array
unset($this->posts);
//get post list
$this->posts[] = new Post("Sample post 1");
$this->posts[] = new Post("Sample post 2");
$this->posts[] = new Post("Sample post 3");
}
public function getPosts(){
//read posts
$this->readPosts();
//return posts array
return $this->posts;
}
public function getLastPosts(){
//read posts
$this->readPosts();
$count = count($this->posts);
if ($count > 0) {
return $this->posts[$count-1];
}else{
return NULL;
}
}
}
$user = new User("Niton");
$user->showName();
//get user last post
$last_post = $user->getLastPosts();
if ($last_post != null) {
$last_post->showPost();
}
?>