V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
sanorchid
V2EX  ›  PHP

有人用 slimphp 吗?

  •  
  •   sanorchid · Jun 11, 2017 · 3450 views
    This topic created in 3243 days ago, the information mentioned may be changed or developed.
    菜鸟一个。看该框架的主页 https://www.slimframework.com/,示例代码有一段写法不懂,求解。
    $app->get('/hello/{name}', function (Request $request, Response $response) {
    $name = $request->getAttribute('name');
    $response->getBody()->write("Hello, $name");
    return $response;
    });
    $app->run();

    路由那里,那个匿名函数的两个参数,一个是$request,一个是$response,但是这两个变量前面分别加了的 Request 和 Response 是一个什么写法?
    8 replies    2017-06-12 12:58:33 +08:00
    gongpeione
        1
    gongpeione  
       Jun 11, 2017
    jellybool
        2
    jellybool  
       Jun 11, 2017
    我觉得 1 楼的并不是正路

    关键字是:Type Hinting (php 类型约束 )

    个人推荐看这个 http://php.net/manual/en/language.oop5.typehinting.php

    简单理解就是:$request 必须是 Request 的一个实例,$response 同理。
    Jakesoft
        3
    Jakesoft  
       Jun 11, 2017
    这跟 slimphp 有什么关系
    Jakesoft
        4
    Jakesoft  
       Jun 11, 2017
    这应该是 php 基础语法吧
    imcj
        5
    imcj  
       Jun 11, 2017
    @Jakesoft 申明类型,slim 使用了这个特性,这样你可以很容易的在 phpstorm 这样的 ide 里面跳转到 request 的定义。
    Pastsong
        6
    Pastsong  
       Jun 11, 2017
    @jellybool 你给的链接还是跳到 1 楼的了 [Facepalm]

    给个中文的 http://php.net/manual/zh/functions.arguments.php#functions.arguments.type-declaration
    sanorchid
        7
    sanorchid  
    OP
       Jun 12, 2017
    谢谢大家
    miaotaizi
        8
    miaotaizi  
       Jun 12, 2017
    可以先看看这些个东西, 理解里面的一些术语
    https://laravel-china.github.io/php-the-right-way/
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3238 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 40ms · UTC 14:14 · PVG 22:14 · LAX 07:14 · JFK 10:14
    ♥ Do have faith in what you're doing.