Skip to content

Instantly share code, notes, and snippets.

@herloct
Created April 4, 2017 02:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save herloct/62336fc7315ba0f8448d39aaf9d96ec9 to your computer and use it in GitHub Desktop.
Save herloct/62336fc7315ba0f8448d39aaf9d96ec9 to your computer and use it in GitHub Desktop.
PHP Prosedural vs OOP
<?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('fulan@gmail.com', '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;
<?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('fulan@gmail.com', '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