SitePoint Sponsor |
|
User Tag List
View Poll Results: Static Typing:
- Voters
- 45. You may not vote on this poll
-
Yes
11 24.44% -
Maybe
2 4.44% -
No
30 66.67% -
Don't Care
2 4.44%
Results 26 to 50 of 85
-
Aug 29, 2008, 06:55 #26
- Join Date
- May 2006
- Location
- Lancaster University, UK
- Posts
- 7,062
- Mentioned
- 2 Post(s)
- Tagged
- 0 Thread(s)
Ok, I see where I went too Java in this.
So here's a refinement of what I think PHP should have (optionally):
- Return types specified. Any syntax, as long as you can. Personally, I'd prefer:
PHP Code:<?php
string uselessFunction($string){
return $string; //must be a string.
}
?>
Another way could be:
PHP Code:<?php
function uselessFunction($string) returns string{
return $string;
}
?>
PHP Code:<?php
function uselessFunction($string) _ string{
}?> - More type hinting! Yeah, you can specify a type of object or implementation. I mean full:
PHP Code:function uselessFunction(string $string){
- The ability to use an ordinary variable as it's object. For example:
PHP Code:$str = "hello";
echo $str->replace('h', 'H');
All of the above wouldn't mess up current code, but it would surely help make code cleaner and more structured.
Agree?Jake Arkinstall
"Sometimes you don't need to reinvent the wheel;
Sometimes its enough to make that wheel more rounded"-Molona
-
Aug 29, 2008, 06:55 #27
- Join Date
- Sep 2004
- Location
- Zagreb, Croatia
- Posts
- 830
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Just a few thoughts of my own:
Static typing adds nothing to object-oriented programming.
In PHP5, typehints and interfaces are a waste of time. They only make sense if worked together, and they are not necessary at all precisely because PHP is dynamically typed. Duck typing beats static typing any time.
Static typing makes sense only for low-level languages which operate only on basic types, such as C. This allows programmers to have maximum control over their use of system resource, which is in high-level languages such as PHP taken care of by the garbage collectors.
-
Aug 29, 2008, 07:03 #28
- Join Date
- May 2006
- Location
- Lancaster University, UK
- Posts
- 7,062
- Mentioned
- 2 Post(s)
- Tagged
- 0 Thread(s)
Interfaces are most definitely not a waste of time.
If you give yourself restraint, your code is more stable.Jake Arkinstall
"Sometimes you don't need to reinvent the wheel;
Sometimes its enough to make that wheel more rounded"-Molona
-
Aug 29, 2008, 07:35 #29
- Join Date
- Oct 2006
- Location
- Bucharest, Romania
- Posts
- 143
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
A patch for hinting scalar types like int, string, boolean has already been done and proposed but didn't make it. Among the emails from the php-internals list that I read, some people suggested that would be more useful to hint the general type, scalar or non-scalar. Like:
PHP Code:function foo(scalar $param) {}
Honestly, I don't think type hinting is what PHP needs, but lambdas and namespaces (which we'll have with PHP 5.3). More means of organizing our programs.
I don't want PHP bloated in a Java style. It already has a lot of quirks with function names and param order and parser problems. Python doesn't have the type-hinting facilities PHP has right now, yet, there are people that do very nice programs with it.I'm under construction | http://igstan.ro/
-
Aug 29, 2008, 07:47 #30
- Join Date
- May 2006
- Location
- Lancaster University, UK
- Posts
- 7,062
- Mentioned
- 2 Post(s)
- Tagged
- 0 Thread(s)
I'm not talking about the result of the PHP application - the result is the same.
However if you create a set of classes for PHP and they NEED to be certain types etc, you should be able to define them and let PHP handle errors when they don't suit - rather than going through that horrible type-validation stage.Jake Arkinstall
"Sometimes you don't need to reinvent the wheel;
Sometimes its enough to make that wheel more rounded"-Molona
-
Aug 29, 2008, 08:14 #31
- Join Date
- Dec 2005
- Location
- Essex, UK
- Posts
- 241
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by arkinstall
Originally Posted by arkinstall
Originally Posted by arkinstall
-
Aug 29, 2008, 08:32 #32
- Join Date
- May 2006
- Location
- Lancaster University, UK
- Posts
- 7,062
- Mentioned
- 2 Post(s)
- Tagged
- 0 Thread(s)
Technically everything wouldn't be an object.
The parser simply notices the object notation isn't connected to an object but a variable. It then verifies the type of variable and passes it to predefined functions for that particular variable type.
Kind of like general functions but with the parameter being it's 'host'.
There are alot of possibilities for something like that. You could create your own defined functions for variables:
PHP Code:<?php
function getFirst() for string $str{
return $str[0];
}
$var = 'hello';
echo $var->getFirst(); //output: hJake Arkinstall
"Sometimes you don't need to reinvent the wheel;
Sometimes its enough to make that wheel more rounded"-Molona
-
Aug 29, 2008, 08:56 #33
- Join Date
- Sep 2004
- Location
- Zagreb, Croatia
- Posts
- 830
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I have argued this extensively in other threads on this forum, so I won't repeat all the arguments here. But most briefly, what define's an object's type is not its class, but its behavior -- which is actually the collection of its public properties.
Try reading a bit about duck typing.
-
Aug 29, 2008, 08:56 #34
- Join Date
- Jul 2005
- Posts
- 606
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I think the type-hinting that's already available is sufficient, one of the things that has helped the language grow over the years is it's expressiveness and dynamic nature, to drastically change that, or to crowbar such functionality into it would be a huge mistake.
Good applications will have measures in place to mitigate the risks of data integrity problems already, or they should do.
These sort of suggestions are quite typical of people that are new to the language, if you've been working with it for several years it's quite incomprehensible to imagine it being as strict as Java or C. If anything we should want to increase the expressiveness of PHP, not decrease it. Things like lexical scope, more convenient short-hands and better support for iterators would be a move in the right direction.
-
Aug 29, 2008, 09:15 #35
- Join Date
- Jun 2004
- Location
- Copenhagen, Denmark
- Posts
- 6,157
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
-
Aug 29, 2008, 09:30 #36
- Join Date
- May 2006
- Location
- Lancaster University, UK
- Posts
- 7,062
- Mentioned
- 2 Post(s)
- Tagged
- 0 Thread(s)
Not necessarily. An integer can pass as a string. But if you're expecting a boolean and 29 or "leaf" is passed, there are problems.
Basically variables passed are attempted to be cast as the variable the function accepts. If it's a string, 2 will pass. If it's a number, "52" would pass. If it's a boolean, 0 and 1 would pass, "true" and "false" would pass, too.Jake Arkinstall
"Sometimes you don't need to reinvent the wheel;
Sometimes its enough to make that wheel more rounded"-Molona
-
Aug 29, 2008, 09:47 #37
- Join Date
- May 2004
- Location
- Central USA
- Posts
- 806
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
That idea wouldn't really go over well, and defeats the whole purpose of requiring a strict type anyway - I can just see the list of exceptions now - what about NULL? What about objects that have a __toString() method defined? I imagine some sort of table would have to be created that would then map out what type actually accepts which other types and would look something like these ... it would be a mess, and in my opinion, a mistake for PHP.
I agree with the sentiment that strict type hinting would go against the very core dynamic nature of PHP, and shouldn't be done. There are a few times when it may be nice, but the type casting added in PHP5 remedies most of those cases for me.Stackbox CMS - Full edit-on-page drag-and-drop CMS
Autoridge - Vehicle information & maintenance part numbers
Twitter | Blog | Online Javascript Compressor
-
Sep 1, 2008, 00:52 #38
- Join Date
- Jan 2004
- Location
- Oslo, Norway
- Posts
- 894
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I came across a "blitz interview" in which Bruce Tate is asked
Imagine, that you have the right to add five Ruby language elements to the next version of Java. What would they be? Can you include quick code samples as well?
This might apply equally to PHP.Dagfinn Reiersøl
PHP in Action / Blog / Twitter
"Making the impossible possible, the possible easy,
and the easy elegant" -- Moshe Feldenkrais
-
Sep 1, 2008, 01:12 #39
- Join Date
- Jul 2005
- Posts
- 606
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Not necessarily. An integer can pass as a string. But if you're expecting a boolean and 29 or "leaf" is passed, there are problems.
Basically variables passed are attempted to be cast as the variable the function accepts. If it's a string, 2 will pass. If it's a number, "52" would pass. If it's a boolean, 0 and 1 would pass, "true" and "false" would pass, too.
-
Sep 1, 2008, 04:33 #40
- Join Date
- May 2006
- Location
- Lancaster University, UK
- Posts
- 7,062
- Mentioned
- 2 Post(s)
- Tagged
- 0 Thread(s)
It makes perfect sense.
Basically if a parameter is expected to be one type, the passed variable is attempted to be cast to that type. If it can't be (i.e. a boolean to a string) then it errors.
So to rephrase that extract:
An integer can be cast to a string: 29 == "29".
An integer 29 can't be cast to boolean realistically
A string 'leaf' can't be cast to a boolean
The string "52" can be cast to an integer.
integers 0 and 1, and strings "true" and "false" can both be cast to a boolean.
That's how it already works - I'm just suggesting that it be carried out with function parameters too.Jake Arkinstall
"Sometimes you don't need to reinvent the wheel;
Sometimes its enough to make that wheel more rounded"-Molona
-
Sep 1, 2008, 05:16 #41
- Join Date
- Sep 2004
- Location
- Zagreb, Croatia
- Posts
- 830
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Hm -- and why not '1D'? or 11101'?
It's not working quite as you're saying (string 'leaf' is cast to boolean as TRUE), but I get what you mean -- a typehint for basic type would implicitly call a typecast, i.e. function foo (string $bar) would have the same effect as calling the function as foo((string) $bar). I could actually live with that; however, this would mean that you have one syntax for two different effects (type check and typecast), which is a major no-no.
-
Sep 1, 2008, 05:23 #42
- Join Date
- May 2006
- Location
- Lancaster University, UK
- Posts
- 7,062
- Mentioned
- 2 Post(s)
- Tagged
- 0 Thread(s)
why not '1D'? or 11101'?
this would mean that you have one syntax for two different effects
For one the typecast is in brackets whilst the type check isn't. Secondly the typecheck exists only in the function definition, where the typecasting would error.
I do beleive, however, that 'leaf' shouldn't cast to boolean. That is a big flaw and is, to be honest, pointless.Jake Arkinstall
"Sometimes you don't need to reinvent the wheel;
Sometimes its enough to make that wheel more rounded"-Molona
-
Sep 1, 2008, 05:37 #43
- Join Date
- Nov 2006
- Posts
- 50
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
-
Sep 1, 2008, 05:41 #44
- Join Date
- May 2006
- Location
- Lancaster University, UK
- Posts
- 7,062
- Mentioned
- 2 Post(s)
- Tagged
- 0 Thread(s)
That should technically be:
PHP Code:if (mysql_errno() > 0){
// handle error
}
Jake Arkinstall
"Sometimes you don't need to reinvent the wheel;
Sometimes its enough to make that wheel more rounded"-Molona
-
Sep 1, 2008, 06:22 #45
-
Sep 1, 2008, 06:41 #46
- Join Date
- May 2006
- Location
- Lancaster University, UK
- Posts
- 7,062
- Mentioned
- 2 Post(s)
- Tagged
- 0 Thread(s)
Not necessarily.
If the expected value is a scalar value, cast the passed variable to it. Otherwise just check it's the same type (or an implementation/extention thereof) and throw an exception otherwise.Jake Arkinstall
"Sometimes you don't need to reinvent the wheel;
Sometimes its enough to make that wheel more rounded"-Molona
-
Sep 1, 2008, 11:15 #47
-
Sep 1, 2008, 11:28 #48
- Join Date
- May 2006
- Location
- Lancaster University, UK
- Posts
- 7,062
- Mentioned
- 2 Post(s)
- Tagged
- 0 Thread(s)
Not really - it's the same thing. In both situations a cast is attempted.
The difference is that objects and scalars can't be cast to other objects.Jake Arkinstall
"Sometimes you don't need to reinvent the wheel;
Sometimes its enough to make that wheel more rounded"-Molona
-
Sep 1, 2008, 17:54 #49
- Join Date
- May 2004
- Location
- Central USA
- Posts
- 806
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I actually wouldn't mind dynamic typecasting of function parameters, either. I can think of a ton of potential great uses for that:
PHP Code:function getUser((int) $userId) {
// Now $userId is safe to use directly in a query because it has been cast to INT...
}
function doSomeIteration((array) $someArray) {
// We can now loop over this fine because it will always be an array...
}
Stackbox CMS - Full edit-on-page drag-and-drop CMS
Autoridge - Vehicle information & maintenance part numbers
Twitter | Blog | Online Javascript Compressor
-
Sep 1, 2008, 22:38 #50
- Join Date
- Sep 2004
- Location
- Zagreb, Croatia
- Posts
- 830
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Actually, Czaries, putting the type into the parentheses, as you did in your examples, kinda solves the issue.
Bookmarks