Skip to content

Prototype/Proof-of-concept for runtime testing of type hints in PHP 5.3. Runtime checks according to DocBlock comments. ;-)

Notifications You must be signed in to change notification settings

bigwhoop/TypeHintMe

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 

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

No packages published

Languages