Created
October 23, 2017 14:43
-
-
Save ahmedash95/ae43e5dcc5a004838cd3e3943af7cbe2 to your computer and use it in GitHub Desktop.
PHP : Loosely Coupled Example
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 | |
Interface IPosts{} | |
Interface IComments{} | |
# Models | |
Class CommentsMysqlModel implements IComments{} | |
Class CommentsMongoDBModel implements IComments{} | |
Class PostsMysqlModel implements IPosts{} | |
Class PostsMongoDBModel implements IPosts{} | |
# Classes | |
Class Posts { | |
private $postsModel; | |
private $commentsModel; | |
public function __construct(IPosts $postsModel,IComments $commentsModel){ | |
$this->postsModel = $PostsModel; | |
$this->commentsModel = $commentsModel; | |
} | |
} | |
// Example Of Usage | |
# IF I would like to use mysql for both | |
$comments = new CommentsMysqModel; | |
$posts = new PostsMysqModel; | |
$postClass = new Posts($posts,$comments); | |
// So if i would like to use comments for mongodb | |
$comments = new CommentsMongoDBModel; | |
$posts = new PostsMysqModel; | |
$postClass = new Posts($posts,$comments); | |
// That's it | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment