Blog
Strong typing in PHP
2007-12-28 18:20:55 by Martynas Jusevičius
Some time ago David Coallier posted a short example on how to use SPL_Types library to achieve strong typing in PHP. For example, it allows you to define explicit types using classes:
$int = new SplInt(3);
$float = new SplFloat(3.1412);Too bad that more examples or documentation are missing, but David mentions he has a tutorial in the works.
An external library is needed since PHP uses weak typing by default. There are some wishes however to have strong typing in PHP 6. It's arguable if PHP would benefit from it (and it would be a major shift in the design of the whole language), but if you prefer making your PHP code more Java-like, you might find SPL_Types useful.
Comments (19)
Don't do it.
PHP is good at being dynamic. Being dynamic is its advantage. if it becomes static it makes it an inferior version of Java/C# etc.
2007-12-28 22:22:14 by Stuart Herbert
Have to be honest; any PHP wishlist that claims that md5/sha1 are the only hash algorithms anyone needs lacks a certain credibility, don't you think? Or one that asks for multiple inheritance? Or one that asks for url rewriting support in PHP :)
I'm all for changes that make it easier to build larger architectures on PHP (such as namespaces), but changing the fundamental nature of PHP itself (static typing instead of dynamic typing) doesn't seem to bring any real benefits, except for making folks used to languages like Java a little more comfortable with PHP.
Dynamic typing is one of PHP's benefits.
Best regards,
Stu
2007-12-29 00:12:49 by Anonymous
"Let's take one advantage (dynamic typing) that PHP has, and remove it"
This is what static typing accomplishes.
2007-12-29 03:14:38 by codesnob
I tend to agree that grafting strong typing into PHP would be a mistake. The fact that the HTTP protocol is text based tells you that the best way to process web requests is as as string. However for the sake of speed, and data consistency, there does come times when being strong typed is highly useful. I moved to PHP from .NET and rejected Java for all my web facing apps. Great work DAVID! SPL ROCKS!
2007-12-29 12:44:59 by Harald Lapp
i don't think, that strong typing is always really necessary - however, custom types. would be very useful, so i think the operator overloading extension should be enabled by default in PHP. just think about "types" where you need meta-information, like a money-type where you store the currency together with the amount of money.
Agreement
Much in agreement with previous comments. Adding static typing would remove one of PHP's big benefits and would make the language *much* less attractive, at least to this developer. Need static typing? Use Java.
Documentation is coming
Hey there, the documentation is under a code freeze on php.net until the end of the year for a good revamp. As soon as it gets back on, I'll commit the docs of SPL_Types (on php.net)
Busby Seo Challenge
Hi you are good!I'am not good, though not as good as you. You have such a great potential. Keep it up!<b><a href="http://pinayspeak.com"> Busby Seo Challenge </a></b>
Static typing is good
2009-04-16 21:04:51 by Gibbs
Definitely pro-static typing. Static is beautiful. Dynamic is a mess. Bug-prone. Can't stand it. Variables can be NULL, ==0, ===0, =="", not defined, and it's not the same. Either it's a big mess or I'm full of sh*t.
I would love to get to use strong typing
2009-05-16 21:12:04 by pcguru
Not having it is definitely inferior. The language should have added support, but you could skip it if you wanted to.
Haveing strong typing would allow me to see programming mistakes in the editor instead of when the customer calls me up because of some fault occurring because of an error.
web developer
Why not optional static typing?
$userId = 4;
$userId = '12'; // no errors
---
int $userId = 4;
$userId = '12'; // generates warning or throws exception.
After all, we already have a limited form of this for type hinting arguments. Dynamic typing gives you shorter code, but static typing lets you find bugs closer to their source, and lets the IDE find some bugs for you before you even run the code.
Strong Typing Rocks!
It just popped out from my mind, why php (weak typing) is so vulnerable with sql injection, while .NET (strong typing) don't?
$name_bad = "' OR 1'";
$query_bad = "SELECT * FROM customers WHERE username = '$name_bad'";
this would lead to bad result in weak typing, while in strong typing language:
string name_bad = "' OR 1'";
string query_bad = "SELECT * FROM customers WHERE username = " + name_bad;
this wouldn't be an issue, since name_bad is considered as a string rather as a partial statement concatenation in query_bad.
this would lead to a major design change in php if it does add support for strong typing in version 6, but it would be a matter of taste whether to use strong or weak typing.
2010-07-24 19:49:31 by Jaen
Strong typing doesn't equal no dynamic typing.
Just compare with Python.
http://www.passforsure.co.uk/646-671.htm
Nice
http://www.passforsure.co.uk/1y0-a09.htm
Amazing
http://www.passforsure.co.uk/e20-340.htm
Good
http://www.passforsure.co.uk/642-515.htm
Lovely
ugg boots
i like<a href="http://www.buyuggsite.com ">ugg boots</a>,<a href="http://www.buyuggsite.com/ugg-argyle-knit-5879-boots-c-14.html ">uggs</a>,<a href="http://www.buyuggsite.com ">ugg australia</a>,<a href="http://www.buyuggsite.com ">ugg classic</a>,<a href="http://www.buyuggsite.com/ugg-argyle-knit-5879-boots-c-14.html ">ugg boots uk</a>,especlially like<a href="http://www.buyuggsite.com ">ugg classic tall</a><a href="http://www.buyuggsite.com/ugg-bailey-button-5803-boots-c-35.html ">tall ugg boots</a><a href="http://www.buyuggsite.com/ugg-bailey-button-5803-boots-c-35.html ">short ugg boots</a><a href="http://www.buyuggsite.com/ugg-classic-cardy-5819-boots-c-8.html ">uggs sale</a><a href="http://www.buyuggsite.com ">buy ugg</a><a href="http://www.buyuggsite.com/ugg-classic-cardy-5819-boots-c-8.html ">buy ugg boots</a><a href="http://www.buyuggsite.com/ugg-classic-crochet-5833-boots-c-1.html ">discount ugg</a><a href="http://www.buyuggsite.com/ugg-classic-crochet-5833-boots-c-1.html ">buy uggs</a><a href="http://www.buyuggsite.com/ugg-classic-mini-5854-boots-c-10.html ">ugg boots online</a><a href="http://www.buyuggsite.com/ugg-classic-mini-5854-boots-c-10.html ">discount uggs</a><a href="http://www.buyuggsite.com/ugg-classic-short-5825-boots-c-9.html ">buy cheap ugg boots</a><a href="http://www.buyuggsite.com/ugg-classic-short-5825-boots-c-9.html ">knitted ugg boots</a><a href="http://www.buyuggsite.com/ugg-classic-tall-5815-boots-c-7.html ">ugg boats australia</a> cheap<a href="http://www.buyuggsite.com/ugg-ultra-short-5225-boots-c-2.html ">ugg boats</a><a href="http://www.buyuggsite.com/ugg-nightfall-5359-boots-c-6.html ">buy ugg site</a><a href="http://www.buyuggsite.com/ugg-nightfall-5359-boots-c-6.html ">buy discount ugg boots</a><a href="http://www.buyuggsite.com/ugg-sundance-ii-5325-boots-c-5.html ">Ladies UGG Boots</a><a href="http://www.buyuggsite.com/ugg-sundance-ii-5325-boots-c-5.html ">UGG boots sale</a> ugg boats<a href="http://www.buyuggsite.com/ugg-classic-tall-5815-boots-c-7.html ">UGG Classic Tall 5815 Boots</a> very much.there are several styles:<a href="http://www.buyuggsite.com/ugg-argyle-knit-5879-boots-c-14.html ">ugg 5879</a><a href="http://www.buyuggsite.com/ugg-bailey-button-5803-boots-c-35.html ">ugg 5803</a><a href="http://www.buyuggsite.com/ugg-classic-cardy-5819-boots-c-8.html ">ugg 5819</a><a href="http://www.buyuggsite.com/ugg-classic-crochet-5833-boots-c-1.html ">ugg 5833</a><a href="http://www.buyuggsite.com/ugg-classic-mini-5854-boots-c-10.html ">ugg 5854</a><a href="http://www.buyuggsite.com/ugg-classic-short-5825-boots-c-9.html ">ugg 5825</a><a href="http://www.buyuggsite.com/ugg-classic-tall-5815-boots-c-7.html ">ugg 5815</a><a href="http://www.buyuggsite.com/ugg-nightfall-5359-boots-c-6.html ">ugg 5359</a><a href="http://www.buyuggsite.com/ugg-sundance-ii-5325-boots-c-5.html ">ugg 5325</a><a href="http://www.buyuggsite.com/ugg-ultra-short-5225-boots-c-2.html ">ugg 5225</a> and they are the<a href="http://www.buyuggsite.com/2010-hot-selling-boots-c-3.html ">2010 Hot Selling Boots</a>
<a href="http://www.vibram5fs.com ">vibram</a>is the world leader in high performance rubber soles, targeted to the outdoor, work, recreation, fashion, repair and orthopedic markets.<a href="http://www.vibram5fs.com ">five fingers</a> is the featured product of their. <a href="http://www.vibram5fs.com ">vibram five fingers </a> are very famous all over of the world<a href="http://www.vibram5fs.com ">five fingers vibra
watches longines
good

Mr
2007-12-28 20:14:26 by Dougal