Skip to content

Instantly share code, notes, and snippets.

@ahmedash95
Created October 23, 2017 14:43
Show Gist options
  • Save ahmedash95/ae43e5dcc5a004838cd3e3943af7cbe2 to your computer and use it in GitHub Desktop.
Save ahmedash95/ae43e5dcc5a004838cd3e3943af7cbe2 to your computer and use it in GitHub Desktop.
PHP : Loosely Coupled Example
<?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