Hello everyone
I figured I would bring my opinion in to support of Sam's request for a more
complete type hinting feature. Namely I am interested in the support for
hinting scalar types on function and method arguments and I am sure it is
safe for me to say that I speak for a lot of people. Most people that I know
find the current type hinting, while useful, ridiculous because it looks like
the job was left unfinished for whatever abstract reason.
In my opinion type hinting should definitely be allowed for scalar values. As
for return types, I am not so sure. So far I have found no use for such a
feature in my own code, so I won't comment on it. If it is added, I welcome
it for those who find it useful but if it is not added I will not loose sleep
over it.
I was thinking at something along the lines of objects also for instance:
$i = new Integer(33);
After my own experiments with the subject I concur that while it can be made
to work, it is not only a bad idea (for the reasons mentioned earlier) it is
also redundant and just unnecessary. There is a lot better way to accomplish
the same and that by allowing scalar values to be hinted. It is simpler,
cleaner, and easier to implement.
What if type hinting just generated an E_NOTICE. Nothing more for the
time being.
Changing it toE_NOTICE
orE_STRICT
defeats the purpose somewhat since if I
write a piece of code that hints that the argument for a-whatever method
needs to be an integer it seems useless if the user of my library can avoid
the issue just by supressing lesser errors and those who do not need to write
extensive error handling code to respond to this sort of error (if they
indeed deem it necessary to do so).
While hinting is, and should remain, optional, when it is used it should be
enforced. After all the user of my library has the option to dump it and go
for another library that does not force types. That is the beauty of having
options.
Tomi Kaistila
PHP Developer
I think E_WARNING
would be appropriate. That's what happens when you
omit an argument to a function right?
And about function return type hinting, I don't think it would be as
useful as parameter type hinting, but it would be useful. Mostly for
stuff like declaring an abstract function in a parent class that must
return a certain type.
class a {
abstract public function getNumber() returns int ;
}
class b extends a {
public function getNumber() {
return rand()
* 3463 ;
}
}
class c extends a {
public function getNumber() {
return 'I'm going to mess everything up by returning a string.' ; //
Would cause error with type hinting.
}
}
Hello everyone
I figured I would bring my opinion in to support of Sam's request for a more
complete type hinting feature. Namely I am interested in the support for
hinting scalar types on function and method arguments and I am sure it is
safe for me to say that I speak for a lot of people. Most people that I know
find the current type hinting, while useful, ridiculous because it looks like
the job was left unfinished for whatever abstract reason.In my opinion type hinting should definitely be allowed for scalar values. As
for return types, I am not so sure. So far I have found no use for such a
feature in my own code, so I won't comment on it. If it is added, I welcome
it for those who find it useful but if it is not added I will not loose sleep
over it.I was thinking at something along the lines of objects also for instance:
$i = new Integer(33);
After my own experiments with the subject I concur that while it can be made
to work, it is not only a bad idea (for the reasons mentioned earlier) it is
also redundant and just unnecessary. There is a lot better way to accomplish
the same and that by allowing scalar values to be hinted. It is simpler,
cleaner, and easier to implement.What if type hinting just generated an E_NOTICE. Nothing more for the
time being.
Changing it toE_NOTICE
orE_STRICT
defeats the purpose somewhat since if I
write a piece of code that hints that the argument for a-whatever method
needs to be an integer it seems useless if the user of my library can avoid
the issue just by supressing lesser errors and those who do not need to write
extensive error handling code to respond to this sort of error (if they
indeed deem it necessary to do so).While hinting is, and should remain, optional, when it is used it should be
enforced. After all the user of my library has the option to dump it and go
for another library that does not force types. That is the beauty of having
options.Tomi Kaistila
PHP Developer
I think
E_WARNING
would be appropriate. That's what happens when you
omit an argument to a function right?
The other thing to note about type hinting is that it could result in faster code.
The value is checked and if needed converted (eg string -> integer) when the
function is called; it then need not be converted again when it is used in
the function. This will presumably be a win if the value is used more than once.
--
Alain Williams
Linux Consultant - Mail systems, Web sites, Networking, Programmer, IT Lecturer.
+44 (0) 787 668 0256 http://www.phcomp.co.uk/
Parliament Hill Computers Ltd. Registration Information: http://www.phcomp.co.uk/contact.php
Chairman of UKUUG: http://www.ukuug.org/
#include <std_disclaimer.h
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Hello,
Alain Williams wrote:
I think
E_WARNING
would be appropriate. That's what happens when you
omit an argument to a function right?The other thing to note about type hinting is that it could result in faster code.
The value is checked and if needed converted (eg string -> integer) when the
function is called; it then need not be converted again when it is used in
the function. This will presumably be a win if the value is used more than once.
I don't think that automagic conversion of types is a good idea.
When the type hint says I want an integer, then only integer should be
accepted; no casting should be done. It may give predictable results
according to the documentation but not always very intuitive. I sense a
possible small trap, and after all type hints should help developers get
code right and spot problems easier without less work.
PHP supports explicit casting and that should be used for that. Because
if this results in a problem alter in the code you know who to blame:
yourself, and not the automagic behavior of PHP.
my 2c,
-
- Markus
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
- Markus
iD8DBQFHfStA1nS0RcInK9ARAu/dAKDNOMC7M1X8CyrjyCrpzt0/oiLsCgCg2YSY
YAUYre0HTc3dLxsbHTu7+Ik=
=KXMz
-----END PGP SIGNATURE
When the type hint says I want an integer, then only integer should be
accepted; no casting should be done. It may give predictable results
Why people that want Java just don't use Java I wonder? PHP never was a
strict static typed language.
Stanislav Malyshev, Zend Software Architect
stas@zend.com http://www.zend.com/
(408)253-8829 MSN: stas@zend.com
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Hi,
Stanislav Malyshev wrote:
When the type hint says I want an integer, then only integer should be
accepted; no casting should be done. It may give predictable resultsWhy people that want Java just don't use Java I wonder? PHP never was a
strict static typed language.
I can't answer you that one :) I want PHP and not Java. Yet I would like
to have type hinting. Don't see a conflict here.
But with the current situation it feels very awkward. We get class and
array type hints but not the other scalar types?! :-/
-
- Markus
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
- Markus
iD8DBQFHfTDj1nS0RcInK9ARAqL3AJsFeFxW/m+xonir+ukKEtOHU/m/WQCfetSx
wvy6jLFuSclijBClsADGG+8=
=8Lwh
-----END PGP SIGNATURE
But with the current situation it feels very awkward. We get class and
array type hints but not the other scalar types?! :-/
Arrays and objects aren't scalar types, so "other" scalar types are in
fact all on them.
Stanislav Malyshev, Zend Software Architect
stas@zend.com http://www.zend.com/
(408)253-8829 MSN: stas@zend.com
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Hi,
you're right. However to me it's an unimportant factor but maybe not for
the ones who don't like to see type hints (but I don't know what there
arguments are).
thanks,
-
- Markus
Stanislav Malyshev wrote:
But with the current situation it feels very awkward. We get class and
array type hints but not the other scalar types?! :-/Arrays and objects aren't scalar types, so "other" scalar types are in
fact all on them.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
iD8DBQFHfTWe1nS0RcInK9ARAvlJAKCvC5rcQ1je1wbnht3GCacQ7E5HoQCgq/Ix
H9GKXUubMQupLwhx1icukFM=
=jQSH
-----END PGP SIGNATURE
When the type hint says I want an integer, then only integer should be
accepted; no casting should be done. It may give predictable resultsWhy people that want Java just don't use Java I wonder? PHP never was a strict
static typed language.
Broken record perhaps? I am getting a bit tired of this "just use Java
argument", it's perhaps even a bit arrogant. From what I read there is
plenty of people that want type hints for static types - there's a few
patches out there, it doesn't slow down the general case. So why should
we not add it? (And yes, I changed my mind)
regards,
Derick
--
Derick Rethans
http://derickrethans.nl | http://ezcomponents.org | http://xdebug.org
Thank you. As long as a feature is useful, why not add it? Just because
some people don't find it useful, this is not at all a reason to
completely discard the idea, especially when the only argument is "well
PHP doesn't need that so just use another language".
When the type hint says I want an integer, then only integer should be
accepted; no casting should be done. It may give predictable resultsWhy people that want Java just don't use Java I wonder? PHP never was a strict
static typed language.Broken record perhaps? I am getting a bit tired of this "just use Java
argument", it's perhaps even a bit arrogant. From what I read there is
plenty of people that want type hints for static types - there's a few
patches out there, it doesn't slow down the general case. So why should
we not add it? (And yes, I changed my mind)regards,
Derick--
Derick Rethans
http://derickrethans.nl | http://ezcomponents.org | http://xdebug.org
Thank you. As long as a feature is useful, why not add it?
With that statement I don't agree though. It needs to be usefull for a
more general public, and not introduce a huge performance loss for
example.
Derick
PS: Top-posting is considered evil on a mailinglist.
--
Derick Rethans
http://derickrethans.nl | http://ezcomponents.org | http://xdebug.org
With that statement I don't agree though. It needs to be usefull for a
more general public, and not introduce a huge performance loss for
example.
You're right, I didn't mean that like it sounded.
But a feature that maintains backwards compatibility and doesn't have a
major disadvantage for those who choose not to use it is almost always
good. In this case, it will benefit many, and to those who choose not to
use it, they will probably never even notice it's there.
So why not? Because it somehow goes against the ideology of some PHP
users? I'm sorry but I just don't see that as reason enough to throw
away something that could be very useful.
PHP is quickly moving from an easy to learn unprofessional scripting
language into an enterprise-level scalable language. Look at it's newer
enhanced XML capabilities, improved OO engine, and all of the
third-party integration libraries.
Finer-grained control over an application (which type hinting
contributes to) is something that fits the newer and better language
that PHP is becoming.
Just so anyone who wants it has it, I have attached the last version of
my patch.
Note that it still issues a fatal error but this can be changed very
easily, would take me about 2 minutes.
Full specs:
Type hinting patch allows for 8 new type hints, in addition to array and
class type hinting.
- Integers (specified by "int", "integer", or "long")
- Floats (specified by "float", "double", or "real")
- Numbers (matches integers and floats, specified by "num" or "number")
- Strings (specified by "string")
- Booleans (specified by "bool" or "boolean")
- Scalars (matches strings, booleans, and numbers; specified by
"scalar") - Resources (specified by "resource")
- Objects (matches any object, specified by "object")
The patch specifies a constant, PATCH_SCALAR_TYPE_HINTING, just there to
indicate that the patch is installed.
When the type hint says I want an integer, then only integer should be
accepted; no casting should be done. It may give predictable resultsWhy people that want Java just don't use Java I wonder? PHP never was a strict
static typed language.Broken record perhaps? I am getting a bit tired of this "just use Java
argument", it's perhaps even a bit arrogant. From what I read there is
plenty of people that want type hints for static types - there's a few
patches out there, it doesn't slow down the general case. So why should
we not add it? (And yes, I changed my mind)regards,
Derick--
Derick Rethans
http://derickrethans.nl | http://ezcomponents.org | http://xdebug.org
Broken record perhaps? I am getting a bit tired of this "just use Java
argument", it's perhaps even a bit arrogant. From what I read there is
plenty of people that want type hints for static types - there's a few
patches out there, it doesn't slow down the general case. So why should
we not add it? (And yes, I changed my mind)
Same here. I am getting generally tired of the attitude some
politically-correct people seem to have about writing "javaish" code. What
the heck is "javaish" code anyway? Most features that exist in both PHP and
Java can also be found in myriad of other languages and it has so far not
stopped the development team from adding a feature when it is clearly useful
and (most importantly) desired an uncounted number of people.
In fact those who oppose the feature seem only capable of doing so with
hair-splitting rhetorics.
am I the only one to consider E_FATAL (as generated for class typehints)
makes type hinting useless - given that there is no compile stage at which
to catch typehint related mistakes
In principle you are correct. But E_FATAL errors should not happen in a
program that is in production use. If they do, it seems someone was not doing
their job properly.
I am not convinced throwing an exception is the best course of action. If it
was, you might make the argument that all errors should be exceptions, while
traditionally it is the other way around and changing that is beyond the
scope of this thread. I actually like the current "division of labor" when it
comes to error handling.
When PHP detects an error, it should actually be an error. Exceptions are
convenient for the code that you as a PHP developer can throw. They are
especially a blessing when writing library code. That way exceptions work for
the error management, instead of competing with it.
I say use E_WARNING
at this stage. If there is some large redecorating with
PHP's error handling in the future, it can be changed then.
Tomi Kaistila
PHP Developer
Broken record perhaps? I am getting a bit tired of this "just use
Java
argument", it's perhaps even a bit arrogant. From what I read there
is
plenty of people that want type hints for static types - there's a
few
patches out there, it doesn't slow down the general case. So why
should
we not add it? (And yes, I changed my mind)
Same here. I am getting generally tired of the attitude some
politically-correct people seem to have about writing "javaish" code. What
the heck is "javaish" code anyway? Most features that exist in both PHP and
Java can also be found in myriad of other languages and it has so far not
stopped the development team from adding a feature when it is clearly useful
and (most importantly) desired an uncounted number of people.In fact those who oppose the feature seem only capable of doing so with
hair-splitting rhetorics.
Thank you, I've been thinking this stuff for a while now, ever since the
attitude I got when first introducing my patch. I'm glad some people
agree with me.
I don't care what my code looks like, and it's not written in some holy
PHP bible that PHP's one and only purpose of existence is to not be like
Java. If a good feature comes along, it should be evaluated on its pros
and cons, not whether or not it reminds us of Java or any other language
for that matter.
I'm sorry I just have no idea where people come up with this stuff. We
are supposed to be thinking technically and logically as we are all
obviously computer people, not like some Java-hating cult. The issue
seems incredibly simple to me, if there's a good feature and it's worth
adding, add it.
Tomi Kaistila schreef:
Broken record perhaps? I am getting a bit tired of this "just use Java
argument", it's perhaps even a bit arrogant. From what I read there is
plenty of people that want type hints for static types - there's a few
patches out there, it doesn't slow down the general case. So why should
we not add it? (And yes, I changed my mind)
Same here. I am getting generally tired of the attitude some
politically-correct people seem to have about writing "javaish" code. What
the heck is "javaish" code anyway? Most features that exist in both PHP and
Java can also be found in myriad of other languages and it has so far not
stopped the development team from adding a feature when it is clearly useful
and (most importantly) desired an uncounted number of people.In fact those who oppose the feature seem only capable of doing so with
hair-splitting rhetorics.am I the only one to consider E_FATAL (as generated for class typehints)
makes type hinting useless - given that there is no compile stage at which
to catch typehint related mistakes
In principle you are correct. But E_FATAL errors should not happen in a
program that is in production use. If they do, it seems someone was not doing
their job properly.
you are right, they shouldn't - but who can say that every execution permutation has
been tested and hammered shut in their code (let alone someone else 3rd party lib or extension)?
in practice mistakes do occur - and saying someone has not been doing their job
properly is little consilation to the end user, client or manager (who gets a white screen of death) ...
there is no reason not to let the application try to gracefully handle a mistake
like this ... besides I was under the impression that E_FATAL meant the engine was
in an unstable state and was unable to continue execution - I don't see why a typehint
failure would cause an unstable engine state (rather the engine is presuming that
the code is going to create an unstable state if it were to continue ... which is
not very helpful in my book).
I am not convinced throwing an exception is the best course of action. If it
was, you might make the argument that all errors should be exceptions, while
traditionally it is the other way around and changing that is beyond the
scope of this thread. I actually like the current "division of labor" when it
comes to error handling.When PHP detects an error, it should actually be an error. Exceptions are
convenient for the code that you as a PHP developer can throw. They are
especially a blessing when writing library code. That way exceptions work for
the error management, instead of competing with it.I say use
E_WARNING
at this stage. If there is some large redecorating with
PHP's error handling in the future, it can be changed then.
that is a very sane arguement. I'll have to concede :-)
Tomi Kaistila
PHP Developer
patches out there, it doesn't slow down the general case. So why should
we not add it? (And yes, I changed my mind)
My opinion is that the language is not a salad of features that somebody
somewhere found useful. The language should have some directions and
properties that make it easy to understand and handy to work with.
Having both strict and non-strict typing in the same language does not
seem like a good idea to me.
Stanislav Malyshev, Zend Software Architect
stas@zend.com http://www.zend.com/
(408)253-8829 MSN: stas@zend.com
patches out there, it doesn't slow down the general case. So why
should
we not add it? (And yes, I changed my mind)My opinion is that the language is not a salad of features that somebody
somewhere found useful. The language should have some directions and
properties that make it easy to understand and handy to work with.
Having both strict and non-strict typing in the same language does not
seem like a good idea to me.
In a way this is true, but I look at it this way. Some languages are
strictly typed, some are dynamically typed. PHP can have the best of
both worlds by having optional strict typing where desired, as well as
dynamic typing. I have numerous cases where I will have a type hinted
parameter and a non type hinted parameter in the same function. This way
we can have maximum flexibility where useful, but also control where
needed.
Keep in mind, this is not "strict typing" in its correct definition.
Strict typing allows very little room for type conversion. This is
optionally hinting the desired type of a function parameter.
In a way this is true, but I look at it this way. Some languages are
strictly typed, some are dynamically typed. PHP can have the best of
both worlds by having optional strict typing where desired, as well as
I do not believe trying to both eat cake and leave it intact would do us
well. Mixing strict and non-strict code would be a nightmare. Absence of
static type control (necessary for interpreted language) would make
strictly typed code less, and not more stable. Add performance penalty
from type checking and effort would be required from PHP newcomers to
understand two code models instead of one - and you get the worst of
both worlds, not the best.
Strict typing allows very little room for type conversion. This is
optionally hinting the desired type of a function parameter.
That's not what I am hearing here on the list.
Stanislav Malyshev, Zend Software Architect
stas@zend.com http://www.zend.com/
(408)253-8829 MSN: stas@zend.com
On Thursday 03 January 2008 22:51:27 Stanislav Malyshev wrote:
In a way this is true, but I look at it this way. Some languages are
strictly typed, some are dynamically typed. PHP can have the best of
both worlds by having optional strict typing where desired, as well asI do not believe trying to both eat cake and leave it intact would do us
well. Mixing strict and non-strict code would be a nightmare. Absence of
static type control (necessary for interpreted language) would make
strictly typed code less, and not more stable. Add performance penalty
from type checking and effort would be required from PHP newcomers to
understand two code models instead of one - and you get the worst of
both worlds, not the best.
I am not convinced that using type hinting would issue a big performance
drawback because of type checking, for the reason that it is already
happening. We can already hint types for arrays and objects. And whenever I
build function that accepts a scalar value, 90% of the time I have to write a
check to make sure the value is acceptable for the operation that occurs
inside the function. So no matter what, type checking occurs. And so far I
have been able to write lightweight programs when it has suited me, with or
without type checking.
It is especially in such cases where it would be helpful to skip the test
myself, when I know that only the type of variables that I specify can be
passed in. It is less and easier coding for me.
Also, as this has been mentioned several times already, using type hinting is
completely voluntary. Hence, newcomers do not need to bother with it until
they are ready to try it out. If it is required in a job, then it hardly
matters because it is probably one of the simplest things they might demand
from a PHP developer.
That's not what I am hearing here on the list.
I did not get this comment.
Tomi Kaistila
PHP Developer
Stanislav Malyshev schreef:
In a way this is true, but I look at it this way. Some languages are
strictly typed, some are dynamically typed. PHP can have the best of
both worlds by having optional strict typing where desired, as well asI do not believe trying to both eat cake and leave it intact would do us
well. Mixing strict and non-strict code would be a nightmare.
possibly like the nightmare that namespaces will give us in there current
form? I mention it because use of typehinting seems alot more voluntary and
less intrusive (when one encounters it in 3rd party code) than namespaces will
be.
Absence of
static type control (necessary for interpreted language) would make
strictly typed code less, and not more stable. Add performance penalty
from type checking and effort would be required from PHP newcomers to
understand two code models instead of one - and you get the worst of
newcomers? or newbies? the level of entry is being raised in all sorts of
areas whether you like it or not as a by product of making php more
suitable to enterprise level development. it's merely a case of
not being able to please everyone all of the time (or of not having your cake
and eating it)
both worlds, not the best.
why do we then have typehinting for objects? and more recently arrays?
I also seem to remember (forgive me if Im mistaken) that you we're a
proponent for the increases in strictness surrounding various things related
to OO. that feels rather hypocritical at some level.
Strict typing allows very little room for type conversion. This is
optionally hinting the desired type of a function parameter.That's not what I am hearing here on the list.
you implied in another post that php should have some kind of structured direction.
how about a language spec and a formal functionality proprosal/acceptance mechanism?
(preferably one that didn't allow major changes like the inclusion of namespaces into
a minor release)
suitable to enterprise level development. it's merely a case of
not being able to please everyone all of the time (or of not having your cake
PHP is pretty good with pleasing people (including enterprise
developers) as it is now, and will get better in 5.3 and 6 - without any
strict typing. So this argument is not exactly in your favor - burden of
proof is on new feature proposals, and bigger the feature - bigger the
burden.
I also seem to remember (forgive me if Im mistaken) that you we're a
proponent for the increases in strictness surrounding various things related
to OO. that feels rather hypocritical at some level.
Like what? You don't expect me to have same opinion to all proposals, do
you?
you implied in another post that php should have some kind of structured direction.
how about a language spec and a formal functionality proprosal/acceptance mechanism?
Yes, I'd be happy to see it happening. No, I don't have time to create
it. If you do - you know where to find a text editor and docbook build
system ;)
(preferably one that didn't allow major changes like the inclusion of namespaces into
a minor release)
5.3 is a "major" release from a lot of aspects, don't let the numbering
fool you. It is generally accepted that waiting for PHP 6 to make any
change would be too long. Unfortunately, we couldn't find a number
between 5 and 6 for a version number, so we'd have to settle on 5.3.
Stanislav Malyshev, Zend Software Architect
stas@zend.com http://www.zend.com/
(408)253-8829 MSN: stas@zend.com
suitable to enterprise level development. it's merely a case of
not being able to please everyone all of the time (or of not having your cakePHP is pretty good with pleasing people (including enterprise
developers) as it is now, and will get better in 5.3 and 6 - without any
strict typing. So this argument is not exactly in your favor - burden of
proof is on new feature proposals, and bigger the feature - bigger the
burden.
Very true. But we have to make sure to give new features a completely
honest and objective view, strictly on their technical merits.
I also seem to remember (forgive me if Im mistaken) that you we're a
proponent for the increases in strictness surrounding various things related
to OO. that feels rather hypocritical at some level.Like what? You don't expect me to have same opinion to all proposals, do
you?you implied in another post that php should have some kind of structured direction.
how about a language spec and a formal functionality proprosal/acceptance mechanism?Yes, I'd be happy to see it happening. No, I don't have time to create
it. If you do - you know where to find a text editor and docbook build
system ;)(preferably one that didn't allow major changes like the inclusion of namespaces into
a minor release)5.3 is a "major" release from a lot of aspects, don't let the numbering
fool you. It is generally accepted that waiting for PHP 6 to make any
change would be too long. Unfortunately, we couldn't find a number
between 5 and 6 for a version number, so we'd have to settle on 5.3.
Definitely, and namespaces have caused me no problems and I've been
developing in PHP 5.3 (heavily using namespaces) for a couple of months.
I supported and still do support the namespace implementation 100%.
Stanislav Malyshev schreef:
In a way this is true, but I look at it this way. Some languages are
strictly typed, some are dynamically typed. PHP can have the best of
both worlds by having optional strict typing where desired, as well asI do not believe trying to both eat cake and leave it intact would do us
well. Mixing strict and non-strict code would be a nightmare.possibly like the nightmare that namespaces will give us in there current
form? I mention it because use of typehinting seems alot more voluntary and
less intrusive (when one encounters it in 3rd party code) than namespaces will
be.
I don't think any of this stuff will be a nightmare. Maybe for someone
who has no idea what they're doing, but in that case programming in any
language would be a nightmare.
Absence of
static type control (necessary for interpreted language) would make
strictly typed code less, and not more stable. Add performance penalty
from type checking and effort would be required from PHP newcomers to
understand two code models instead of one - and you get the worst ofnewcomers? or newbies? the level of entry is being raised in all sorts of
areas whether you like it or not as a by product of making php more
suitable to enterprise level development. it's merely a case of
not being able to please everyone all of the time (or of not having your cake
and eating it)
I don't think we should hold back the advancement of PHP and slow down
hundreds of experienced developers to accommodate the newcomers. That's
like having a universal speed limit of 25mph to accommodate those who
don't know how to drive.
Learning a language is hard. If a newcomer wants to learn PHP, they will
work at it and do it, but keeping PHP at a lower level than what it
should be just to make it easier on newcomers is ridiculous.
both worlds, not the best.
why do we then have typehinting for objects? and more recently arrays?
Exactly. It makes no sense to me to introduce type checking for objects,
but not for scalar types, when 75% of my variables are integers,
strings, booleans, etc, and only about 25% are arrays and objects.
I also seem to remember (forgive me if Im mistaken) that you we're a
proponent for the increases in strictness surrounding various things related
to OO. that feels rather hypocritical at some level.Strict typing allows very little room for type conversion. This is
optionally hinting the desired type of a function parameter.That's not what I am hearing here on the list.
you implied in another post that php should have some kind of structured direction.
how about a language spec and a formal functionality proprosal/acceptance mechanism?
(preferably one that didn't allow major changes like the inclusion of namespaces into
a minor release)
In a way this is true, but I look at it this way. Some languages are
strictly typed, some are dynamically typed. PHP can have the best of
both worlds by having optional strict typing where desired, as well asI do not believe trying to both eat cake and leave it intact would do us
well. Mixing strict and non-strict code would be a nightmare. Absence of
static type control (necessary for interpreted language) would make
strictly typed code less, and not more stable. Add performance penalty
from type checking and effort would be required from PHP newcomers to
understand two code models instead of one - and you get the worst of
both worlds, not the best.
But this is not all-out strict typing. This is simply checking types on
execution of a function. I wrote the patch to follow the exact same
model as array type hinting. The only difference is that instead of
checking if the value is an array, it checks if it is an integer,
string, bool, etc.
There aren't two code models here at all. You can have a function
parameter, or you can have a type-enforced function parameter. PHP
newcomers can just not use type hinting if they want but I don't think
this is a valid argument.
As for performance, you may have a point. However it is probably no
worse than array type hinting, and it's definitely faster than class
type hinting, which were both deemed useful for PHP 5.1.
Strict typing allows very little room for type conversion. This is
optionally hinting the desired type of a function parameter.That's not what I am hearing here on the list.
Sorry I don't understand, what is it you are not hearing on the list?
There aren't two code models here at all. You can have a function
parameter, or you can have a type-enforced function parameter. PHP
So now to use such function you'd have to check your variables for
typing - otherwise your application blows up. And the type-checking
should be total - otherwise you miss some call to some function or some
code path bringing wrong value and your application blows up at runtime
- since static type checking is not available. Meaning, unless all of
your code is type-enforced, you'd have to write a wrapper around each
type-enforced function manually checking that wrong value didn't get in.
But you can do the same checks now, so what is the added value?
--
Stanislav Malyshev, Zend Software Architect
stas@zend.com http://www.zend.com/
(408)253-8829 MSN: stas@zend.com
I don't get it. We already have type hinting, just not for scalars.
The discussion seems to be about whether or not we should have it all.
But, the truth is, we have it. We half way have it. I fought for it to
be all or nothing back then and I still think it is half done that we
don't have scalar type hinting but we have array/object/class type
hinting. Talk about inconsistency.
So, can we switch the mundane conversation from "should we have type
hinting" to "why don't we have scalar type hinting if we have non-scalar
type hinting?" That is the real conversation. Type hinting is here.
It is not going anywhere. Let's finish the job.
--
Brian Moon
Senior Developer
When you care enough to spend the very least.
http://dealnews.com/
So, can we switch the mundane conversation from "should we have type
hinting" to "why don't we have scalar type hinting if we have non-scalar
type hinting?" That is the real conversation. Type hinting is here.
It is not going anywhere. Let's finish the job.
Amen.
Tomi Kaistila
PHP Developer
I don't get it. We already have type hinting, just not for scalars. The
Type hinting for scalars is different because it disables one of the
features of PHP language - conversion of scalar types into each other.
So, can we switch the mundane conversation from "should we have type
hinting" to "why don't we have scalar type hinting if we have non-scalar
type hinting?" That is the real conversation. Type hinting is here. It
These are different kinds of typing, with different consequences. You do
not convert DateTime into PEAR_Soap_Client routinely, nor such
conversion can make any sense at all, so distinguishing between them is
not the same as making hard distinction between integer and float and
string, and disabling conversion between them.
Stanislav Malyshev, Zend Software Architect
stas@zend.com http://www.zend.com/
(408)253-8829 MSN: stas@zend.com
It doesn't disable type conversion unless you specifically tell it to.
Plus the fact that if it issues an E_WARNING, your application will not
necessarily stop execution.
I don't get it. We already have type hinting, just not for scalars. The
Type hinting for scalars is different because it disables one of the
features of PHP language - conversion of scalar types into each other.So, can we switch the mundane conversation from "should we have type
hinting" to "why don't we have scalar type hinting if we have non-scalar
type hinting?" That is the real conversation. Type hinting is here. ItThese are different kinds of typing, with different consequences. You do
not convert DateTime into PEAR_Soap_Client routinely, nor such
conversion can make any sense at all, so distinguishing between them is
not the same as making hard distinction between integer and float and
string, and disabling conversion between them.Stanislav Malyshev, Zend Software Architect
stas@zend.com http://www.zend.com/
(408)253-8829 MSN: stas@zend.com
It doesn't disable type conversion unless you specifically tell it to.
Plus the fact that if it issues an E_WARNING, your application will not
necessarily stop execution.
Stop right here for a moment. Type hints currently throw an
E_RECOVERABLE_ERROR, for obvious reasons. That means, that scalar type
hints should do so as well - without auto conversion. We should not
make this inconsistent or we really fucked it up.
regards,
Derick
--
Derick Rethans
http://derickrethans.nl | http://ezcomponents.org | http://xdebug.org
I don't get it. We already have type hinting, just not for scalars. The
Type hinting for scalars is different because it disables one of the features
of PHP language - conversion of scalar types into each other.
It doesn't disable this. It's still there if you want it. If you want
slightly more control, you can have it too.
regards,
Derick
--
Derick Rethans
http://derickrethans.nl | http://ezcomponents.org | http://xdebug.org
I don't get it. We already have type hinting, just not for scalars.
The discussion seems to be about whether or not we should have it all.
But, the truth is, we have it. We half way have it. I fought for it to
be all or nothing back then and I still think it is half done that we
don't have scalar type hinting but we have array/object/class type
hinting. Talk about inconsistency.So, can we switch the mundane conversation from "should we have type
hinting" to "why don't we have scalar type hinting if we have non-scalar
type hinting?" That is the real conversation. Type hinting is here.
It is not going anywhere. Let's finish the job.
Yes. Atleast add a "scalar" type hint to differentiate from arrays and
objects. A scalar type hint complies with PHP's way of not
differentiating too much between strings, booleans, integers, and
floats.
So now to use such function you'd have to check your variables for
typing - otherwise your application blows up. And the type-checking
should be total - otherwise you miss some call to some function or some
code path bringing wrong value and your application blows up at runtime
- since static type checking is not available.
Unless you are aware of the type of your variables and the data they contain
as you write your applications, you are only inviting trouble anyway. Yes,
PHP handles type juggling on its own but it is not magic wand that makes
caring for the data type of your variables meaningless.
Meaning, unless all of
your code is type-enforced, you'd have to write a wrapper around each
type-enforced function manually checking that wrong value didn't get in.
But you can do the same checks now, so what is the added value?
The one I mentioned not so long ago, or we you not paying attention? Having
the type hinted on the function declaration saves me the time and energy of
checking the type myself, which is something I have to do quite often. It, of
course not, remove the need to validate your arguments completely. But it
would be a big help.
Tomi Kaistila
PHP Developer
Unless you are aware of the type of your variables and the data they contain
as you write your applications, you are only inviting trouble anyway. Yes,
If you think so, you should use statically typed languages, why invite
trouble from the start?
The one I mentioned not so long ago, or we you not paying attention? Having
the type hinted on the function declaration saves me the time and energy of
checking the type myself, which is something I have to do quite often. It, of
Maybe you should write your code differently, so that you shouldn't
check it so often. I just checked in Zend Framework source - which is
not a small body of code - is_integer is used in capacity you want to
use typehints (i.e. "this type or error out") only twice, and in both
cases I'm not sure it's needed. is_float is not used even once, and
is_string I found used only three times in this way, all IMHO
unnecessary since simple (string) would work.
It could make at least minimal sense to make it converting - but making
it type-strict just makes your application blow up in different, less
expected, places without actually saving you any time on coding.
Stanislav Malyshev, Zend Software Architect
stas@zend.com http://www.zend.com/
(408)253-8829 MSN: stas@zend.com
Unless you are aware of the type of your variables and the data they
contain as you write your applications, you are only inviting trouble
anyway. Yes,If you think so, you should use statically typed languages, why invite
trouble from the start?
It is not inviting trouble. In fact it is a good way of avoiding trouble. If
anything it only encourages discipline when writing code and I have yet to
find a programmer who thinks that is a bad thing.
It is one of the things that often annoys me with php functions is that they
do everything they can to avoid failing. While this saves me from looking at
a E_FATAL error, it also subjects my data to changes that are I cannot know
and often differ from function to function.
What this encourages is careless coding standards, where programmers just
assume that PHP will look after them, which is no doubt one of the reasons
behind the popular myth that PHP encourages sloppy programming.
In the days of PHP 4 there was not much of a choice. The best solution was to
either avoid failing at all cost or return a false value to prevent data from
being twisted. Now we have exceptions which makes failing quickly to prevent
mistakes easy and useful.
Here is a small example of how data can be twisted:
function incr($number)
{
static $total = 0;
$total += $number;
return $total;
}
$n1 = 2; $n2 = '1';
incr($n1);
$n3 = incr($n2);
print $n3;
$n2 is a string value of the number one. What happens if, during a more
complex computation an incorrect value is store to $n2, lets say an
alphabetic character?
The value would be interpreted as a zero and the result would be wrong.
This is just a small example, but hidden among 30,000 lines of code, looking
for it would definitely ruin my day.
Maybe you should write your code differently, so that you shouldn't
check it so often.
I could try that. But then again I have found that the way that I write my
code has produced quality work for which I have been rewarded with satisfied
users. So obviously I am doing something right.
One thing that I will not do is be arrogant enough to assume that because
someone does something differently from me it has to be wrong. If anything is
sure, is that people will use your code for things that you never considered
(note that this is not a bad thing).
I just checked in Zend Framework source - which is
not a small body of code - is_integer is used in capacity you want to
use typehints (i.e. "this type or error out") only twice, and in both
cases I'm not sure it's needed. is_float is not used even once, and
is_string I found used only three times in this way, all IMHO
unnecessary since simple (string) would work.
Right... So your argument is that because you guys did not do it with the Zend
Framework, none of us should do it?
It could make at least minimal sense to make it converting - but making
it type-strict just makes your application blow up in different, less
expected, places without actually saving you any time on coding.
Actually it makes less sense to have type hinting as sort of a hidden way to
cast values. Whenever execute operations somewhere the developer cannot see
it, you risk blind-sighting him because one minute he expects only X to
happen and suddenly (usually upon discovering a bug, potentially in
production use) he notices that both X and Y happened.
Tomi Kaistila
PHP Developer
$n2 is a string value of the number one. What happens if, during a more
complex computation an incorrect value is store to $n2, lets say an
alphabetic character?
The value would be interpreted as a zero and the result would be wrong.
If you change data, result changes - surprise! If you set $n2 to 42,
result would be wrong too, if by right you mean $n2 being 1 and not 42.
How it has anything to do with static typing?
Stanislav Malyshev, Zend Software Architect
stas@zend.com http://www.zend.com/
(408)253-8829 MSN: stas@zend.com
If you change data, result changes - surprise! If you set $n2 to 42,
result would be wrong too, if by right you mean $n2 being 1 and not 42.
How it has anything to do with static typing?
Ohh gee wiz... Well odds are that you would not want the value to increase by
zero, when the intention was to increase with anything but zero.
Would type hinting help? Well, it would efficiently assure that the value
passed into it was a number and thus the resulted would not get screwed
because we assumed PHP's type juggling would save the day.
Honestly Stanislav, do you even care or read what people write to or is it
your intension only to knit-pick and and be a thicky?
Here is the short version for you. Yes, we all agree that there are existing
workarounds to having scalar values typed hinted. Just like we could have
workarounds for the missing OO features in PHP 4? This does not invalidate
the need for either feature.
Tomi Kaistila
PHP Developer
There aren't two code models here at all. You can have a function
parameter, or you can have a type-enforced function parameter. PHPSo now to use such function you'd have to check your variables for
typing - otherwise your application blows up. And the type-checking
should be total - otherwise you miss some call to some function or some
code path bringing wrong value and your application blows up at runtime
- since static type checking is not available. Meaning, unless all of
your code is type-enforced, you'd have to write a wrapper around each
type-enforced function manually checking that wrong value didn't get in.
But you can do the same checks now, so what is the added value?
type-hinting is asserting.
checking of types is needed only on interface-border points (where
type-hints would be used), but usually can be skipped in a lot points
in "private methods"
It's good to be sure, that input-value is exactly the one which is
supposed to be there
currently the typical code is:
public function someMethod($var1)
{
if (!is_int($var1)) {
throw new UnexpectedValueException();
}
// …
}
it looks too long and (what is even worse) it hides actual
"business-logic" behing checks. I prefer the following code:
public function someMethod(integer $var1)
{
// …
}
--
Alexey Zakhlestin
http://blog.milkfarmsoft.com/
type-hinting is asserting.
checking of types is needed only on interface-border points (where
With strict checking, that means instead of calling:
foo($bar);
you'd have now to do:
if(is_integer($bar)) {
// stupid foo() call would blow up if passed not integer, so I have
to manually handle it
// oh wait, actually I don't have any way to handle it - I need
foo() to be called to proceed
// so let's just
die("can't call foo() because $bar is not integer");
}
foo($bar);
Now how it's easier or makes application more stable? What happens if
foo() is changed and now accepts both integers and strings? What happens
if someone forgets to write a wrapper?
Stanislav Malyshev, Zend Software Architect
stas@zend.com http://www.zend.com/
(408)253-8829 MSN: stas@zend.com
you'd have now to do:
if(is_integer($bar)) {
!is_integer() was meant there of course.
--
Stanislav Malyshev, Zend Software Architect
stas@zend.com http://www.zend.com/
(408)253-8829 MSN: stas@zend.com
W liście Stanislav Malyshev z dnia czwartek 03 stycznia 2008:
type-hinting is asserting.
checking of types is needed only on interface-border points (whereWith strict checking, that means instead of calling:
foo($bar);
you'd have now to do:
if(is_integer($bar)) {
// stupid foo() call would blow up if passed not integer, so I have
to manually handle it
// oh wait, actually I don't have any way to handle it - I need
foo() to be called to proceed
// so let's just
die("can't call foo() because $bar is not integer");
}
foo($bar);
This code is just a good argument FOR type hints. When foo is:
function foo(int $bar) {...}
and you want the code to die if $bar is not integer, then foo($bar) would be
the way to go, without the need for the mess you posted. And if you know that
you have either an int or string representing an integer in $bar, then
foo((integer)$bar) would work just well.
The only problem with the cast is that (integer)'abc' is 0, but it has been so
for a long time and there is no need to change it. is_numeric check solves
the problem.
--
Paweł Stradomski
This code is just a good argument FOR type hints. When foo is:
function foo(int $bar) {...}
and you want the code to die if $bar is not integer, then foo($bar) would be
Why would you want it? I wouldn't want my code to die, I would want it
to work.
you have either an int or string representing an integer in $bar, then
foo((integer)$bar) would work just well.
And then how exactly strict typing helps you? You can not control at
development time if everybody uses (integer) or not, since being
interpreted PHP has no static type checking. So only option to find it
out is to leave a minefield behind hoping that testers and not actual
users would be ones who make it blow up. What if testers fail to find it?
Stanislav Malyshev, Zend Software Architect
stas@zend.com http://www.zend.com/
(408)253-8829 MSN: stas@zend.com
This code is just a good argument FOR type hints. When foo is:
function foo(int $bar) {...}
and you want the code to die if $bar is not integer, then foo($bar) would beWhy would you want it? I wouldn't want my code to die, I would want it
to work.you have either an int or string representing an integer in $bar, then
foo((integer)$bar) would work just well.And then how exactly strict typing helps you? You can not control at
development time if everybody uses (integer) or not, since being
interpreted PHP has no static type checking. So only option to find it
out is to leave a minefield behind hoping that testers and not actual
users would be ones who make it blow up. What if testers fail to find it?
Check google for all fatal errors we introduced about OO strictness
(or getter/setter) and you will see what does that mean to be unclear
about how strict one should be.
I'm not arguing to force strictness but I would love to have it, on a
per class basis for example (</dream>).
--
Pierre
http://blog.thepimp.net | http://www.libgd.org
W liście Stanislav Malyshev z dnia piątek 04 stycznia 2008:
This code is just a good argument FOR type hints. When foo is:
function foo(int $bar) {...}
and you want the code to die if $bar is not integer, then foo($bar) would
beWhy would you want it? I wouldn't want my code to die, I would want it
to work.
You wrote:
With strict checking, that means instead of calling:
foo($bar);
you'd have now to do:
if(is_integer($bar)) {
// stupid foo() call would blow up if passed not integer, so I have
to manually handle it
// oh wait, actually I don't have any way to handle it - I need
foo() to be called to proceed
// so let's just
die("can't call foo() because $bar is not integer");
}
This shows that you explicitly die() if $bar is not integer. But with strict
type hinting this is unneccessary - the script would automatically die if
$bar was not integer. In other words, with strict scalar type hinting the
above would behave just like
foo($bar)
with the only difference being the error message (in your version you've got
your own error message, automatic script failure would raise some built-in
error - which IMO should be E_RECOVERABLE_ERROR, as E_FATAL gives blano
output on production sites where display_error is 0).
Not that I care if scalar type hinting is implemented or not, I just wanted to
point out that your example is just plain wrong.
you have either an int or string representing an integer in $bar, then
foo((integer)$bar) would work just well.And then how exactly strict typing helps you?
Here it doesn't help, but is easy to work around if you are sure you know what
you're doing. This is the same workaround that is needed if someone used
if (! is_integer($bar)) {die()}
inside of the foo method.
--
Paweł Stradomski
If foo() is changed, the type hints should be removed. Most of the time
however, a function will not change its functionality enough to change
the purpose of the arguments.
type-hinting is asserting.
checking of types is needed only on interface-border points (whereWith strict checking, that means instead of calling:
foo($bar);
you'd have now to do:
if(is_integer($bar)) {
// stupid foo() call would blow up if passed not integer, so I have
to manually handle it
// oh wait, actually I don't have any way to handle it - I need
foo() to be called to proceed
// so let's just
die("can't call foo() because $bar is not integer");
}
foo($bar);Now how it's easier or makes application more stable? What happens if
foo() is changed and now accepts both integers and strings? What happens
if someone forgets to write a wrapper?Stanislav Malyshev, Zend Software Architect
stas@zend.com http://www.zend.com/
(408)253-8829 MSN: stas@zend.com
The value is this:
With type hinting:
function a(string $mystring, num $mynum, object $myobject) {
}
Without type hinting:
function a($mystring, $mynum, $myobject) {
if (!is_string($mystring)) {
trigger_error('Parameter 1 of function a() must be a string.',
E_USER_WARNING) ;
}
if (!is_int($mynum) and !is_float($mynum)) {
trigger_error('Parameter 2 of function a() must be a number.',
E_USER_WARNING) ;
}
if (!is_object($myobject) {
trigger_error('Parameter 3 of function a() must be an object.',
E_USER_WARNING) ;
}
}
On Thu, 2008-01-03 at 13:51 -0800, Stanislav Malyshev wrote:
There aren't two code models here at all. You can have a function
parameter, or you can have a type-enforced function parameter. PHPSo now to use such function you'd have to check your variables for
typing - otherwise your application blows up. And the type-checking
should be total - otherwise you miss some call to some function or some
code path bringing wrong value and your application blows up at runtime
- since static type checking is not available. Meaning, unless all of
your code is type-enforced, you'd have to write a wrapper around each
type-enforced function manually checking that wrong value didn't get in.
But you can do the same checks now, so what is the added value?
When the type hint says I want an integer, then only integer should be
accepted; no casting should be done. It may give predictable resultsWhy people that want Java just don't use Java I wonder? PHP never was a strict
static typed language.Broken record perhaps? I am getting a bit tired of this "just use Java
argument", it's perhaps even a bit arrogant.
It is arrogant.
For what I see the strictness is making its way in php simply like OO
made it. First we resist (we all remember how hard it was to introduce
OO API to some extensions "why bloat it with OO?"). In doubts, we only
need to look at all the OO strictness we added in the past versions.
Please not that I use "we" for all php internals developers, as a
group (even if we all have different opinions, we reflect only one in
our releases).
From what I read there is
plenty of people that want type hints for static types - there's a few
patches out there, it doesn't slow down the general case. So why should
we not add it? (And yes, I changed my mind)
Mines was always in favor of strictness for all types. As it is not
necessary to create clean code, it is a really nice addition to bring
PHP to yet another good level. Without talking about reducing the WTF
in some cases, especially when you have to work in teams with
developers not aware of PHP's "weirdness" (please don't tell me to ask
them to RTFM, that's off base).
Cheers,
When the type hint says I want an integer, then only integer should be
accepted; no casting should be done. It may give predictable resultsWhy people that want Java just don't use Java I wonder? PHP never was a
strict static typed language.
You aren't making it a strict static typed language. All that you are doing
is saying that at certain points (function entry) that the values are checked
and converted to certain types. If within the fuction an argument hinted as
integer is used in a string context, the juggling will still happen.
What you are doing to allowing the function author to protect the integrity
of his code without having to resort to a regex/... to check what the arguments are.
--
Alain Williams
Linux Consultant - Mail systems, Web sites, Networking, Programmer, IT Lecturer.
+44 (0) 787 668 0256 http://www.phcomp.co.uk/
Parliament Hill Computers Ltd. Registration Information: http://www.phcomp.co.uk/contact.php
Chairman of UKUUG: http://www.ukuug.org/
#include <std_disclaimer.h
You aren't making it a strict static typed language. All that you are doing
is saying that at certain points (function entry) that the values are checked
and converted to certain types. If within the fuction an argument hinted as
integer is used in a string context, the juggling will still happen.
So it's basically another way to say (string)? Again, that's not what
was said by other people discussing it. But all that just to save one
(string) inside function?
What you are doing to allowing the function author to protect the integrity
of his code without having to resort to a regex/... to check what the arguments are.
If you need regex, you'd still have to use regex. Type juggling can do
only so much.
--
Stanislav Malyshev, Zend Software Architect
stas@zend.com http://www.zend.com/
(408)253-8829 MSN: stas@zend.com
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Stanislav Malyshev wrote:
You aren't making it a strict static typed language. All that you are
doing
is saying that at certain points (function entry) that the values are
checked
and converted to certain types. If within the fuction an argument
hinted as
integer is used in a string context, the juggling will still happen.So it's basically another way to say (string)? Again, that's not what
was said by other people discussing it. But all that just to save one
(string) inside function?
I agree with Stanislav here.
For me "type hinting" has nothing to do with "type conversion". Hinting
a 'string' expects a string, nothing else, not even an object.
If you need this, you need to call the function/method with an explicit
conversion. It's a less 'wtf' factor going on.
-
- Markus
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
- Markus
iD8DBQFHfYjI1nS0RcInK9ARAvEwAKC0WhOjaogX+f84rQArW5s071fSDgCgriWl
1+JM1RK46VVFL7Bz8pjjID0=
=ORed
-----END PGP SIGNATURE
For me "type hinting" has nothing to do with "type conversion". Hinting
a 'string' expects a string, nothing else, not even an object.
Agreed. It seems like a way to compromise between those who support this
feature and those who do not, but in a way that completely defeats the
purpose of type hinting.
Tomi Kaistila
PHP Developer
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1Hello,
Alain Williams wrote:
I think
E_WARNING
would be appropriate. That's what happens when you
omit an argument to a function right?The other thing to note about type hinting is that it could result in faster code.
The value is checked and if needed converted (eg string -> integer) when the
function is called; it then need not be converted again when it is used in
the function. This will presumably be a win if the value is used more than once.I don't think that automagic conversion of types is a good idea.
When the type hint says I want an integer, then only integer should be
accepted; no casting should be done. It may give predictable results
Maybe the type checks should be stricter than normal.
What are we going to want here ?
- integer - possibly convert from:
- string, in which case we just accept digits with an option + or -
The important thing is that the entire string must convert - float - the standard truncate towards zero
Nothing else
- string, in which case we just accept digits with an option + or -
- float - possibly convert from:
- integers - standard convertion
- string - accept a float in the standard formats
The important thing is that the entire string must convert
- boolean. At first glance this is easier as the convert to boolean for
lots of types is well defined. In most cases the programmer would
not be interested in a type check, so if he specifies it then we
should be very restrictive. - string - possibly convert from:
- integers & float - standard convertion
- boolean - standard convertion
- objects - only if they have a __toString() method
--
Alain Williams
Linux Consultant - Mail systems, Web sites, Networking, Programmer, IT Lecturer.
+44 (0) 787 668 0256 http://www.phcomp.co.uk/
Parliament Hill Computers Ltd. Registration Information: http://www.phcomp.co.uk/contact.php
Chairman of UKUUG: http://www.ukuug.org/
#include <std_disclaimer.h
Yes it seems PHP will omit E_WARNING
if you omit an argument.
(Had to actually check.)
Not a bad choice for an error and probably also easier to manage when you are
dealing with complicated error handling in large applications.
I would suggest E_WARNING
or E_FATAL, but not E_NOTICE
or E_STRICT, for the
simple fact that they are ignore by a major portion of PHP developers and to
use them would prompt a high risk of people writing bad code.
Also, now that you mentioned abstract classes, type hinting might also be
useful with interfaces.
Tomi Kaistila
PHP Developer
On Thursday 03 January 2008 19:21:21 you wrote:
I think
E_WARNING
would be appropriate. That's what happens when you
omit an argument to a function right?And about function return type hinting, I don't think it would be as
useful as parameter type hinting, but it would be useful. Mostly for
stuff like declaring an abstract function in a parent class that must
return a certain type.class a {
abstract public function getNumber() returns int ;
}
class b extends a {
public function getNumber() {
returnrand()
* 3463 ;
}
}
class c extends a {
public function getNumber() {
return 'I'm going to mess everything up by returning a string.' ; //
Would cause error with type hinting.
}
}Hello everyone
I figured I would bring my opinion in to support of Sam's request for a
more complete type hinting feature. Namely I am interested in the support
for hinting scalar types on function and method arguments and I am sure
it is safe for me to say that I speak for a lot of people. Most people
that I know find the current type hinting, while useful, ridiculous
because it looks like the job was left unfinished for whatever abstract
reason.In my opinion type hinting should definitely be allowed for scalar
values. As for return types, I am not so sure. So far I have found no use
for such a feature in my own code, so I won't comment on it. If it is
added, I welcome it for those who find it useful but if it is not added I
will not loose sleep over it.I was thinking at something along the lines of objects also for
instance: $i = new Integer(33);After my own experiments with the subject I concur that while it can be
made to work, it is not only a bad idea (for the reasons mentioned
earlier) it is also redundant and just unnecessary. There is a lot better
way to accomplish the same and that by allowing scalar values to be
hinted. It is simpler, cleaner, and easier to implement.What if type hinting just generated an E_NOTICE. Nothing more for the
time being.Changing it to
E_NOTICE
orE_STRICT
defeats the purpose somewhat since
if I write a piece of code that hints that the argument for a-whatever
method needs to be an integer it seems useless if the user of my library
can avoid the issue just by supressing lesser errors and those who do not
need to write extensive error handling code to respond to this sort of
error (if they indeed deem it necessary to do so).While hinting is, and should remain, optional, when it is used it should
be enforced. After all the user of my library has the option to dump it
and go for another library that does not force types. That is the beauty
of having options.Tomi Kaistila
PHP Developer
am I the only one to consider E_FATAL (as generated for class typehints) makes
type hinting useless - given that there is no compile stage at which to catch
typehint related mistakes. which means there is no way to trap the issue and
offer some useful/user-friendly feedback to the user (in practice it usually
means the white page of death for a site visitor)
E_WARNING
also is not much better given that one would assume the function/method
that was passed incorrect variables (according to type) wouldn't run.
would an exception not be the most suitable thing to generate on a typehint error?
to me anything else makes typehinting in production environments pretty much unusable
unless one write code like so:
function foo(Foo $f) { /* do foo stuff */ }
$f = getFoo();
if ($f instanceof Foo) {
foo($f);
}
now the issue with such code is not that it is alot more verbose than might
strictly be needed but that the instanceof statement makes the typehint rather
superfluous.
Tomi Kaistila schreef:
Yes it seems PHP will omit
E_WARNING
if you omit an argument.(Had to actually check.)
Not a bad choice for an error and probably also easier to manage when you are
dealing with complicated error handling in large applications.I would suggest
E_WARNING
or E_FATAL, but notE_NOTICE
or E_STRICT, for the
simple fact that they are ignore by a major portion of PHP developers and to
use them would prompt a high risk of people writing bad code.Also, now that you mentioned abstract classes, type hinting might also be
useful with interfaces.Tomi Kaistila
PHP DeveloperOn Thursday 03 January 2008 19:21:21 you wrote:
I think
E_WARNING
would be appropriate. That's what happens when you
omit an argument to a function right?And about function return type hinting, I don't think it would be as
useful as parameter type hinting, but it would be useful. Mostly for
stuff like declaring an abstract function in a parent class that must
return a certain type.class a {
abstract public function getNumber() returns int ;
}
class b extends a {
public function getNumber() {
returnrand()
* 3463 ;
}
}
class c extends a {
public function getNumber() {
return 'I'm going to mess everything up by returning a string.' ; //
Would cause error with type hinting.
}
}Hello everyone
I figured I would bring my opinion in to support of Sam's request for a
more complete type hinting feature. Namely I am interested in the support
for hinting scalar types on function and method arguments and I am sure
it is safe for me to say that I speak for a lot of people. Most people
that I know find the current type hinting, while useful, ridiculous
because it looks like the job was left unfinished for whatever abstract
reason.In my opinion type hinting should definitely be allowed for scalar
values. As for return types, I am not so sure. So far I have found no use
for such a feature in my own code, so I won't comment on it. If it is
added, I welcome it for those who find it useful but if it is not added I
will not loose sleep over it.I was thinking at something along the lines of objects also for
instance: $i = new Integer(33);
After my own experiments with the subject I concur that while it can be
made to work, it is not only a bad idea (for the reasons mentioned
earlier) it is also redundant and just unnecessary. There is a lot better
way to accomplish the same and that by allowing scalar values to be
hinted. It is simpler, cleaner, and easier to implement.What if type hinting just generated an E_NOTICE. Nothing more for the
time being.
Changing it toE_NOTICE
orE_STRICT
defeats the purpose somewhat since
if I write a piece of code that hints that the argument for a-whatever
method needs to be an integer it seems useless if the user of my library
can avoid the issue just by supressing lesser errors and those who do not
need to write extensive error handling code to respond to this sort of
error (if they indeed deem it necessary to do so).While hinting is, and should remain, optional, when it is used it should
be enforced. After all the user of my library has the option to dump it
and go for another library that does not force types. That is the beauty
of having options.Tomi Kaistila
PHP Developer
I think we should stick with E_WARNING
for now. If you want to use
exceptions you can throw one in a custom error handler. I can change
about 4 lines of code in my patch to have it emit an E_WARNING
easily.
am I the only one to consider E_FATAL (as generated for class typehints) makes
type hinting useless - given that there is no compile stage at which to catch
typehint related mistakes. which means there is no way to trap the issue and
offer some useful/user-friendly feedback to the user (in practice it usually
means the white page of death for a site visitor)
E_WARNING
also is not much better given that one would assume the function/method
that was passed incorrect variables (according to type) wouldn't run.would an exception not be the most suitable thing to generate on a typehint error?
to me anything else makes typehinting in production environments pretty much unusable
unless one write code like so:function foo(Foo $f) { /* do foo stuff */ }
$f = getFoo();
if ($f instanceof Foo) {
foo($f);
}now the issue with such code is not that it is alot more verbose than might
strictly be needed but that the instanceof statement makes the typehint rather
superfluous.Tomi Kaistila schreef:
Yes it seems PHP will omit
E_WARNING
if you omit an argument.(Had to actually check.)
Not a bad choice for an error and probably also easier to manage when you are
dealing with complicated error handling in large applications.I would suggest
E_WARNING
or E_FATAL, but notE_NOTICE
or E_STRICT, for the
simple fact that they are ignore by a major portion of PHP developers and to
use them would prompt a high risk of people writing bad code.Also, now that you mentioned abstract classes, type hinting might also be
useful with interfaces.Tomi Kaistila
PHP DeveloperOn Thursday 03 January 2008 19:21:21 you wrote:
I think
E_WARNING
would be appropriate. That's what happens when you
omit an argument to a function right?And about function return type hinting, I don't think it would be as
useful as parameter type hinting, but it would be useful. Mostly for
stuff like declaring an abstract function in a parent class that must
return a certain type.class a {
abstract public function getNumber() returns int ;
}
class b extends a {
public function getNumber() {
returnrand()
* 3463 ;
}
}
class c extends a {
public function getNumber() {
return 'I'm going to mess everything up by returning a string.' ; //
Would cause error with type hinting.
}
}Hello everyone
I figured I would bring my opinion in to support of Sam's request for a
more complete type hinting feature. Namely I am interested in the support
for hinting scalar types on function and method arguments and I am sure
it is safe for me to say that I speak for a lot of people. Most people
that I know find the current type hinting, while useful, ridiculous
because it looks like the job was left unfinished for whatever abstract
reason.In my opinion type hinting should definitely be allowed for scalar
values. As for return types, I am not so sure. So far I have found no use
for such a feature in my own code, so I won't comment on it. If it is
added, I welcome it for those who find it useful but if it is not added I
will not loose sleep over it.I was thinking at something along the lines of objects also for
instance: $i = new Integer(33);
After my own experiments with the subject I concur that while it can be
made to work, it is not only a bad idea (for the reasons mentioned
earlier) it is also redundant and just unnecessary. There is a lot better
way to accomplish the same and that by allowing scalar values to be
hinted. It is simpler, cleaner, and easier to implement.What if type hinting just generated an E_NOTICE. Nothing more for the
time being.
Changing it toE_NOTICE
orE_STRICT
defeats the purpose somewhat since
if I write a piece of code that hints that the argument for a-whatever
method needs to be an integer it seems useless if the user of my library
can avoid the issue just by supressing lesser errors and those who do not
need to write extensive error handling code to respond to this sort of
error (if they indeed deem it necessary to do so).While hinting is, and should remain, optional, when it is used it should
be enforced. After all the user of my library has the option to dump it
and go for another library that does not force types. That is the beauty
of having options.Tomi Kaistila
PHP Developer
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Hello Sam, Jochem,
Sam Barrow wrote:
I think we should stick with
E_WARNING
for now. If you want to use
exceptions you can throw one in a custom error handler. I can change
about 4 lines of code in my patch to have it emit anE_WARNING
easily.am I the only one to consider E_FATAL (as generated for class typehints) makes
type hinting useless -
I'm currently going down exactly this road. Simplified:
set_error_handler('throwit');
function throwit($code, $msg, $file, $line) {
if (0 == error_reporting()
) {
return;
}
throw new Exception("$msg in $file on $line", $code);
}
Of course, Jochem, you're right. Exceptions would be nice.
WrongArgumentException, InvalidWhateverException, ...
-
- Markus
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
- Markus
iD8DBQFHfVWT1nS0RcInK9ARApW9AJ9OF+IM/bKWP5D21TDd6r8tTFhNEACgl9Ok
rS+MNouW6p/4C4iKOW3/AlU=
=887G
-----END PGP SIGNATURE
Of course, Jochem, you're right. Exceptions would be nice.
WrongArgumentException, InvalidWhateverException, ...
That would be very nice. The full blown suite of exception error
handlers could be a future project, depending on code contributors and
community support.
As for my patch, I think we can all agree on E_WARNING.
Of course, Jochem, you're right. Exceptions would be nice.
WrongArgumentException, InvalidWhateverException, ...That would be very nice. The full blown suite of exception error
handlers could be a future project, depending on code contributors and
community support.As for my patch, I think we can all agree on E_WARNING.
Definitely not. Type hints now throw E_RECOVERABLE_ERROR, and that
should be the same for any other typehinting system that we add.
Derick
Definitely not. Type hints now throw E_RECOVERABLE_ERROR, and that
should be the same for any other typehinting system that we add.
Then we don't add any, because without static type control it's just a
ticking timebomb waiting to blow up your production code (and having
application display "An internal error happened, please contact
developers" is blowing up no less than having blank screen).
Stanislav Malyshev, Zend Software Architect
stas@zend.com http://www.zend.com/
(408)253-8829 MSN: stas@zend.com
I think E_WARNING
would be appropriate. That's what happens when you
omit an argument to a function right?
And about function return type hinting, I don't think it would be as
useful as parameter type hinting, but it would be useful. Mostly for
stuff like declaring an abstract function in a parent class that must
return a certain type.
class a {
abstract public function getNumber() returns int ;
}
class b extends a {
public function getNumber() {
return rand()
* 3463 ;
}
}
class c extends a {
public function getNumber() {
return 'I'm going to mess everything up by returning a
string.' ; //
Would cause error with type hinting.
}
}
Hello everyone
I figured I would bring my opinion in to support of Sam's request for
a more
complete type hinting feature. Namely I am interested in the support
for
hinting scalar types on function and method arguments and I am sure it
is
safe for me to say that I speak for a lot of people. Most people that
I know
find the current type hinting, while useful, ridiculous because it
looks like
the job was left unfinished for whatever abstract reason.In my opinion type hinting should definitely be allowed for scalar
values. As
for return types, I am not so sure. So far I have found no use for
such a
feature in my own code, so I won't comment on it. If it is added, I
welcome
it for those who find it useful but if it is not added I will not
loose sleep
over it.I was thinking at something along the lines of objects also for
instance:
$i = new Integer(33);
After my own experiments with the subject I concur that while it can
be made
to work, it is not only a bad idea (for the reasons mentioned earlier)
it is
also redundant and just unnecessary. There is a lot better way to
accomplish
the same and that by allowing scalar values to be hinted. It is
simpler,
cleaner, and easier to implement.What if type hinting just generated an E_NOTICE. Nothing more for
the
time being.
Changing it toE_NOTICE
orE_STRICT
defeats the purpose somewhat
since if I
write a piece of code that hints that the argument for a-whatever
method
needs to be an integer it seems useless if the user of my library can
avoid
the issue just by supressing lesser errors and those who do not need
to write
extensive error handling code to respond to this sort of error (if
they
indeed deem it necessary to do so).While hinting is, and should remain, optional, when it is used it
should be
enforced. After all the user of my library has the option to dump it
and go
for another library that does not force types. That is the beauty of
having
options.Tomi Kaistila
PHP Developer
Hello everyone
I figured I would bring my opinion in to support of Sam's request for a more
complete type hinting feature. Namely I am interested in the support for
hinting scalar types on function and method arguments and I am sure it is
safe for me to say that I speak for a lot of people. Most people that I know
find the current type hinting, while useful, ridiculous because it looks like
the job was left unfinished for whatever abstract reason.In my opinion type hinting should definitely be allowed for scalar values. As
for return types, I am not so sure. So far I have found no use for such a
feature in my own code, so I won't comment on it. If it is added, I welcome
it for those who find it useful but if it is not added I will not loose sleep
over it.I was thinking at something along the lines of objects also for instance:
$i = new Integer(33);
After my own experiments with the subject I concur that while it can be made
to work, it is not only a bad idea (for the reasons mentioned earlier) it is
also redundant and just unnecessary. There is a lot better way to accomplish
the same and that by allowing scalar values to be hinted. It is simpler,
cleaner, and easier to implement.What if type hinting just generated an E_NOTICE. Nothing more for the
time being.
Changing it toE_NOTICE
orE_STRICT
defeats the purpose somewhat since if I
write a piece of code that hints that the argument for a-whatever method
needs to be an integer it seems useless if the user of my library can avoid
the issue just by supressing lesser errors and those who do not need to write
extensive error handling code to respond to this sort of error (if they
indeed deem it necessary to do so).While hinting is, and should remain, optional, when it is used it should be
enforced. After all the user of my library has the option to dump it and go
for another library that does not force types. That is the beauty of having
options.Tomi Kaistila
PHP Developer
We've discussed scalar type hinting many times in the past and decided
against it.
It really doesn't fit in very well with PHP's loosely typed nature which
is one of the main reasons it has been so easy to use. The only reason
why it works with classes and arrays is because those are unambiguous
constructs.
Even this thread shows that there's no alignment between people on what
it should actually do. This is due to the lose nature of PHP. For
example, HTTP vars come in as strings. So if you have
http://localhost/?n=1 in your URL there are some in this thread which
would expect it to be successfully juggled by an "int" type hint even
though it actually comes in as a string; and some who want it to be
ultra-strict and if the PHP type isn't an integer it should emit an
error. What happens when you pass an object with a __toString() method
to a function expecting a "string"? I think it should work and convert.
I am sure some here disagree. This is exactly the problem!
Saying that it won't confuse newbies is also wrong. PHP is so popular
because it's so easy for people to pick up. Part of this also includes
ability to look at other people's code, understand it, copy-on-write
(e.g. a Wordpress plug-in). The more features in PHP the harder it'll be
for developers to deal with. Yes, we bit the bullet for namespaces
because there were strong reasons in favor of it, we as a community had
it on our wishlist for a long time and a lot of work went into it. And
even when we did it we had to find a way to make it work for PHP. In
this case, with the lose typing of PHP creating a consistent model which
will be apparent to people without many questions is a big issue.
Also, for those who say that when this feature is not used it doesn't
have a performance impact that is also a problematic statement. If it
exists people will use it. Most people using PHP don't understand (and
shouldn't have to understand) how the language is implemented. It may
very well be that even if we could agree on the semantics we'll see all
sorts of developers "decorate" their code with type hints (because they
think it'll help readability) who may down the road find themselves in a
surprise when it comes to performance.
Last, just because something can be implemented and a patch exists
doesn't mean it should be in PHP. There are lots of things I can
implement for PHP which shouldn't be in PHP. Also don't under estimate
what happens when you get bloatware and over time how much harder it
becomes to maintain and also how it does eventually end up slowing
things down (over time there are more branches, more code a.k.a more
cache misses, etc...)
We have what I think is a very exciting PHP version coming up with 5.3.
The featureset has been agreed upon. It'd be great if all the
contributing energy in this thread could be channeled towards playing
around with 5.3 esp. the new i18n extension, namespaces, help with
migration/incompatibility notes and garbage collector benchmarks. We are
now at the stage where we really need the code base to mature with
feedback from users. The more you can invest in that the better.
Ouch, sorry for the long email! :'(
Andi
P.S.- The runtime piece of this patch also looks wrong. I didn't dive
deep but those if() statements don't look too friendly.
It really doesn't fit in very well with PHP's loosely typed nature which
is one of the main reasons it has been so easy to use.
I think this is one of the cornerstones that two sides disagree the most on.
People are afraid that PHP would turn into a strong-typed language by
allowing type hinting for scalar values. I do not think this would be the
case. You would still have all the freedom that you have no with PHP, since
PHP's type juggling is still in effect.
The only thing that would change is that the validation of the data that is
passed into functions would be somewhat automated, but since the validation
occurs with or without type hinting the result is still the same. If data is
incorrent, an error occurs whether it is an exception, a false return type,
or a triggered error. The behaviour of the program does not change with type
hinting.
Even this thread shows that there's no alignment between people on what
it should actually do.
This is true, but contributed to it is that so far we have spent more time and
energy on the silly debate whether or not type hinting is a useful feature.
There is enough technical proof to suggest that it is in fact a useful
feature.
What I would like to see is actual discussion on how it should behave and see
if a concensus can be derived.
Saying that it won't confuse newbies is also wrong. PHP is so popular
because it's so easy for people to pick up. Part of this also includes
ability to look at other people's code, understand it, copy-on-write
(e.g. a Wordpress plug-in).
This is an argument that I honestly do not understand. Type hinting is already
here. It exists in PHP. How can the concept be abandoned on the bases that it
would confuse new users, if the concept has already been approved and
implemented?
Tomi Kaistila
PHP Developer
It really doesn't fit in very well with PHP's loosely typed nature which
is one of the main reasons it has been so easy to use.
I think this is one of the cornerstones that two sides disagree the most on.
People are afraid that PHP would turn into a strong-typed language by
allowing type hinting for scalar values. I do not think this would be the
case. You would still have all the freedom that you have no with PHP, since
PHP's type juggling is still in effect.The only thing that would change is that the validation of the data that is
passed into functions would be somewhat automated, but since the validation
occurs with or without type hinting the result is still the same. If data is
incorrent, an error occurs whether it is an exception, a false return type,
or a triggered error. The behaviour of the program does not change with type
hinting.
Exactly. This is not strict typing. It's type hinting (key word,
hinting).
Even this thread shows that there's no alignment between people on what
it should actually do.
This is true, but contributed to it is that so far we have spent more time and
energy on the silly debate whether or not type hinting is a useful feature.
There is enough technical proof to suggest that it is in fact a useful
feature.What I would like to see is actual discussion on how it should behave and see
if a concensus can be derived.Saying that it won't confuse newbies is also wrong. PHP is so popular
because it's so easy for people to pick up. Part of this also includes
ability to look at other people's code, understand it, copy-on-write
(e.g. a Wordpress plug-in).
This is an argument that I honestly do not understand. Type hinting is already
here. It exists in PHP. How can the concept be abandoned on the bases that it
would confuse new users, if the concept has already been approved and
implemented?
Exactly, array and class type hints are actually more confusing than
scalar type hints. What's more confusing to a beginner, having to pass
an instance of a certain object to a function, or having to pass a
number or string?
Tomi Kaistila
PHP Developer
We've discussed scalar type hinting many times in the past and decided
against it.
It really doesn't fit in very well with PHP's loosely typed nature which
is one of the main reasons it has been so easy to use. The only reason
why it works with classes and arrays is because those are unambiguous
constructs.
It doesn't fit. This is not a technical argument, this is your opinion
on the way PHP should be. That's the problem here.
Even this thread shows that there's no alignment between people on what
it should actually do. This is due to the lose nature of PHP. For
example, HTTP vars come in as strings. So if you have
http://localhost/?n=1 in your URL there are some in this thread which
would expect it to be successfully juggled by an "int" type hint even
though it actually comes in as a string; and some who want it to be
ultra-strict and if the PHP type isn't an integer it should emit an
error. What happens when you pass an object with a __toString() method
to a function expecting a "string"? I think it should work and convert.
I am sure some here disagree. This is exactly the problem!
We keep saying that scalar type hinting is not intended to be used for
input. Even so, if you really wanted to you could run your input through
a very simple function and convert each variable into the appropriate
type.
function typeConvert($variable) {
if (is_array($variable)) {
foreach ($variable as $k => $v) {
$variable[$k] = typeConvert($v) ;
}
}
else if (is_object($variable)) {
foreach ($variable as $k => $v) {
$variable -> $k = typeConvert($v) ;
}
}
else {
if (is_numeric($variable) and substr($variable, 0, 1) !== '0') {
if (substr_count($variable, '.')) {
$variable = (float) $variable ;
}
else {
$variable = (int) $variable ;
}
}
}
return $variable ;
}
Saying that it won't confuse newbies is also wrong. PHP is so popular
because it's so easy for people to pick up. Part of this also includes
ability to look at other people's code, understand it, copy-on-write
(e.g. a Wordpress plug-in). The more features in PHP the harder it'll be
for developers to deal with. Yes, we bit the bullet for namespaces
because there were strong reasons in favor of it, we as a community had
it on our wishlist for a long time and a lot of work went into it. And
even when we did it we had to find a way to make it work for PHP. In
this case, with the lose typing of PHP creating a consistent model which
will be apparent to people without many questions is a big issue.
I've said this numerous times too. How hard is it to understand? If
someone can't understand this simple concept they are sure as hell not
going to understand object oriented or anything.
Plus the fact that this is actually easier to understand than
array/object type hints.
Also, for those who say that when this feature is not used it doesn't
have a performance impact that is also a problematic statement. If it
exists people will use it. Most people using PHP don't understand (and
shouldn't have to understand) how the language is implemented. It may
very well be that even if we could agree on the semantics we'll see all
sorts of developers "decorate" their code with type hints (because they
think it'll help readability) who may down the road find themselves in a
surprise when it comes to performance.
Well that's their decision. I could use functions and not use a single
class in my applications, and they would be faster. I sacrifice a little
performance in order to make my code better.
Last, just because something can be implemented and a patch exists
doesn't mean it should be in PHP. There are lots of things I can
implement for PHP which shouldn't be in PHP. Also don't under estimate
what happens when you get bloatware and over time how much harder it
becomes to maintain and also how it does eventually end up slowing
things down (over time there are more branches, more code a.k.a more
cache misses, etc...)
You are right here. But define bloatware. Type hinting is already in
PHP. All I want to do is add a couple of types to this already
implemented feature. My patch is pretty small.
We have what I think is a very exciting PHP version coming up with 5.3.
The featureset has been agreed upon. It'd be great if all the
contributing energy in this thread could be channeled towards playing
around with 5.3 esp. the new i18n extension, namespaces, help with
migration/incompatibility notes and garbage collector benchmarks. We are
now at the stage where we really need the code base to mature with
feedback from users. The more you can invest in that the better.
Good point, namespaces need to be tested and brought to a production
state, and i18n looks pretty interesting.
Ouch, sorry for the long email! :'(
Andi
P.S.- The runtime piece of this patch also looks wrong. I didn't dive
deep but those if() statements don't look too friendly.
It's worked for me fine for a couple of months. The if statements in
zend_compile.c are pretty confusing (as far as the brace indentation
goes).
See below:
-----Original Message-----
From: Sam Barrow [mailto:sam@sambarrow.com]
Sent: Friday, January 04, 2008 5:47 AM
To: Andi Gutmans
Cc: internals@lists.php.net
Subject: RE: [PHP-DEV] RE: Optional scalar type hintingWe've discussed scalar type hinting many times in the past and
decided
against it.
It really doesn't fit in very well with PHP's loosely typed nature
which
is one of the main reasons it has been so easy to use. The only
reason
why it works with classes and arrays is because those are
unambiguous
constructs.It doesn't fit. This is not a technical argument, this is your opinion
on the way PHP should be. That's the problem here.
Are you saying that languages are designed only based on technicalities?
So why are languages so different? Each language has a spirit and an
emphasis and this is definitely something to think about. It also
relates to what the applicable market and users are.
-snip-
So why don't we first see if people can agree on what this does:
I think all the following ones should work:
function iwantint(int $n) {
iwantint("1"); // "1" + 1 works great.
function iwantstring(string $str) {}
iwantstring($toStringObject); // Works in strcmp()
function iwantfloat(float $f) {}
iwantfloat(2);
function iwantbool(bool $b) {}
iwantbool(1);
function iwantnull(null $n) {}
iwantnull(null); //Does this exist?
And for consistency of the language we would be using PHP's type
juggling system to see if there's a match or not and you'd get whatever
results you get today.
Btw I think there's absolutely no argument that the error should be
exactly the same as with array/class hints. No way are we going to have
two different errors for the same kind of functionality. That's
ludicrous.
More below:...
Saying that it won't confuse newbies is also wrong. PHP is so
popular
because it's so easy for people to pick up. Part of this also
includes
ability to look at other people's code, understand it, copy-on-write
(e.g. a Wordpress plug-in). The more features in PHP the harder
it'll
be
for developers to deal with. Yes, we bit the bullet for namespaces
because there were strong reasons in favor of it, we as a community
had
it on our wishlist for a long time and a lot of work went into it.
And
even when we did it we had to find a way to make it work for PHP. In
this case, with the lose typing of PHP creating a consistent model
which
will be apparent to people without many questions is a big issue.I've said this numerous times too. How hard is it to understand? If
someone can't understand this simple concept they are sure as hell not
going to understand object oriented or anything.
There are actually a lot of PHP developers who don't do OO oriented.
This is why we left it as a choice to developers (it's been around btw
since 1997 at least the basic OO functionality).
Anyway what's confusing here are the semantics not the feature itself.
For example, I expect something different than you do.
Plus the fact that this is actually easier to understand than
array/object type hints.
I don't think so because there's no potential type juggling in those
(which is why we did it) but it's a pointless argument because it
doesn't add much value to the discussion.
Also, for those who say that when this feature is not used it
doesn't
have a performance impact that is also a problematic statement. If
it
exists people will use it. Most people using PHP don't understand
(and
shouldn't have to understand) how the language is implemented. It
may
very well be that even if we could agree on the semantics we'll see
all
sorts of developers "decorate" their code with type hints (because
they
think it'll help readability) who may down the road find themselves
in a
surprise when it comes to performance.Well that's their decision. I could use functions and not use a single
class in my applications, and they would be faster. I sacrifice a
little
performance in order to make my code better.Last, just because something can be implemented and a patch exists
doesn't mean it should be in PHP. There are lots of things I can
implement for PHP which shouldn't be in PHP. Also don't under
estimate
what happens when you get bloatware and over time how much harder it
becomes to maintain and also how it does eventually end up slowing
things down (over time there are more branches, more code a.k.a more
cache misses, etc...)You are right here. But define bloatware. Type hinting is already in
PHP. All I want to do is add a couple of types to this already
implemented feature. My patch is pretty small.
Small small but the way it is currently implemented I both don't agree
with (semantics) and I think it can also be implemented better (for
starters I'd use a switch() statement in the runtime piece. I am not too
worried about zend_compile.c).
P.S.- The runtime piece of this patch also looks wrong. I didn't
dive
deep but those if() statements don't look too friendly.It's worked for me fine for a couple of months. The if statements in
zend_compile.c are pretty confusing (as far as the brace indentation
goes).
I'm sure it worked fine for you but I don't think I agree with the
semantics and I believe implementation could be improved.
Andi
See below:
-----Original Message-----
From: Sam Barrow [mailto:sam@sambarrow.com]
Sent: Friday, January 04, 2008 5:47 AM
To: Andi Gutmans
Cc: internals@lists.php.net
Subject: RE: [PHP-DEV] RE: Optional scalar type hintingWe've discussed scalar type hinting many times in the past and
decided
against it.
It really doesn't fit in very well with PHP's loosely typed nature
which
is one of the main reasons it has been so easy to use. The only
reason
why it works with classes and arrays is because those are
unambiguous
constructs.It doesn't fit. This is not a technical argument, this is your opinion
on the way PHP should be. That's the problem here.Are you saying that languages are designed only based on technicalities?
So why are languages so different? Each language has a spirit and an
emphasis and this is definitely something to think about. It also
relates to what the applicable market and users are.
True. I'm saying that this is being taken too far in this case. PHP with
type hinting is still PHP, I am not asking to redesign the entire
structure of the language.
-snip-
So why don't we first see if people can agree on what this does:
I think all the following ones should work:
function iwantint(int $n) {
iwantint("1"); // "1" + 1 works great.function iwantstring(string $str) {}
iwantstring($toStringObject); // Works instrcmp()
function iwantfloat(float $f) {}
iwantfloat(2);function iwantbool(bool $b) {}
iwantbool(1);function iwantnull(null $n) {}
iwantnull(null); //Does this exist?
Null hint does not exist, as null is only one value and it would be
pointless.
I've stated my opinion on this, I'm going for standard hinting. Int
means int, not "1" or "one" or "1one". Bool means boolean true or false,
not "true", 1, 0, "0", etc.
And for consistency of the language we would be using PHP's type
juggling system to see if there's a match or not and you'd get whatever
results you get today.Btw I think there's absolutely no argument that the error should be
exactly the same as with array/class hints. No way are we going to have
two different errors for the same kind of functionality. That's
ludicrous.
Errors should all be the same for any type hint.
More below:...
Saying that it won't confuse newbies is also wrong. PHP is so
popular
because it's so easy for people to pick up. Part of this also
includes
ability to look at other people's code, understand it, copy-on-write
(e.g. a Wordpress plug-in). The more features in PHP the harder
it'll
be
for developers to deal with. Yes, we bit the bullet for namespaces
because there were strong reasons in favor of it, we as a community
had
it on our wishlist for a long time and a lot of work went into it.
And
even when we did it we had to find a way to make it work for PHP. In
this case, with the lose typing of PHP creating a consistent model
which
will be apparent to people without many questions is a big issue.I've said this numerous times too. How hard is it to understand? If
someone can't understand this simple concept they are sure as hell not
going to understand object oriented or anything.There are actually a lot of PHP developers who don't do OO oriented.
This is why we left it as a choice to developers (it's been around btw
since 1997 at least the basic OO functionality).
OK. And they're not using OO right? So tell them not to use type hinting
either!
Anyway what's confusing here are the semantics not the feature itself.
For example, I expect something different than you do.Plus the fact that this is actually easier to understand than
array/object type hints.I don't think so because there's no potential type juggling in those
(which is why we did it) but it's a pointless argument because it
doesn't add much value to the discussion.
You have a point, but it is still a very simple concept.
Also, for those who say that when this feature is not used it
doesn't
have a performance impact that is also a problematic statement. If
it
exists people will use it. Most people using PHP don't understand
(and
shouldn't have to understand) how the language is implemented. It
may
very well be that even if we could agree on the semantics we'll see
all
sorts of developers "decorate" their code with type hints (because
they
think it'll help readability) who may down the road find themselves
in a
surprise when it comes to performance.Well that's their decision. I could use functions and not use a single
class in my applications, and they would be faster. I sacrifice a
little
performance in order to make my code better.Last, just because something can be implemented and a patch exists
doesn't mean it should be in PHP. There are lots of things I can
implement for PHP which shouldn't be in PHP. Also don't under
estimate
what happens when you get bloatware and over time how much harder it
becomes to maintain and also how it does eventually end up slowing
things down (over time there are more branches, more code a.k.a more
cache misses, etc...)You are right here. But define bloatware. Type hinting is already in
PHP. All I want to do is add a couple of types to this already
implemented feature. My patch is pretty small.Small small but the way it is currently implemented I both don't agree
with (semantics) and I think it can also be implemented better (for
starters I'd use a switch() statement in the runtime piece. I am not too
worried about zend_compile.c).P.S.- The runtime piece of this patch also looks wrong. I didn't
dive
deep but those if() statements don't look too friendly.It's worked for me fine for a couple of months. The if statements in
zend_compile.c are pretty confusing (as far as the brace indentation
goes).I'm sure it worked fine for you but I don't think I agree with the
semantics and I believe implementation could be improved.
Semantics can be changed, my only goal is to enable scalar type hinting in PHP.
Implementation is a small issue. I have no problem fixing up my code to
the community's liking.
I've stated my opinion on this, I'm going for standard hinting. Int
means int, not "1" or "one" or "1one". Bool means boolean true or false,
I don't see any difference in substance between 1 and "1".
not "true", 1, 0, "0", etc.
Same for boolean - I don't see any substantial difference between
(int)0, (float)0 and (boolean)false and see no reason ever to make a
function that distinguishes between those. Now, statically-typed
compiled languages do it for the very good reason - it is very hard to
implement such versatile API manually. It is infeasible to make all C
function declared as foo(int i) to take char* instead and use atoi() or
whatever it needs to be - and do it for each and every function.
That's exactly why scripting languages with loose typing were created -
to allow programmers to get rid of these technicalities and work with
substance of the values, not with bit representation of them. Now you
come back and say - no, my function would accept (boolean)0 but would
pretend it can not handle (int)0 because the bits in a type field of the
variable are wrong. What could be the practical purpose of such
dumbing-down? Getting all the stress of the programming in statically
typed language without compile-time checking and safety as a
compensation for the stress? Doesn't look as a good deal for me.
Stanislav Malyshev, Zend Software Architect
stas@zend.com http://www.zend.com/
(408)253-8829 MSN: stas@zend.com