티스토리 뷰

웹개발/Php

php7 클로져 사용하기

yaku 2019. 4. 13. 12:43

<?php
/**d,0c ./. ;.l;. ㅣ;'/,.
* closer.php
*
*
* @since 2018. 6. 13.
* @version 1.0
*/

class App
{
protected $routes = array();
protected $responseStatus = '200 ok';
protected $responseContentType = 'text/html';
protected $responseBody = 'Hello world';

public function addRoute($routePath, $routeCallBack)
{
$this->routes[$routePath] = $routeCallBack->bindTo($this,__CLASS__);
}

public function dispatch($currentPath)
{
//print_r($currentPath);
foreach ( $this->routes as $routePath => $callback) {
echo $routePath;
echo $currentPath;
if ( $routePath === $currentPath) {
$callback(); // 콜백 함수
}
}

echo $this->responseBody;
}
}

$app = new App();

$app->addRoute('/user/josh', function(){
$this->reponseContentType = 'apllcation/json;charset=utf8';
$this->responseBody = '{"name": "Josh"}';
});

$app->dispatch('/user/josh');

'웹개발 > Php' 카테고리의 다른 글

PHP CLI 정리하기  (0) 2017.06.08
PHP - DateTime을 활용하여 날짜 계산하기  (0) 2017.04.04
state 패턴 샘플  (0) 2017.02.06
데코레이터패턴 샘플  (0) 2017.02.06
옵저버패턴샘플  (0) 2017.02.06
댓글
D-DAY
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
글 보관함