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

Johannes Schlüter:
Types in PHP and MySQL
Sep 05, 2016 @ 18:38:21

Johannes Schlüter has a post to his site detailing the handling of types in PHP and MySQL and how they might act differently than expected in some situations.

Since PHP 7.0 has been released there's more attention on scalar types. Keeping types for data from within your application is relatively simple. But when talking to external systems, like a database things aren't always as one eventually might initially expect.

He talks about MySQL types and how they relate to the "network protocol" being used, converting everything to strings. He includes a few examples of hinting on the results, one where an integer is expected/string provided and another where a string was type hinted but an integer was returned. He points out that sometimes this is a limitation of what PHP can handle, not always what MySQL returns. He also includes other examples of returning decimals - sometimes as a number value and others as a string.

This leaves the question whether you should disable the emulation in order to get the correct types. Doing this has some impact on performance characteristics: With native prepared statements there will be a client-server round-trip during the prepare and another round-trip for the execute.
tagged: types typehinting mysql database string integer decimal preparedstatement pdo

Link: http://schlueters.de/blog/archives/182-Types-in-PHP-and-MySQL.html

Slawek Lukasiewicz's Blog:
Throwing Exception with Type Hinting Failed
Aug 10, 2011 @ 14:58:57

Slawek Lukasiewicz has a helpful hint posted to his blog today about handling type hinting failures in a what he sees as a "more correct" way than just throwing an error - throwing an exception when the hinting criteria's not met.

One of the most annoying behavior of PHP type hinting feature is triggering error instead of throwing exception when instance type is invalid.

By default, a failure generates a "catchable error" that, thanks to the custom error handling methods PHP includes, can be correctly handled. He includes a snippet of code that defines the errorHandler method that checks the error type for E_RECOVERABLE_ERROR and throws the exception (an ErrorException) if found.

tagged: typehinting exception failure catchable error handling custom

Link:

Jani Hartikainen's Blog:
PHP typehinting gotcha in exceptions/methods
Jul 14, 2011 @ 15:41:55

Jani Hartikainen has a new post with a gotcha he came across when using typehinting in exceptions and methods.

A small gotcha I recently ran into: If you use typehinting, PHP never checks that the class your typehint specifies actually exists!

He includes simple examples that define the class type hint as some random string and PHP never throws an error for it. He also notes that it's even worse when dealing with namespaces - how it handles exceptions locally unless you specify otherwise. Of course, these "features" can be put to good use in the right situations, but they can be confusing for a developer without a keen eye.

tagged: typehinting exception method error class missing

Link:

Mike Willbanks' Blog:
PHP Type Hinting
Jul 08, 2009 @ 17:57:11

There's been lots of talk about type hinting lately (both on blogs and on mailing lists) so Mike Willbanks has decided to add some of his own thoughts about why it should be included.

You may be wondering why you would want type hinting in a dynamic language. This can come in very handy especially if you are looking to enforce a specific type or need to ensure that you are indeed working with say an integer. Since this is written in C, it will be far faster than actually type casting it yourself or enforcing it yourself. General speed improvements are always nice to have.

He includes two code examples of it in use and points to the php.internals mailing list for the vote count either way. So far (as of the time of those post) the results seem mixed with some recommending it outright while others suggest waiting until the spec is completely done before integration (PHP6?).

tagged: mailinglist phpinternals typehinting

Link:

Ilia Alshanetsky's Blog:
Type hinting rehashed (now with type casting support)
Jul 07, 2009 @ 12:52:37

Ilia Alshanetsky takes a look at type hinting in PHP (he's made some previous comments on the matter) and some of the community's comments about its suggested inclusion in the language.

There has been a lot of comments both on this blog and the internals list. There seems to be a fairly large group of core developers who like the idea as well as surpassingly large support base on the user level too (wow, didn't think that this many people want type hinting).

Despite some of the nay-sayers who don't think it's a good idea, the majority seems to approve and work has already been done on a new patch you can apply to your systems. He includes the Changelog information as well as links to the patch (txt) and a test suite (bz2).

tagged: patch cast type typehinting

Link:

Ilia Alshanetsky's Blog:
Type hinting for PHP 5.3
Jul 02, 2009 @ 14: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.

tagged: patch scalar numeric typehinting

Link:

Marco Tabini's Blog:
Thoughts for a new year: PHP as the new Java
Dec 19, 2007 @ 19:47:00

Marco Tabini (of php|architect magazine) has posted some of his thoughts on why PHP just might be the "new Java" in the upcoming year.

Historically, PHP has been developed following the scratch-an-itch method [...]. This development process makes for some haphazard functionality, and has, in the past, been the source of many annoyances that need to be handled with extreme care.

He does mention some of the good things that have come out of this process like OOP in PHP4, SimpleXML functionality and type hinting but warns that if things swing too much the other way (planning out everything, only adding things by committee) that PHP might swerve towards a dangerous precedent - the Java community's mentality.

tagged: java thought opinion oop php4 simplexml typehinting java thought opinion oop php4 simplexml typehinting

Link:

Marco Tabini's Blog:
Thoughts for a new year: PHP as the new Java
Dec 19, 2007 @ 19:47:00

Marco Tabini (of php|architect magazine) has posted some of his thoughts on why PHP just might be the "new Java" in the upcoming year.

Historically, PHP has been developed following the scratch-an-itch method [...]. This development process makes for some haphazard functionality, and has, in the past, been the source of many annoyances that need to be handled with extreme care.

He does mention some of the good things that have come out of this process like OOP in PHP4, SimpleXML functionality and type hinting but warns that if things swing too much the other way (planning out everything, only adding things by committee) that PHP might swerve towards a dangerous precedent - the Java community's mentality.

tagged: java thought opinion oop php4 simplexml typehinting java thought opinion oop php4 simplexml typehinting

Link:

Padraic Brady's Blog:
Namespaces (or Yet Another Pointless Opinion Piece)
Dec 14, 2007 @ 18:01:00

Adding even more to the huge namespaces debates going on, Padraic Brady has some of his opinions posted to his blog on the subject.

The problem isn't that namespaces are bad, it's that these arcane structures seem to have so little impact on development in PHP today for many many people. Does that mean it's unimportant?

He talks about the real value of namespaces, a comparison to type hinting's inclusion, the current amount of (needless?) traffic on the php.internals list and his opinions on why namespaces are valid.

tagged: namespace discussion opinion typehinting phpinternals namespace discussion opinion typehinting phpinternals

Link:

Padraic Brady's Blog:
Namespaces (or Yet Another Pointless Opinion Piece)
Dec 14, 2007 @ 18:01:00

Adding even more to the huge namespaces debates going on, Padraic Brady has some of his opinions posted to his blog on the subject.

The problem isn't that namespaces are bad, it's that these arcane structures seem to have so little impact on development in PHP today for many many people. Does that mean it's unimportant?

He talks about the real value of namespaces, a comparison to type hinting's inclusion, the current amount of (needless?) traffic on the php.internals list and his opinions on why namespaces are valid.

tagged: namespace discussion opinion typehinting phpinternals namespace discussion opinion typehinting phpinternals

Link:


Trending Topics: