Type-safe access to php superglobals.
Class representing the $_SERVER superglobal.
Example of accessing 'REQUEST_METHOD' on $_SERVER:
use type Superglobals\Server;
$server = new Server();
$server->$REQUEST_METHOD; // 'GET', 'HEAD', 'POST', 'PUT', etc. or null
Example of using the raw $_SERVER superglobal:
use type Superglobals\Server;
$unsafeServer = Server::_UNSAFE();
$unsafeServer['REQUEST_METHOD']; // 'GET', 'HEAD', 'PUT', etc. or null
$unsafeServer['UNKNOWN_VARIABLE']; // Could be anything
Class representing the $_GET superglobal.
Example of using $_GET with a user defined query string:
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
Class representing the $_POST superglobal.
Example of using $_POST with a user defined form:
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
Class representing the $_COOKIE superglobal.
Example of using $_COOKIE with user defined cookies:
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'
Class representing the $_SESSION superglobal.
Example of using $_SESSION with user defined properties:
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
This exception is thrown when you are not using a class attribute on the class being instantiated by one of the superglobals class defined in this library.
Here is an example using $_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
Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.
If you have a suggestion that would make this better, please create a pull request. You can also simply open an issue with the tag "enhancement". Don't forget to give the project a star! Thanks again!
- Create your Feature Branch (
git checkout -b feature/AmazingFeature
) - Commit your Changes (
git commit -m 'Add some AmazingFeature'
) - Push to the Branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
Superglobals is MIT-licensed.