Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Post{
- private $post = "";
- public function __construct($post){
- $this->post = $post;
- }
- public function showPost(){
- echo $this->post. "<br>";
- }
- }
- class User{
- private $name = "";
- private $posts = [];
- public function __construct($name){
- $this->name = $name;
- }
- public function showName (){
- echo "Last post from ". $this->name .": <br>";
- }
- 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();
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement