Skip to content

Instantly share code, notes, and snippets.

@nezarfadle
Last active August 24, 2017 07:21
Show Gist options
  • Save nezarfadle/89899c5151c3cae610df064a1ef0b551 to your computer and use it in GitHub Desktop.
Save nezarfadle/89899c5151c3cae610df064a1ef0b551 to your computer and use it in GitHub Desktop.

Try the Code

<?php

interface IUser
{
    function changePassword( $password );
}

class User implements IUser
{
    
    private $password;
    
    public static function find($id)
    {
        return new User();
    }
    
    function changePassword( $password )
    {
        $this->password = $password;
        // Do some database persistant
    }
}


class SecurityUser implements IUser
{
    private $user;
    
    function __construct(IUser $user)
    {
        $this->user = $user;
    }
    
    function changePassword( $password )
    {
        $password = password_hash( $password, PASSWORD_DEFAULT );
        $this->user->changePassword( $password );
        return $password; // This line is not necessary, I added if for the sake of seeing the new hashed password
    }
}

$user = new SecurityUser( User::find(1) );
echo $user->changePassword ( 'new secret' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment