News Feed
Sections




News Archive
feed this:

Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Nikita Popov's Blog:
Scalar type hinting is harder than you think
March 07, 2012 @ 10:03:47

In this new post to his blog Nikita talks about scalar type hinting and why it's harder than most people think to accomplish.

One of the features originally planned for PHP 5.4 was scalar type hinting. But as you know, they weren't included in the release. Recently the topic has come up again on the mailing list and there has been a hell lot of discussion about it. Yesterday ircmaxell published a blog post about his particular proposals. The reactions on reddit were mixed. On one hand it is clear that people do really want scalar type hints, on the other hand they didn't seem to like that particular proposal.

He gets into some of the details of some of the current proposals and their problems like the strict versus loosely-typed nature of PHP and type hinting that was included but not enforced. One he does like, however, is one based on casting - how the variable ends up being cast rather than the specific type it is when it comes into the function/method. This one still has its flaws, so he suggests another method - weak type hints but with stricter input validation (without casting). He also briefly mentions something called "box based type hinting" that would allow users to define their own hinting rules.

Don't worry - code examples (pseudo-code obviously) are included for each of these proposals to help you understand the differences.

0 comments voice your opinion now!
type hinting static strict looselytyped proposal scalar


Sebastian Bergmann's Blog:
Scalar Type Hints in PHP 5.3.99
November 24, 2010 @ 10:04:06

