Created
May 5, 2017 04:14
-
-
Save teddy-hoo/9e006c8e0020dce2b9c8781b0d6ecec3 to your computer and use it in GitHub Desktop.
php oop in a typical web application
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 | |
| /** | |
| * basic model | |
| */ | |
| class Model { | |
| protected $connection = 'db'; | |
| public static | |
| } | |
| /** | |
| * order model | |
| */ | |
| class Order extends Model { | |
| protected $fields = [ | |
| 'id', | |
| 'money', | |
| ]; | |
| public function create ($data) { | |
| // create row in db ... | |
| $id = 1111; | |
| return $id; | |
| } | |
| } | |
| /** | |
| * order business logic | |
| */ | |
| class OrderService { | |
| public function validateParams ($params) { | |
| // validation logic ... | |
| } | |
| public function createOrder ($params) { | |
| $this->validateParams($params); | |
| return (new Order())->create($params); | |
| } | |
| } | |
| /** | |
| * basic controller | |
| */ | |
| class BaseController { | |
| protected function getPostParams () { | |
| return $_POST; | |
| } | |
| } | |
| /** | |
| * order controller | |
| */ | |
| class OrderController extends BaseController { | |
| public function createAction() { | |
| $params = $this->getPostParams(); | |
| $orderService = new OrderService(); | |
| $result = $orderService->createOrder($params); | |
| return $result; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment