vb0ivin/superglobals

对PHP超全局变量的类型安全访问

v2.0.0 2022-09-14 00:21 UTC

This package is auto-updated.

Last update: 2024-09-14 21:33:41 UTC


README

CI

对PHP超全局变量的类型安全访问。

组件

服务器

代表$_SERVER超全局的类。

在$_SERVER中访问'REQUEST_METHOD'的示例

use type Superglobals\Server;

$server = new Server();
$server->$REQUEST_METHOD; // 'GET', 'HEAD', 'POST', 'PUT', etc. or null

使用原始$_SERVER超全局的示例

use type Superglobals\Server;

$unsafeServer = Server::_UNSAFE();
$unsafeServer['REQUEST_METHOD']; // 'GET', 'HEAD', 'PUT', etc. or null
$unsafeServer['UNKNOWN_VARIABLE']; // Could be anything

获取

代表$_GET超全局的类。

使用自定义查询字符串的$_GET示例

use type Superglobals\{Get, QueryString, QueryVariable};

<<QueryString>>
final class PersonQuery {
    <<QueryVariable>> public ?int $age;
    <<QueryVariable>> public ?string $name;
}

$query = new Get(PersonQuery::class);
$query?->data?->age; // Do something with the person's age
$query?->data?->name; // Do something with the person's name

提交

代表$_POST超全局的类。

使用自定义表单的$_POST示例

use type Superglobals\{Form, FormVariable, Post};

<<Form>>
final class SignupForm {
    <<FormVariable>> public ?int $age;
    <<FormVariable>> public ?string $name;
    <<FormVariable>> public ?string $email;
}

$form = new Post(SignupForm::class);
$form?->data?->age; // Do something with the person's age
$form?->data?->name; // Do something with the person's name
$form?->data?->email; // Do something with the person's email

Cookie

代表$_COOKIE超全局的类。

使用自定义Cookie的$_COOKIE示例

use type Superglobals\{Cookie, Cookies, CookiesData};

<<CookiesData>>
final class MyCookies {
    <<Cookie>> public ?string $foo;
    <<Cookie>> public ?int $bar;
    <<Cookie>> public ?float $baz;
}

$cookies = new Cookies(MyCookies::class);
$fooCookie = $cookies?->data?->foo; // Do something with the data within cookie 'foo'
$barCookie = $cookies?->data?->bar; // Do something with the data within cookie 'bar'
$bazCookie = $cookies?->data?->baz; // Do something with the data within cookie 'baz'

会话

代表$_SESSION超全局的类。

使用自定义属性的$_SESSION示例

use type Superglobals\{Session, SessionData, SessionVariable};

<<SessionData>>
final class MySession {
    <<SessionVariable>> public ?string $savedID;
    <<SessionVariable>> public ?string $lastPageSeen;
}

$session = new Session(MySession::class);
$session?->data?->savedID; // Do something with the session's saved ID
$session?->data?->lastPageSeen; // Do something with the session's last seen page

SuperglobalException

当您在使用本库中定义的超全局类之一实例化的类上未使用类属性时,将抛出此异常。

以下是一个使用$_POST的示例

use type Superglobals\{Form, FormVariable, Post};

// INVALID CODE
final class InvalidSignupForm {
    <<FormVariable>> public ?string $name;
}
$invalidForm = new Post(InvalidSignupForm::class); // An exception will be thrown here

// VALID CODE
<<Form>>
final class ValidSignupForm {
    <<FormVariable>> public ?string $name;
}
$validForm = new Post(ValidSignupForm::class); // No exception thrown here

贡献

贡献使开源社区成为一个如此神奇的学习、灵感和创造的地方。您做出的任何贡献都受到极大的赞赏。

如果您有改进此项目的建议,请创建一个pull request。您也可以简单地打开一个带有“增强”标签的问题。别忘了给项目加星!再次感谢!

  1. 创建您的功能分支(git checkout -b feature/AmazingFeature
  2. 提交您的更改(git commit -m '添加一些AmazingFeature'
  3. 推送到分支(git push origin feature/AmazingFeature
  4. 打开Pull Request

许可证

Superglobals采用MIT许可证。