In the theme of other recent posts mentioning the scalar type hinting that has been included in the main line of code that is headed towards the next PHP release, Sebastian Bergmann has a new post about their inclusion in PHP 5.3.99 (yes, that's PHP 5.4) and the new syntax it introduces.

In a nutshell, this means that PHP 5.3.99 introduces new syntax -- scalar type hints -- but no new semantics. The latter can either be implemented as an extension written in C/C++, in userland PHP code, or in a tool that statically analyzes the code.

He includes an example fro userland with a "php_check_parameters" function that looks at the arguments of the current method and uses Reflection to check against the type hints for the correct value type.

0 comments voice your opinion now!
reflection scalar type hint feature userland


Johannes Schluter's Blog:
More on scalar type hints in PHP trunk
November 23, 2010 @ 09:13:24

Johannes Schluter has posted a bit more information about the scalar type hinting that's been included in the main development line of the PHP language (trunk).

Some time ago I wrote an article about the implementation of type hints for non-object types for PHP. Meanwhile many things happened and that implementation was replaced by a different one. Readers of my previous post might know that I have doubts about type hints in PHP.

He shows some of the example syntax for the hinting and points out how, in one case, there's no error thrown when the type hint is a native one but an error is thrown on a custom type hint.

So why is there a syntax added which is ignored? [...] Well, I let it to you to decide whether it make sense to have two syntaxes which look the same but do very different things (being ignored vs. throwing a terminating error) and whether it makes sense to push a system where the core language behaves differently depending on the system. [...] I seriously hoped PHP was out of the era of introducing new inconsistencies.
0 comments voice your opinion now!
scalar type hints trunk inconsistent


Johannes Schluter's Blog:
Scalar type hints in PHP trunk
August 09, 2010 @ 08:44:23

As Johannes Schluter mentions in his latest blog post, another new feature has been added to the trunk of the PHP project - scalar type hints.

So in my blog series I try to cover all additions to PHP trunk so I have to mention scalar type hints.

He gives examples of both simple hinting (ensuring a value is a float) and a more complex example setting an attribute on a PDO connection (checking for an integer). Sample error messages are included as well. He also gives some advice on mixing strong and weak typing in your apps - it's like "opening a can of worms".

0 comments voice your opinion now!
scalar type hint trunk feature language


Patrick Allaert's Blog:
Readable PHP code #2 - Make your API handle more!
May 28, 2010 @ 08:41:22

In a new post today Patrick Allaert has continued his "Readable Code" series with some tips on how you can make your API handle more.

His suggestions include:

  • Changing the handling to look for multiples by default
  • Avoiding inner looping (repeated looping) and try for code reuse
  • Transforming a scalar value into an array to make it easier to handle this way (by casting)
  • and how to handle objects in the same array-handling kind of code

He also tosses in a little mention of performance, noting that a call with range in the argument to his array-handling method is faster than looping over the same range and calling the method each time.

0 comments voice your opinion now!
readable api handle array object scalar tip


Ilia Alshanetsky's Blog:
Scalar Type Hints are Here!
May 24, 2010 @ 11:48:19

Ilia Alshanetsky has a quick new post with some great news for some PHP developers out there - scalar type hinting has been introduced into the latest trunk of the PHP project.

About an hour ago, something I've been fighting for almost 2 years happened. The Scalar Type Hinting patch for PHP (the one I wrote almost a year ago) has been adjusted for PHP's trunk tree and committed by Derick.

You can see some of the type hinting features already included in PHP in the manual. This patch adds hinting ability for the other (non-object and non-array) variable types.

1 comment voice your opinion now!
scalar type hint trunk patch


Ilia Alshanetsky's Blog:
Type hinting for PHP 5.3
July 02, 2009 @ 09:46:18

One of the features included in the PHP 5.x releases is type hinting for validating that you have the right values for your functions and methods coming in. In this new post to his blog Ilia Alshanetshy takes a look at the the feature and gives a patch to add it .

On a general level most people agree it would be a good idea to have, since it is an optional feature and does not introduce any regressions, heck you can even mix type hinted code with the non-type hinted one. The "PROBLEM" has always been combining of PHP's typeless nature with type hinting, which is where the consensus has been difficult (impossible) to reach.

He mentions an example of why its such an issue (technically, both 1 and "1" are valid numbers) and includes a link to a patch that gives a new hint to help with the problem.

0 comments voice your opinion now!
patch scalar numeric typehinting


PHP in Action:
Type hints are more useful for scalars than objects
September 11, 2008 @ 10:08:33

On the PHP in Action blog, there's a new post looking at a recent library that was posted to support type hinting on scalars. They agree with his choice of subjects, noting that they see type hinting as much more useful on scalars than on objects.

I admit that these judgments are hard to make. I could be wrong, more or less. Type hints are probably useful when code becomes stable enough and at the boundaries between modules. But I still tend to avoid using them until I get an actual bug that might have been prevented by a type hint. Their usefulness is and has to be an empirical question. The purpose of using them has to be catching errors earlier, so if they don't have that effect, there's no point.

He lists three reasons why he had given up on type hinting before, one being the limited usefulness when it came to objects. Applying it to scalars is a different matter, though, and can prevent improper passing of array/scalars when the other is needed.

0 comments voice your opinion now!
type hinting scalar object array string class library


Derick Rethans' Blog:
Scalar Type Hinting and DB Design Slides
June 17, 2006 @ 18:52:04

Derick Rethans has two blog posts for us today, the first details a patch he created which allows us to use type hints for all variable types in PHP.

Derick himself admits that its probably not the best approach:

The patch is most likely not the best way of implementing this either, and I am also not sure if this patch should be applied to PHP's CVS at all.

He offers the following as an example of how this would be used:

function foo2( integer $i, float $f, bool $b ) { }
foo2( 'string', 42, 49.9 );

The example above should show:

Catchable fatal error: Argument 1 passed to foo2() must be of type integer, string given, called in /tmp/test.php on line 16 and defined in /tmp/test.php on line 12

Derick also posted his Database Design Slides from his talk earlier this year at php|architects php|tek

0 comments voice your opinion now!
database design scalar typehints database design scalar typehints



Community Events





Don't see your event here?
Let us know!


project list threedevsandamaybe laravel introduction podcast bugfix series code application library interview developer release community wordpress api language framework configure

All content copyright, 2014 PHPDeveloper.org :: info@phpdeveloper.org - Powered by the Solar PHP Framework