Skip to content

Instantly share code, notes, and snippets.

@hadaytullah
Last active October 25, 2019 09:17
Show Gist options
  • Save hadaytullah/a0bf201b0d3bd1ac6ac3442364421207 to your computer and use it in GitHub Desktop.
Save hadaytullah/a0bf201b0d3bd1ac6ac3442364421207 to your computer and use it in GitHub Desktop.
Object oriented programming example in php
<? php
class User {
private $name;
private $lastLoggedInAt;
private $loggedIn;
function __construct($name) {
$this->name = $name;
$this->loggedIn = false;
$this->lastLoggedInAt = null;
}
function isLoggedIn() {
return $this->loggedIn;
}
function getLastLoggedInAt() {
return $this->lastLoggedInAt;
}
function logIn() {
$this->lastLoggedInAt = date("Y-m-d H:i:s");
$this->loggedIn = true;
}
function logOut() {
$this->loggedIn = false;
}
function getName() {
return $this->name;
}
function setName($name) {
$this->name = $name;
}
function canEdit($comment) {
if($comment->getAuthor() instanceof User and $comment->getAuthor() === $this){
return true;
}
return false;
}
function canDelete($comment) {
return false;
}
}
class Moderator extends User{
function __construct($name) {
parent::__construct($name);
}
function canEdit($comment) {
if($comment->getAuthor() instanceof Moderator and $comment->getAuthor() === $this){
return true;
}
return false;
}
function canDelete($comment) {
if($comment instanceof Comment){
return true;
}
return false;
}
}
class Admin extends Moderator{
function __construct($name) {
parent::__construct($name);
}
function canEdit($comment) {
if($comment instanceof Comment){
return true;
}
return false;
}
function canDelete($comment) {
if($comment instanceof Comment){
return true;
}
return false;
}
}
class Comment {
private $author;
private $message;
private $repliedTo;
private $createdAt;
function __construct($author, $message, $repliedTo=null) {
$this->author = $author;
$this->message = $message;
$this->repliedTo = $repliedTo;
$this->createdAt = date("Y-m-d H:i:s");
}
function getMessage() {
return $this->message;
}
function setMessage($message) {
$this->message = $message;
}
function getCreatedAt() {
return $this->createdAt;
}
function getAuthor() {
return $this->author;
}
function getRepliedTo() {
return $this->repliedTo;
}
function __toString() {
if (!is_null($this->repliedTo) and $this->repliedTo instanceof Comment){
return "{$this->message} by {$this->author->getName()} (replied to {$this->repliedTo->getAuthor()->getName()})";
}
return "{$this->message} by {$this->author->getName()}";
}
}
class OOPTest extends TestCase {
public function testExample() {
//should be set somewhere globally
date_default_timezone_set("Europe/Helsinki");
$user = new User("User 1");
$this->assertEquals("User 1", $user->getName(), "User name is set correctly");
$mod = new Moderator("Moderator");
$this->assertInstanceOf(User::class, $mod, "Moderator is a User");
}
public function testComment() {
//should be set somewhere globally
date_default_timezone_set("Europe/Helsinki");
$user = new User("User 1");
$this->assertEquals("User 1", $user->getName(), "User name is set correctly");
$mod = new Moderator("Moderator");
$this->assertInstanceOf(User::class, $mod, "Moderator is a User");
$admin = new Admin("Admin");
$commentUser = new Comment($user, "My first comment!");
$this->assertEquals(true, $user->canEdit($commentUser), "User can edit his comment");
$this->assertEquals(false, $mod->canEdit($commentUser), "Moderator can't edit user comment");
$this->assertEquals(true, $admin->canEdit($commentUser), "Admin can edit user comment");
$commentModerator = new Comment($mod, "My first comment!", null);
$this->assertEquals(false, $user->canEdit($commentModerator), "User can't edit moderator comment");
$this->assertEquals(true, $mod->canEdit($commentModerator), "Moderator can edit his comment");
$this->assertEquals(true, $admin->canEdit($commentModerator), "Admin can edit user comment");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment