<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
    <title>Blog of Max Horvath</title>
    <link rel="alternate" type="text/html" href="http://www.maxhorvath.com/" />
    <link rel="self" type="application/atom+xml" href="http://www.maxhorvath.com/atom.xml" />
    <id>tag:www.maxhorvath.com,2008-03-19://1</id>
    <updated>2009-05-27T09:42:34Z</updated>
    <subtitle>For more quality in software products ...</subtitle>
    <generator uri="http://www.sixapart.com/movabletype/">Movable Type Open Source 4.1</generator>

<entry>
    <title>Type Hints for scalar values - PHPTypeSafe now compatible with current PHP 5.3</title>
    <link rel="alternate" type="text/html" href="http://www.maxhorvath.com/2009/05/type-hints-for-scalar-values-phptypesafe-now-compatible-with-current-php-53.html" />
    <id>tag:www.maxhorvath.com,2009://1.33</id>

    <published>2009-05-24T14:40:22Z</published>
    <updated>2009-05-27T09:42:34Z</updated>

    <summary>Today I released the second public version of my new library PHPTypeSafe. It is now compatible with the current version of PHP 5.3 (right now it is RC2). I think we won&apos;t see any major changes with the namespace handling in PHP 5.3 anymore, so I felt I could upgrade the library.</summary>
    <author>
        <name>Max Horvath</name>
        <uri>http://www.maxhorvath.com</uri>
    </author>
    
        <category term="PHP" scheme="http://www.sixapart.com/ns/types#category" />
    
        <category term="PHPTypeSafe" scheme="http://www.sixapart.com/ns/types#category" />
    
    <category term="php" label="PHP" scheme="http://www.sixapart.com/ns/types#tag" />
    <category term="phptypesafe" label="PHPTypeSafe" scheme="http://www.sixapart.com/ns/types#tag" />
    <category term="scalarvalues" label="Scalar values" scheme="http://www.sixapart.com/ns/types#tag" />
    <category term="typehints" label="Type Hints" scheme="http://www.sixapart.com/ns/types#tag" />
    
    <content type="html" xml:lang="en" xml:base="http://www.maxhorvath.com/">
        <![CDATA[<p>
Today I released the second public version of my new library <a href="http://wiki.github.com/max-horvath/PHPTypeSafe" onClick="javascript: pageTracker._trackPageview ('/outgoing/wiki.github.com');">PHPTypeSafe</a>. It is now compatible with the current version of PHP 5.3 (right now it is RC2). I think we won't see any major changes with the namespace handling in PHP 5.3 anymore, so I felt I could upgrade the library.
</p>

<p>
One mayor move is the hosting of the source. I moved the sources from my own public subversion repository to GitHub, as the library is Open Source and may be improved more easily when using git.
</p>

<p>
PHPTypeSafe still provides the ability to use <a href="http://www.php.net/" onClick="javascript: pageTracker._trackPageview ('/outgoing/php.net');">PHP</a>s <a href="http://www.php.net/manual/en/language.oop5.typehinting.php" onClick="javascript: pageTracker._trackPageview ('/outgoing/php.net');">Type Hinting</a> feature for scalar values.
</p>

<h3><strong><big>So what does PHPTypeHint offer?</big></strong></h3>

<p>
By including PHPTypeSafe in your project you will be able to use Parameter Type Hinting for scalar values and the special type <code>resource</code>. A simple example on how to use this functionality would be:
</p>

<textarea name="code" class="php:nogutter:nocontrols" cols="60" rows="10">
<?php

require 'com.maxhorvath.phptypesafe.phar';

class Foo
{
    public static function bar(string $msg, int $counter)
    {
        echo $counter . '. run: ' . $msg . "\n\n";
    }
}

$postCount = 0;

Foo::bar('test', ++$postCount);

echo "The next run will result in an an ErrorException: \n\n";

Foo::bar(false, ++$postCount);

?>
</textarea>

<p>
The output would be:
</p>

<textarea name="code" class="html:nogutter:nocontrols" cols="60" rows="10">
1. run: test

The next run will result in an an ErrorException: 

PHP Fatal error:  Uncaught exception 'ErrorException' with message 'Argument 1 passed to com\maxhorvath\phptypesafe\Foo::bar must be of type string, boolean given, called in test.php on line 7' in test.php:7
Stack trace:
#0 test.php(19): com\maxhorvath\phptypesafe\ErrorHandler::analyzeError(4096, 'Argument 1 pass...', 'test.php', 7, Array)
#1 test.php(19): com\maxhorvath\phptypesafe\Foo::bar(false, 2)
#2 {main}
  thrown in test.php on line 7
</textarea>

<p>
PHPTypeSafe offers the support for the following types:
</p>

<ul>
    <li><code>boolean</code> (including alias <code>bool</code>)</li>
    <li><code>float</code> (including aliases <code>double</code> and <code>real</code>)</li>
    <li><code>integer</code> (including aliases <code>int</code> and <code>long</code>)</li>
    <li><code>resource</code></li>
    <li><code>string</code></li>
</ul>

<p>
Every type hint matcher is strict (so '1' won't match with the <code>integer</code> type hint), as you shouldn't use type hints if you aren't sure about the given /required type of value. (Those cases could still be checked using <code>is_string()</code>, <code>is_int()</code> and the like.)
</p>

<p>
You can get PHPTypeSafe RC2 via git at <a href="http://github.com/max-horvath/PHPTypeSafe/tree/v1.0RC2" onClick="javascript: pageTracker._trackPageview ('/outgoing/github.com');">GitHub</a> or download the <a href="http://github.com/max-horvath/PHPTypeSafe/downloads" onClick="javascript: pageTracker._trackPageview ('/outgoing/github.com');">PHAR archive</a> at GitHub.
</p>]]>
        
    </content>
</entry>

<entry>
    <title>Type Hints for scalar values - PHPTypeSafe 1.0 RC2 released</title>
    <link rel="alternate" type="text/html" href="http://www.maxhorvath.com/2008/09/type-hints-for-scalar-values-phptypesafe-10-rc2-released.html" />
    <id>tag:www.maxhorvath.com,2008://1.19</id>

    <published>2008-09-02T20:07:00Z</published>
    <updated>2009-05-27T09:43:18Z</updated>

    <summary>Today I released the first version of my new library PHPTypeSafe. It provides the ability to use PHPs Type Hinting feature for scalar values.</summary>
    <author>
        <name>Max Horvath</name>
        <uri>http://www.maxhorvath.com</uri>
    </author>
    
        <category term="PHP" scheme="http://www.sixapart.com/ns/types#category" />
    
        <category term="PHPTypeSafe" scheme="http://www.sixapart.com/ns/types#category" />
    
    <category term="php" label="PHP" scheme="http://www.sixapart.com/ns/types#tag" />
    <category term="phptypesafe" label="PHPTypeSafe" scheme="http://www.sixapart.com/ns/types#tag" />
    <category term="scalarvalues" label="Scalar values" scheme="http://www.sixapart.com/ns/types#tag" />
    <category term="typehints" label="Type Hints" scheme="http://www.sixapart.com/ns/types#tag" />
    
    <content type="html" xml:lang="en" xml:base="http://www.maxhorvath.com/">
        <![CDATA[<p>
Today I released the first public version of my new library <a href="https://dev.maxhorvath.com/trac/phptypesafe/" onClick="javascript: pageTracker._trackPageview ('/outgoing/dev.maxhorvath.com');">PHPTypeSafe</a>. It provides the ability to use <a href="http://www.php.net/" onClick="javascript: pageTracker._trackPageview ('/outgoing/php.net');">PHP</a>s <a href="http://www.php.net/manual/en/language.oop5.typehinting.php" onClick="javascript: pageTracker._trackPageview ('/outgoing/php.net');">Type Hinting</a> feature for scalar values.
</p>

<h3><strong><big>The PHP way of Type Hinting</big></strong></h3>

<p>
PHP 5 introduced Type Hinting. Functions were now able to force parameters to be objects (by specifying the name of the class in the function prototype) or arrays (since PHP 5.1). But Type Hints can only be of the object and array type. Traditional Type Hinting with <code>int</code> and <code>string</code> isn't supported. 
</p>

<p>
There were several request on the PHP internals maillinglist to introduce Type Hinting for scalar values, too. All of them got rejected, but at the moment there's <a href="http://wiki.php.net/rfc/typehint" onClick="javascript: pageTracker._trackPageview ('/outgoing/php.net');">a request for comment</a> going on at the <a href="http://wiki.php.net/" onClick="javascript: pageTracker._trackPageview ('/outgoing/php.net');">PHP Wiki</a>. Besides Type Hints for scalar values the proposals also includes Type Hinting for return values. While the first proposal can be done in the userland, the later can only be archived by changing PHP itself.
</p>

<h3><strong><big>So what does PHPTypeHint offer?</big></strong></h3>

<p>
By including PHPTypeSafe in your project you will be able to use Parameter Type Hinting for scalar values, the compound type <code>object</code> and the special type <code>resource</code>. A simple example on how to use this functionality would be:
</p>

<textarea name="code" class="php:nogutter:nocontrols" cols="60" rows="10">
<?php

require 'com.maxhorvath.phptypesafe.phar';

class Foo
{
    public static function bar(string $msg, int $counter)
    {
        echo $counter . '. run: ' . $msg . "\n\n";
    }
}

$postCount = 0;

Foo::bar('test', ++$postCount);

echo "The next run will result in an an ErrorException: \n\n";

Foo::bar(false, ++$postCount);

?>
</textarea>

<p>
The output would be:
</p>

<textarea name="code" class="html:nogutter:nocontrols" cols="60" rows="10">
1. run: test

The next run will result in an an ErrorException: 

PHP Fatal error:  Uncaught exception 'ErrorException' with message 'Argument 1 passed to com::maxhorvath::phptypesafe::Foo::bar must be of type string, boolean given, called in test.php on line 7' in test.php:7
Stack trace:
#0 test.php(19): com::maxhorvath::phptypesafe::ErrorHandler::analyzeError(4096, 'Argument 1 pass...', 'test.php', 7, Array)
#1 test.php(19): com::maxhorvath::phptypesafe::Foo::bar(false, 2)
#2 {main}
  thrown in test.php on line 7
</textarea>

<p>
PHPTypeSafe offers the support for the following types:
</p>

<ul>
    <li><code>boolean</code> (including alias <code>bool</code>)</li>
    <li><code>float</code> (including aliases <code>double</code> and <code>real</code>)</li>
    <li><code>integer</code> (including aliases <code>int</code> and <code>long</code>)</li>
    <li><code>object</code> (generic object matcher)</li>
    <li><code>resource</code></li>
    <li><code>string</code></li>
</ul>

<p>
Every type hint matcher is strict (so '1' won't match with the <code>integer</code> type hint), as you shouldn't use type hints if you aren't sure about the given /required type of value. (Those cases could still be checked using <code>is_string()</code>, <code>is_int()</code> and the like.)
</p>

<h3><strong><big>Requirements and where to get it</big></strong></h3>

<p>
PHPTypeSafe has been written using <a href="http://www.php.net/language.namespaces" onClick="javascript: pageTracker._trackPageview ('/outgoing/php.net');">namespaces</a> and is being packaged as a <a href="http://www.php.net/manual/en/book.phar.php" onClick="javascript: pageTracker._trackPageview ('/outgoing/php.net');">Phar</a> PHP Archive. That's why it requires you to use PHP 5.3 (or later).
</p>

<p>
You can download PHPTypeSafe at <a href="https://dev.maxhorvath.com/trac/phptypesafe/downloader/download/category/2" onClick="javascript: pageTracker._trackPageview ('/outgoing/dev.maxhorvath.com');">dev.maxhorvath.com</a>.
</p>

<h3><strong><big>Feedback</big></strong></h3>

<p>
I would be very happy to receive your feedback on how you like the library and if there are any issues on how to use it.
</p>]]>
        
    </content>
</entry>

</feed> 