-
Notifications
You must be signed in to change notification settings - Fork 0
bigwhoop/TypeHintMe
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
I had this idea about using the __call magic method to implement a simple form of type hinting in PHP. Because we all already use phpDocumentor to comment our methods, I thought it would be pretty handy to just use the @param tag as the invoker. An example: <?php class Auth { /** * @param string $username * @param string $password * @param bool $regenerateSessionId */ public function authenticate($username, $password, $regenerateSessionId) {} } ?> In order to catch all calls to the Auth->authenticate() method we need to wrap something aroung the Auth class. Here comes TypeHintMe's ObjectProxy into play. <?php namespace App; use BigWhoop\TypeHintMe; require 'Auth.php'; require 'TypeHintMe.phar'; $auth = new Auth(); $auth->authenticate('Philippe', 'Gerber', 1); // No error $auth = new TypeHintMe\ObjectProxy($auth); $auth->authenticate('Philippe', 'Gerber', true); // No error $auth->authenticate('Philippe', 'Gerber', 1); // Error: Argument 3... ?> If you want to validate a constructor as well you have to use the ObjectProxyFactory->create() method. <?php $constructorParams = array('alwaysRegenerateSessionId' => true); $auth = TypeHinteMe\ObjectProxyFactory::create('App\Auth', $constructorParams); ?> That's about it. :) PS: Oh, and yeah, TypeHintMe is not meant for production work. But it can come in pretty handy if you want to track grubby method calls inside your app. :]
About
Prototype/Proof-of-concept for runtime testing of type hints in PHP 5.3. Runtime checks according to DocBlock comments. ;-)
Resources
Stars
Watchers
Forks
Releases
No releases published
Packages 0
No packages published