Created
April 4, 2017 02:59
-
-
Save herloct/62336fc7315ba0f8448d39aaf9d96ec9 to your computer and use it in GitHub Desktop.
PHP Prosedural vs OOP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Member | |
{ | |
private $email; | |
public function getEmail(): string | |
{ | |
return $this->email; | |
} | |
private $password; | |
public function authenticate(string $password): bool | |
{ | |
return $this->password === $password; | |
} | |
public function __construct(string $email, string $password) | |
{ | |
$this->email = $email; | |
$this->password = $password; | |
} | |
} | |
// -- | |
$member = new Member('[email protected]', 'some password'); | |
// pake password yg bener | |
$result = $member->authenticate('some password'); | |
echo 'Member ', $member->getEmail(), ' is ', ($result ? 'authenticated' : 'not authenticated'), PHP_EOL; | |
// pake password yg salah | |
$result = $member->authenticate('wrong password'); | |
echo 'Member ', $member->getEmail(), ' is ', ($result ? 'authenticated' : 'not authenticated'), PHP_EOL; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function member_create(string $email, string $password): array | |
{ | |
return array( | |
'email' => $email, | |
'password' => $password | |
); | |
} | |
function member_authenticate(array $member, string $password): bool | |
{ | |
return $member['password'] === $password; | |
} | |
// -- | |
$member = member_create('[email protected]', 'some password'); | |
// pake password yg bener | |
$result = member_authenticate($member, 'some password'); | |
echo 'Member ', $member['email'], ' is ', ($result ? 'authenticated' : 'not authenticated'), PHP_EOL; | |
// pake password yg salah | |
$result = member_authenticate($member, 'wrong password'); | |
echo 'Member ', $member['email'], ' is ', ($result ? 'authenticated' : 'not authenticated'), PHP_EOL; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment