티스토리 뷰

웹개발/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' 카테고리의 다른 글

php7 클로져 사용하기  (0) 2019.04.13
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
댓글
댓글쓰기 폼