Will method overloading by method signature be implemented in php6 or
even php 5.3?
Example:
<?php
namespace xyz;
import core::TestClass;
class Test extends TestClass {
public string function test(integer $int) {
return "Hi";
}
public integer function test(string $string, integer $int) {
return $int;
}
}
?>
I think this would be a very big advantage and would help developers to
write better code.
Hans, such overloading would be incompatible with php's dynamic nature
As far as I remember, even type-hinting for basic-types (strings,
integers) was rejected
Will method overloading by method signature be implemented in php6 or
even php 5.3?Example:
<?php
namespace xyz;
import core::TestClass;
class Test extends TestClass {
public string function test(integer $int) { return "Hi"; } public integer function test(string $string, integer $int) { return $int; }
}
?>
I think this would be a very big advantage and would help developers to
write better code.
--
Alexey Zakhlestin
http://blog.milkfarmsoft.com/
Why would it be incompatible with php's dynamic nature? (I already heard many people saying "that this is not the php way") But why ? Don't you like it or do you think it is just not possible to be implemented in php?
I could provide a patch that makes it possible (even with complete visibility and inheritance rules). We already use it and it saves us a lot of code.
I think it is better than the '$x = "defaultValue"' kind of way to accept different count and types of parameters because it is MUCH more expressive and intuitive.
It wouldn't even break backward compatibility beause it is just "syntactical sugar" to checking the variable types manually and then dispatching to the right behaviour.
-----Ursprüngliche Nachricht-----
Von: Alexey Zakhlestin [mailto:indeyets@gmail.com]
Gesendet: Samstag, 13. Oktober 2007 22:22
An: Hans Moog
Cc: internals@lists.php.net
Betreff: Re: [PHP-DEV] Method overloading by method signature
Hans, such overloading would be incompatible with php's dynamic nature
As far as I remember, even type-hinting for basic-types (strings,
integers) was rejected
Will method overloading by method signature be implemented in php6 or
even php 5.3?Example:
<?php
namespace xyz;
import core::TestClass;
class Test extends TestClass {
public string function test(integer $int) { return "Hi"; } public integer function test(string $string, integer $int) { return $int; }
}
?>
I think this would be a very big advantage and would help developers to
write better code.
--
Alexey Zakhlestin
http://blog.milkfarmsoft.com/
can your patch handle the following situation? how?
class A
{
public function fun1($a)
{
//…
}
public function fun1($a, $b)
{
//…
}
}
$a = new A();
$a->fun1('a', 'b', 'c'); // which method is called here?
Why would it be incompatible with php's dynamic nature? (I already heard many people saying "that this is not the php way") But why ? Don't you like it or do you think it is just not possible to be implemented in php?
I could provide a patch that makes it possible (even with complete visibility and inheritance rules). We already use it and it saves us a lot of code.
I think it is better than the '$x = "defaultValue"' kind of way to accept different count and types of parameters because it is MUCH more expressive and intuitive.
It wouldn't even break backward compatibility beause it is just "syntactical sugar" to checking the variable types manually and then dispatching to the right behaviour.
-----Ursprüngliche Nachricht-----
Von: Alexey Zakhlestin [mailto:indeyets@gmail.com]
Gesendet: Samstag, 13. Oktober 2007 22:22
An: Hans Moog
Cc: internals@lists.php.net
Betreff: Re: [PHP-DEV] Method overloading by method signatureHans, such overloading would be incompatible with php's dynamic nature
As far as I remember, even type-hinting for basic-types (strings,
integers) was rejectedWill method overloading by method signature be implemented in php6 or
even php 5.3?Example:
<?php
namespace xyz;
import core::TestClass;
class Test extends TestClass {
public string function test(integer $int) { return "Hi"; } public integer function test(string $string, integer $int) { return $int; }
}
?>
I think this would be a very big advantage and would help developers to
write better code.--
Alexey Zakhlestin
http://blog.milkfarmsoft.com/
--
Alexey Zakhlestin
http://blog.milkfarmsoft.com/
It shows an error that fun1 is not implemented for the signature of the passed parameters.
But you could still provide a method that matches any number and type of parameters by adding the following method declaration.
/**
- This method accepts parameters of any type and any count.
/
public function fun1($parameters ...) {
/- array(3) {
- [0] => string(1) "a"
- [1] => string(2) "b"
- [2] => string(3) "c"
- }
*/
var_dump($parameters);
}
The "..." notation (making it possible to have a dynamic count of parameters of the given type (missing typehint automatically chooses mixed)) is only allowed for the last parameter.
So method defintions like the following one are also possible:
public function fun1(string $firstParam, string $remainingParameters ...) {
/*
- string(1) "a"
*/
var_dump($firstParam);
/*
- array(3) {
- [0] => string(1) "b"
- [1] => string(2) "c"
- }
*/
var_dump($remainingParameters);
}
-----Ursprüngliche Nachricht-----
Von: Alexey Zakhlestin [mailto:indeyets@gmail.com]
Gesendet: Samstag, 13. Oktober 2007 23:20
An: Hans Moog
Cc: internals@lists.php.net
Betreff: Re: [PHP-DEV] Method overloading by method signature
can your patch handle the following situation? how?
class A
{
public function fun1($a)
{
//…
}
public function fun1($a, $b)
{
//…
}
}
$a = new A();
$a->fun1('a', 'b', 'c'); // which method is called here?
Why would it be incompatible with php's dynamic nature? (I already heard many people saying "that this is not the php way") But why ? Don't you like it or do you think it is just not possible to be implemented in php?
I could provide a patch that makes it possible (even with complete visibility and inheritance rules). We already use it and it saves us a lot of code.
I think it is better than the '$x = "defaultValue"' kind of way to accept different count and types of parameters because it is MUCH more expressive and intuitive.
It wouldn't even break backward compatibility beause it is just "syntactical sugar" to checking the variable types manually and then dispatching to the right behaviour.
-----Ursprüngliche Nachricht-----
Von: Alexey Zakhlestin [mailto:indeyets@gmail.com]
Gesendet: Samstag, 13. Oktober 2007 22:22
An: Hans Moog
Cc: internals@lists.php.net
Betreff: Re: [PHP-DEV] Method overloading by method signatureHans, such overloading would be incompatible with php's dynamic nature
As far as I remember, even type-hinting for basic-types (strings,
integers) was rejectedWill method overloading by method signature be implemented in php6 or
even php 5.3?Example:
<?php
namespace xyz;
import core::TestClass;
class Test extends TestClass {
public string function test(integer $int) { return "Hi"; } public integer function test(string $string, integer $int) { return $int; }
}
?>
I think this would be a very big advantage and would help developers to
write better code.--
Alexey Zakhlestin
http://blog.milkfarmsoft.com/
--
Alexey Zakhlestin
http://blog.milkfarmsoft.com/
It shows an error that fun1 is not implemented for the signature of the passed parameters.
But you could still provide a method that matches any number and type of parameters by adding the following method declaration.
Well, this looks exactly like incompatibility to me, because currently
any function accepts any number of parameters (and you get
non-explicit ones by using func_get_args()
)
/**
- This method accepts parameters of any type and any count.
/
public function fun1($parameters ...) {
/
- array(3) {
- [0] => string(1) "a"
- [1] => string(2) "b"
- [2] => string(3) "c"
- }
*/
var_dump($parameters);
}The "..." notation (making it possible to have a dynamic count of parameters of the given type (missing typehint automatically chooses mixed)) is only allowed for the last parameter.
So method defintions like the following one are also possible:
public function fun1(string $firstParam, string $remainingParameters ...) {
/*
- string(1) "a"
*/
var_dump($firstParam);/*
- array(3) {
- [0] => string(1) "b"
- [1] => string(2) "c"
- }
*/
var_dump($remainingParameters);
}-----Ursprüngliche Nachricht-----
Von: Alexey Zakhlestin [mailto:indeyets@gmail.com]
Gesendet: Samstag, 13. Oktober 2007 23:20
An: Hans Moog
Cc: internals@lists.php.net
Betreff: Re: [PHP-DEV] Method overloading by method signaturecan your patch handle the following situation? how?
class A
{
public function fun1($a)
{
//…
}public function fun1($a, $b) { //… }
}
$a = new A();
$a->fun1('a', 'b', 'c'); // which method is called here?Why would it be incompatible with php's dynamic nature? (I already heard many people saying "that this is not the php way") But why ? Don't you like it or do you think it is just not possible to be implemented in php?
I could provide a patch that makes it possible (even with complete visibility and inheritance rules). We already use it and it saves us a lot of code.
I think it is better than the '$x = "defaultValue"' kind of way to accept different count and types of parameters because it is MUCH more expressive and intuitive.
It wouldn't even break backward compatibility beause it is just "syntactical sugar" to checking the variable types manually and then dispatching to the right behaviour.
-----Ursprüngliche Nachricht-----
Von: Alexey Zakhlestin [mailto:indeyets@gmail.com]
Gesendet: Samstag, 13. Oktober 2007 22:22
An: Hans Moog
Cc: internals@lists.php.net
Betreff: Re: [PHP-DEV] Method overloading by method signatureHans, such overloading would be incompatible with php's dynamic nature
As far as I remember, even type-hinting for basic-types (strings,
integers) was rejectedWill method overloading by method signature be implemented in php6 or
even php 5.3?Example:
<?php
namespace xyz;
import core::TestClass;
class Test extends TestClass {
public string function test(integer $int) { return "Hi"; } public integer function test(string $string, integer $int) { return $int; }
}
?>
I think this would be a very big advantage and would help developers to
write better code.--
Alexey Zakhlestin
http://blog.milkfarmsoft.com/--
Alexey Zakhlestin
http://blog.milkfarmsoft.com/
--
Alexey Zakhlestin
http://blog.milkfarmsoft.com/
As long as you only got one method per name it doesn't change things, so it wouldn't brake backward compatibility and old functions would still behave like expected (so it is not incompatible).
But if you want to be more strict and limit the coder to use your functions only with a specific parameter signature (and thats the only reason to use type hints - to ensure the method is used correctly and build more robust applications), it is better to tell the coder that he made a mistake than just going on and guessing which method to call.
The new PHP5 object model introduces signature checking (yay!) but since PHP doesn't allow overloading a great deal of flexibilty was lost in this move (boo!). No longer can you override a method & provide an incompatible signature. This mixture of strict OO + PHP's traditional loose typing is really shooting PHP in the foot.
I think, that this ist the reason why nearly nobody uses this great new feature (to ensure that the passed parameters are correct), because when he does, he isn't able to provide different ways to use a method anymore.
And allowing signature checks would really enhance php's security because an integer id (for example) can never be used to insert malicious sql into an sql statement and by that create an sql injection.
You wouldn't have to put a mysql_escape_string (or integer typecast) around every single parameter anymore because you would be able to rely on an integer id, really being an integer id without messing around with thousands of custom checks (that would even boost performance since string operations are always a bit slow).
And like i already said - this "new feature" wouldn't make php a strong typed language (you would still have all the possibilities of using variables as different types), it would just offer the coder a method to write more strict method declarations that would definitly enhance the performance, security an robustness of applications.
-----Ursprüngliche Nachricht-----
Von: Alexey Zakhlestin [mailto:indeyets@gmail.com]
Gesendet: Sonntag, 14. Oktober 2007 10:04
An: Hans Moog
Cc: internals@lists.php.net
Betreff: Re: [PHP-DEV] Method overloading by method signature
It shows an error that fun1 is not implemented for the signature of the passed parameters.
But you could still provide a method that matches any number and type of parameters by adding the following method declaration.
Well, this looks exactly like incompatibility to me, because currently
any function accepts any number of parameters (and you get
non-explicit ones by using func_get_args()
)
/**
- This method accepts parameters of any type and any count.
/
public function fun1($parameters ...) {
/
- array(3) {
- [0] => string(1) "a"
- [1] => string(2) "b"
- [2] => string(3) "c"
- }
*/
var_dump($parameters);
}The "..." notation (making it possible to have a dynamic count of parameters of the given type (missing typehint automatically chooses mixed)) is only allowed for the last parameter.
So method defintions like the following one are also possible:
public function fun1(string $firstParam, string $remainingParameters ...) {
/*
- string(1) "a"
*/
var_dump($firstParam);/*
- array(3) {
- [0] => string(1) "b"
- [1] => string(2) "c"
- }
*/
var_dump($remainingParameters);
}-----Ursprüngliche Nachricht-----
Von: Alexey Zakhlestin [mailto:indeyets@gmail.com]
Gesendet: Samstag, 13. Oktober 2007 23:20
An: Hans Moog
Cc: internals@lists.php.net
Betreff: Re: [PHP-DEV] Method overloading by method signaturecan your patch handle the following situation? how?
class A
{
public function fun1($a)
{
//…
}public function fun1($a, $b) { //… }
}
$a = new A();
$a->fun1('a', 'b', 'c'); // which method is called here?Why would it be incompatible with php's dynamic nature? (I already heard many people saying "that this is not the php way") But why ? Don't you like it or do you think it is just not possible to be implemented in php?
I could provide a patch that makes it possible (even with complete visibility and inheritance rules). We already use it and it saves us a lot of code.
I think it is better than the '$x = "defaultValue"' kind of way to accept different count and types of parameters because it is MUCH more expressive and intuitive.
It wouldn't even break backward compatibility beause it is just "syntactical sugar" to checking the variable types manually and then dispatching to the right behaviour.
-----Ursprüngliche Nachricht-----
Von: Alexey Zakhlestin [mailto:indeyets@gmail.com]
Gesendet: Samstag, 13. Oktober 2007 22:22
An: Hans Moog
Cc: internals@lists.php.net
Betreff: Re: [PHP-DEV] Method overloading by method signatureHans, such overloading would be incompatible with php's dynamic nature
As far as I remember, even type-hinting for basic-types (strings,
integers) was rejectedWill method overloading by method signature be implemented in php6 or
even php 5.3?Example:
<?php
namespace xyz;
import core::TestClass;
class Test extends TestClass {
public string function test(integer $int) { return "Hi"; } public integer function test(string $string, integer $int) { return $int; }
}
?>
I think this would be a very big advantage and would help developers to
write better code.--
Alexey Zakhlestin
http://blog.milkfarmsoft.com/--
Alexey Zakhlestin
http://blog.milkfarmsoft.com/
--
Alexey Zakhlestin
http://blog.milkfarmsoft.com/
But if you want to be more strict and limit the coder to use your
functions only with a specific parameter signature (and thats the
The purpose of signature overloading in most languages isn't input type
control, it's different functionality on different types. However,
unlike many other languages, PHP does not have static typing, and that
would not allow to know which function is called immediately, only at
runtime. This has both performance impact and code transparency impact -
you wouldn't know which function will be called until runtime. I am not
convinced this added complexity serves any real need - though you are
welcome to prove otherwise, i.e. present some use case when one can't do
without signature overloading.
only reason to use type hints - to ensure the method is used
correctly and build more robust applications), it is better to tell
BTW, I'm not sure how exactly it makes the code more robust - if you
call it with wrong type and it's not checked, the app would probably die
on fatal error. If you call it with wrong type and it is checked, the
app would die on fatal error couple of lines above. Unless you use some
kind of static analysis tool to verify your app prior to deployment (if
you know such tools please tell me!) I don't see much difference here,
mostly syntax sugar and enforcing right style (which is not bad - just
it's not that big a deal).
lost in this move (boo!). No longer can you override a method &
provide an incompatible signature. This mixture of strict OO + PHP's
traditional loose typing is really shooting PHP in the foot.
If you don't want strict signatures - why you are using them? Nobody
forces you to type your arguments. And inheritance is certainly not for
overriding objects with incompatible signatures, if you try to do it you
are misusing it.
I think, that this ist the reason why nearly nobody uses this great
new feature (to ensure that the passed parameters are correct),
The above statement is not true. This feature is used by many
developers. Please stop using "unless you do it my way, nobody will use
PHP" argument - it doesn't work.
Stanislav Malyshev, Zend Software Architect
stas@zend.com http://www.zend.com/
(408)253-8829 MSN: stas@zend.com
Posting to newsgroup php.internals, you wrote:
But if you want to be more strict and limit the coder to use your
functions only with a specific parameter signature (and thats theThe purpose of signature overloading in most languages isn't input type
control, it's different functionality on different types. However,
unlike many other languages, PHP does not have static typing, and that
would not allow to know which function is called immediately, only at
runtime. This has both performance impact and code transparency impact -
you wouldn't know which function will be called until runtime. I am not
convinced this added complexity serves any real need - though you are
welcome to prove otherwise, i.e. present some use case when one can't do
without signature overloading.only reason to use type hints - to ensure the method is used
correctly and build more robust applications), it is better to tellBTW, I'm not sure how exactly it makes the code more robust - if you
call it with wrong type and it's not checked, the app would probably die
on fatal error. If you call it with wrong type and it is checked, the
app would die on fatal error couple of lines above. Unless you use some
kind of static analysis tool to verify your app prior to deployment (if
you know such tools please tell me!) I don't see much difference here,
mostly syntax sugar and enforcing right style (which is not bad - just
it's not that big a deal).lost in this move (boo!). No longer can you override a method &
provide an incompatible signature. This mixture of strict OO + PHP's
traditional loose typing is really shooting PHP in the foot.If you don't want strict signatures - why you are using them? Nobody
forces you to type your arguments. And inheritance is certainly not for
overriding objects with incompatible signatures, if you try to do it you
are misusing it.I think, that this ist the reason why nearly nobody uses this great
new feature (to ensure that the passed parameters are correct),The above statement is not true. This feature is used by many
developers. Please stop using "unless you do it my way, nobody will use
PHP" argument - it doesn't work.Stanislav Malyshev, Zend Software Architect
stas@zend.com http://www.zend.com/
(408)253-8829 MSN: stas@zend.com
Ciao,
/|\ Umberto Salsi
/_/ www.icosaedro.it
Posting to newsgroup php.internals, Stanislav Malyshev wrote:
only reason to use type hints - to ensure the method is used
correctly and build more robust applications), it is better to tellBTW, I'm not sure how exactly it makes the code more robust - if you
call it with wrong type and it's not checked, the app would probably die
on fatal error. If you call it with wrong type and it is checked, the
app would die on fatal error couple of lines above. Unless you use some
kind of static analysis tool to verify your app prior to deployment (if
you know such tools please tell me!) I don't see much difference here,
mostly syntax sugar and enforcing right style (which is not bad - just
it's not that big a deal).
Most of the current applications of PHP do not need strict type checking, and
PHP as we know today perfectly fits these needs. But debugging and "cleaning"
large applications may become a nightmare.
That's why I developed PHPLint, a PHP parser and validator that performs
a static analysis of the source, ensuring the safe handling of types. In
a word, this tool makes PHP very close to a strong-typed language without
the need to further complicate the interpreter with new features that would
pervert the nature of the language.
Every constant, variable, property has a well defined type, and every
function and method has a well defined signature that can be guessed by
PHPLint or provided through specific meta-code as in this example:
$i = 123;
type of the variable guessed as int
define("RND_K", 1.0 / (1.0 + getrandmax()
));
type of the constant guessed as double
function rnd()
{
return RND_K * rand()
;
}
signature guessed as float()
$i = rnd();
ERROR: assigning double to int
function textToattribute(/.string./ $a)
{
return """ . htmlspecialchars($a) . """;
}
signature string(string)
Best regards,
/|\ Umberto Salsi
/_/ www.icosaedro.it
Yeah PHPLint is cool and very useful when you want to be strict. We do use it regularly. But it doesn't enable you to overload your methods as described because this could only be done by a native language construct or manual type checks and dispatching.
I thought it would be a good idea to embed this into the language because php is getting more and more object oriented and overloading functions is one of the basic features of an object oriented programming language. Overloading is already possible but php doesn't offer the coder a short syntax to support him writing oveloaded functions. I would love to see a solution for simpler overloading since that would result in a better readability of code.
But since I and Richard Quadling seem to be the only ones who would like to have this feature in php, I will put no further effort in this request and comply to the will of the majority :|
Perhaps a later release of php will have this feature. (After all, even namespace support was added and nobody wanted namespaces at first, because it was not the "php way" - so there is still hope :P )
-----Ursprüngliche Nachricht-----
Von: Umberto Salsi [mailto:salsi@icosaedro.it]
Gesendet: Montag, 15. Oktober 2007 16:36
An: internals@lists.php.net
Betreff: Re: AW: [PHP-DEV] Method overloading by method signature
Posting to newsgroup php.internals, Stanislav Malyshev wrote:
only reason to use type hints - to ensure the method is used
correctly and build more robust applications), it is better to tellBTW, I'm not sure how exactly it makes the code more robust - if you
call it with wrong type and it's not checked, the app would probably die
on fatal error. If you call it with wrong type and it is checked, the
app would die on fatal error couple of lines above. Unless you use some
kind of static analysis tool to verify your app prior to deployment (if
you know such tools please tell me!) I don't see much difference here,
mostly syntax sugar and enforcing right style (which is not bad - just
it's not that big a deal).
Most of the current applications of PHP do not need strict type checking, and
PHP as we know today perfectly fits these needs. But debugging and "cleaning"
large applications may become a nightmare.
That's why I developed PHPLint, a PHP parser and validator that performs
a static analysis of the source, ensuring the safe handling of types. In
a word, this tool makes PHP very close to a strong-typed language without
the need to further complicate the interpreter with new features that would
pervert the nature of the language.
Every constant, variable, property has a well defined type, and every
function and method has a well defined signature that can be guessed by
PHPLint or provided through specific meta-code as in this example:
$i = 123;
type of the variable guessed as int
define("RND_K", 1.0 / (1.0 + getrandmax()
));
type of the constant guessed as double
function rnd()
{
return RND_K * rand()
;
}
signature guessed as float()
$i = rnd();
ERROR: assigning double to int
function textToattribute(/.string./ $a)
{
return """ . htmlspecialchars($a) . """;
}
signature string(string)
Best regards,
/|\ Umberto Salsi
/_/ www.icosaedro.it
But since I and Richard Quadling seem to be the only ones who would like
to have this feature in php [...]
No, you are of course not alone.
ragards dtg
--
_
ASCII ribbon campaign ( )
against HTML e-mail X
/ \
FWIW, I think this is a great feature. Forgive my lack of intimate knowledge
with the core and extension, but would this be possible as an extension to
PHP? i.e., xdebug modifies how certain internals of PHP work through the
zend_extension_ts ini setting.
Yeah PHPLint is cool and very useful when you want to be strict. We do use
it regularly. But it doesn't enable you to overload your methods as
described because this could only be done by a native language construct or
manual type checks and dispatching.I thought it would be a good idea to embed this into the language because
php is getting more and more object oriented and overloading functions is
one of the basic features of an object oriented programming language.
Overloading is already possible but php doesn't offer the coder a short
syntax to support him writing oveloaded functions. I would love to see a
solution for simpler overloading since that would result in a better
readability of code.But since I and Richard Quadling seem to be the only ones who would like
to have this feature in php, I will put no further effort in this request
and comply to the will of the majority :|Perhaps a later release of php will have this feature. (After all, even
namespace support was added and nobody wanted namespaces at first, because
it was not the "php way" - so there is still hope :P )-----Ursprüngliche Nachricht-----
Von: Umberto Salsi [mailto:salsi@icosaedro.it]
Gesendet: Montag, 15. Oktober 2007 16:36
An: internals@lists.php.net
Betreff: Re: AW: [PHP-DEV] Method overloading by method signaturePosting to newsgroup php.internals, Stanislav Malyshev wrote:
only reason to use type hints - to ensure the method is used
correctly and build more robust applications), it is better to tellBTW, I'm not sure how exactly it makes the code more robust - if you
call it with wrong type and it's not checked, the app would probably die
on fatal error. If you call it with wrong type and it is checked, the
app would die on fatal error couple of lines above. Unless you use some
kind of static analysis tool to verify your app prior to deployment (if
you know such tools please tell me!) I don't see much difference here,
mostly syntax sugar and enforcing right style (which is not bad - just
it's not that big a deal).Most of the current applications of PHP do not need strict type checking,
and
PHP as we know today perfectly fits these needs. But debugging and
"cleaning"
large applications may become a nightmare.That's why I developed PHPLint, a PHP parser and validator that performs
a static analysis of the source, ensuring the safe handling of types. In
a word, this tool makes PHP very close to a strong-typed language without
the need to further complicate the interpreter with new features that
would
pervert the nature of the language.Every constant, variable, property has a well defined type, and every
function and method has a well defined signature that can be guessed by
PHPLint or provided through specific meta-code as in this example:$i = 123;
type of the variable guessed as int
define("RND_K", 1.0 / (1.0 +
getrandmax()
));type of the constant guessed as double
function rnd()
{
return RND_K *rand()
;
}signature guessed as float()
$i = rnd();
ERROR: assigning double to int
function textToattribute(/.string./ $a)
{
return """ . htmlspecialchars($a) . """;
}signature string(string)
Best regards,
/|\ Umberto Salsi
/_/ www.icosaedro.it--
--
It looked like something resembling white marble, which was
probably what it was: something resembling white marble.
-- Douglas Adams, "The Hitchhikers Guide to the Galaxy"
That's why I developed PHPLint, a PHP parser and validator that performs
a static analysis of the source, ensuring the safe handling of types. In
a word, this tool makes PHP very close to a strong-typed language without
the need to further complicate the interpreter with new features that would
pervert the nature of the language.
I think this is better solution than messing with the language :)
Stanislav Malyshev, Zend Software Architect
stas@zend.com http://www.zend.com/
(408)253-8829 MSN: stas@zend.com
It allows you to be strict when messing with types but it doesn't allow you to overload type hinted methods ... so this is no solution to the problem of overloading type hinted methods, is it ?
-----Ursprüngliche Nachricht-----
Von: Stanislav Malyshev [mailto:stas@zend.com]
Gesendet: Mo 15.10.2007 18:38
An: Umberto Salsi
Cc: internals@lists.php.net
Betreff: Re: AW: [PHP-DEV] Method overloading by method signature
That's why I developed PHPLint, a PHP parser and validator that performs
a static analysis of the source, ensuring the safe handling of types. In
a word, this tool makes PHP very close to a strong-typed language without
the need to further complicate the interpreter with new features that would
pervert the nature of the language.
I think this is better solution than messing with the language :)
Stanislav Malyshev, Zend Software Architect
stas@zend.com http://www.zend.com/
(408)253-8829 MSN: stas@zend.com
Hello Umberto,
Monday, October 15, 2007, 3:36:22 PM, you wrote:
Posting to newsgroup php.internals, Stanislav Malyshev wrote:
only reason to use type hints - to ensure the method is used
correctly and build more robust applications), it is better to tellBTW, I'm not sure how exactly it makes the code more robust - if you
call it with wrong type and it's not checked, the app would probably die
on fatal error. If you call it with wrong type and it is checked, the
app would die on fatal error couple of lines above. Unless you use some
kind of static analysis tool to verify your app prior to deployment (if
you know such tools please tell me!) I don't see much difference here,
mostly syntax sugar and enforcing right style (which is not bad - just
it's not that big a deal).
Most of the current applications of PHP do not need strict type checking, and
PHP as we know today perfectly fits these needs. But debugging and "cleaning"
large applications may become a nightmare.
That's why I developed PHPLint,
I think this is a very nice tool and the way to go in PHP. You should
showcase demo it on one of the conferences! And provide links here of
course :-)
marcus
a PHP parser and validator that performs
a static analysis of the source, ensuring the safe handling of types. In
a word, this tool makes PHP very close to a strong-typed language without
the need to further complicate the interpreter with new features that would
pervert the nature of the language.
Every constant, variable, property has a well defined type, and every
function and method has a well defined signature that can be guessed by
PHPLint or provided through specific meta-code as in this example:
$i = 123;
type of the variable guessed as int
define("RND_K", 1.0 / (1.0 +
getrandmax()
));type of the constant guessed as double
function rnd()
{
return RND_K *rand()
;
}signature guessed as float()
$i = rnd();
ERROR: assigning double to int
function textToattribute(/.string./ $a)
{
return """ . htmlspecialchars($a) . """;
}signature string(string)
Best regards,
/|\ Umberto Salsi
/_/ www.icosaedro.it
Best regards,
Marcus
Hello Hans,
If you have such a patch you should definitively post it here so that we
can hve a look. Most interesting to us is however the oerformance impact. As
that was the main reason to go any further than adding return type hints.
marcus
Saturday, October 13, 2007, 10:47:23 PM, you wrote:
Why would it be incompatible with php's dynamic nature? (I already heard
many people saying "that this is not the php way") But why ? Don't you
like it or do you think it is just not possible to be implemented in php?
I could provide a patch that makes it possible (even with complete
visibility and inheritance rules). We already use it and it saves us a lot of code.
I think it is better than the '$x = "defaultValue"' kind of way to accept
different count and types of parameters because it is MUCH more expressive and intuitive.
It wouldn't even break backward compatibility beause it is just
"syntactical sugar" to checking the variable types manually and then dispatching to the right behaviour.
-----Ursprüngliche Nachricht-----
Von: Alexey Zakhlestin [mailto:indeyets@gmail.com]
Gesendet: Samstag, 13. Oktober 2007 22:22
An: Hans Moog
Cc: internals@lists.php.net
Betreff: Re: [PHP-DEV] Method overloading by method signature
Hans, such overloading would be incompatible with php's dynamic nature
As far as I remember, even type-hinting for basic-types (strings,
integers) was rejected
Will method overloading by method signature be implemented in php6 or
even php 5.3?Example:
<?php
namespace xyz;
import core::TestClass;
class Test extends TestClass {
public string function test(integer $int) { return "Hi"; } public integer function test(string $string, integer $int) { return $int; }
}
?>
I think this would be a very big advantage and would help developers to
write better code.
Best regards,
Marcus
I will do so, but i will have to modify it to work with the current php version and I wanted to know if everybody hates this way of method overloading first.
Btw: We made some benchmark tests when we decided to use it. And the method calls were about 0.1 Percent slower. But without having to check or escape every parameter anymore it even boosts performance for us, since we really try to build very robust applications and because of that check all parameters that could be problematic.
But if you write quick and dirty code without thinking about sql injections or other problems and not checking anyhting, it is definitly a bit slower (about 0.1 percent of a normal method call).
But I think this loss of performance is worth it since the current way of php's mixture of strict OO + traditional loose typing is really shooting PHP in the foot. (Nobody will ever really use typehinting when he want's to offer a number of ways to use a method).
-----Ursprüngliche Nachricht-----
Von: Marcus Boerger [mailto:helly@php.net]
Gesendet: Sonntag, 14. Oktober 2007 09:29
An: Hans Moog
Cc: Alexey Zakhlestin; internals@lists.php.net
Betreff: Re: AW: [PHP-DEV] Method overloading by method signature
Hello Hans,
If you have such a patch you should definitively post it here so that we
can hve a look. Most interesting to us is however the oerformance impact. As
that was the main reason to go any further than adding return type hints.
marcus
Saturday, October 13, 2007, 10:47:23 PM, you wrote:
Why would it be incompatible with php's dynamic nature? (I already heard
many people saying "that this is not the php way") But why ? Don't you
like it or do you think it is just not possible to be implemented in php?
I could provide a patch that makes it possible (even with complete
visibility and inheritance rules). We already use it and it saves us a lot of code.
I think it is better than the '$x = "defaultValue"' kind of way to accept
different count and types of parameters because it is MUCH more expressive and intuitive.
It wouldn't even break backward compatibility beause it is just
"syntactical sugar" to checking the variable types manually and then dispatching to the right behaviour.
-----Ursprüngliche Nachricht-----
Von: Alexey Zakhlestin [mailto:indeyets@gmail.com]
Gesendet: Samstag, 13. Oktober 2007 22:22
An: Hans Moog
Cc: internals@lists.php.net
Betreff: Re: [PHP-DEV] Method overloading by method signature
Hans, such overloading would be incompatible with php's dynamic nature
As far as I remember, even type-hinting for basic-types (strings,
integers) was rejected
Will method overloading by method signature be implemented in php6 or
even php 5.3?Example:
<?php
namespace xyz;
import core::TestClass;
class Test extends TestClass {
public string function test(integer $int) { return "Hi"; } public integer function test(string $string, integer $int) { return $int; }
}
?>
I think this would be a very big advantage and would help developers to
write better code.
Best regards,
Marcus
Btw: We made some benchmark tests when we decided to use it. And the
method calls were about 0.1 Percent slower. But without having to
check or escape every parameter anymore it even boosts performance
If you think parameter typing would free you from having input
validation, IMHO you are on a wrong way.
Stanislav Malyshev, Zend Software Architect
stas@zend.com http://www.zend.com/
(408)253-8829 MSN: stas@zend.com
Hello Hans,
Sunday, October 14, 2007, 1:11:35 PM, you wrote:
I will do so, but i will have to modify it to work with the current php
version and I wanted to know if everybody hates this way of method overloading first.
Btw: We made some benchmark tests when we decided to use it. And the
method calls were about 0.1 Percent slower. But without having to check or
escape every parameter anymore it even boosts performance for us, since we
really try to build very robust applications and because of that check all
parameters that could be problematic.
But if you write quick and dirty code without thinking about sql
injections or other problems and not checking anyhting, it is definitly a
bit slower (about 0.1 percent of a normal method call).
But I think this loss of performance is worth it since the current way of
php's mixture of strict OO + traditional loose typing is really shooting
PHP in the foot. (Nobody will ever really use typehinting when he want's
to offer a number of ways to use a method).
Reading this it occurs to me that you are on a completely wrong track. PHP
is untyped for a good reason. It was a choice to deal with the untyped web
environment. Later we added type hints to help code readability. Yet OOP is
only a small apspect of PHP and always will be. That said the language for
you would probably have another one. Also type hinting and overloading do
not in any way free you from filtering and escaping. If you think so you may
test your code again and hopefuly not find too many security holes.
The reason I asked for performance impact is that in general we do not like
to add features that are not necessary for the web environment but have a
negative impact on it. Also reading of an impact of 0.1 percent seems very
strange as basically anything below 2% is close to the measurable limit and
the onlt way to measure such tiny impacts would be counting assembled
instructions using callgrind.
Last but not least we added typehints to enable developers to generate
better understandable error messages. These go especially well with the
typical easy PHP application designs and PHPs stack trace ability in
exception handling.
marcus
Hi,
[...]
Later we added type hints to help code readability.
Let me jump at this:
==
function xpath($arg) {
if ($arg instanceof DomDocument) {
return new DomXPath($arg);
} else if ($arg instanceof XmlTree) {
return new DomXPath($this->loadXML($arg->getSource()));
} else if (is_string($arg)) {
return new DomXPath($this->loadXML($arg));
} else {
throw new IllegalArgumentException('Unsupported argument type');
}
}
== vs. ==
function xpath(DomDocument $arg) {
return new DomXPath($arg);
}
function xpath(XmlTree $arg) {
return new DomXPath($this->loadXML($arg->getSource())));
}
function xpath($arg) { // Untyped = default
if (!is_string($arg)) {
throw new IllegalArgumentException('Unsupported argument type');
}
return new DomXPath($this->loadXML($arg));
}
==
If we consider the readability argument only: Which one of the above more
readable? You decide:)
- Timm
When it would be:
==
function xpath(DomDocument $arg) {
return new DomXPath($arg);
}
function xpath(XmlTree $arg) {
return new DomXPath($this->loadXML($arg->getSource())));
}
function xpath(string $arg) {
return new DomXPath($this->loadXML($arg));
}
(since when method overloding by sigantures wer put into php, scalar types should be possible, too, shouldn't they?)
I would prefer the second one because I understand the behaviour much faster than analyzing the if switch (and this if switch is really simple to understand because there is only one command in every block). The second reason for me to select the second version is, that I am able to write better documentation for each behaviour (although, in this situation there isn't that much to be commented).
-----Ursprüngliche Nachricht-----
Von: Timm Friebe [mailto:thekid@thekid.de]
Gesendet: Montag, 15. Oktober 2007 20:47
An: internals@lists.php.net
Betreff: Re: [PHP-DEV] Method overloading by method signature
Hi,
[...]
Later we added type hints to help code readability.
Let me jump at this:
==
function xpath($arg) {
if ($arg instanceof DomDocument) {
return new DomXPath($arg);
} else if ($arg instanceof XmlTree) {
return new DomXPath($this->loadXML($arg->getSource()));
} else if (is_string($arg)) {
return new DomXPath($this->loadXML($arg));
} else {
throw new IllegalArgumentException('Unsupported argument type');
}
}
== vs. ==
function xpath(DomDocument $arg) {
return new DomXPath($arg);
}
function xpath(XmlTree $arg) {
return new DomXPath($this->loadXML($arg->getSource())));
}
function xpath($arg) { // Untyped = default
if (!is_string($arg)) {
throw new IllegalArgumentException('Unsupported argument type');
}
return new DomXPath($this->loadXML($arg));
}
==
If we consider the readability argument only: Which one of the above more
readable? You decide:)
- Timm
When it would be:
==
function xpath(DomDocument $arg) {
return new DomXPath($arg);
}function xpath(XmlTree $arg) {
return new DomXPath($this->loadXML($arg->getSource())));
}function xpath(string $arg) {
return new DomXPath($this->loadXML($arg));
}(since when method overloding by sigantures wer put into php, scalar types should be possible, too, shouldn't they?)
I would prefer the second one because I understand the behaviour much faster than analyzing the if switch (and this if switch is really simple to understand because there is only one command in every block). The second reason for me to select the second version is, that I am able to write better documentation for each behaviour (although, in this situation there isn't that much to be commented).
-----Ursprüngliche Nachricht-----
Von: Timm Friebe [mailto:thekid@thekid.de]
Gesendet: Montag, 15. Oktober 2007 20:47
An: internals@lists.php.net
Betreff: Re: [PHP-DEV] Method overloading by method signatureHi,
[...]
Later we added type hints to help code readability.
Let me jump at this:
==
function xpath($arg) {
if ($arg instanceof DomDocument) {
return new DomXPath($arg);
} else if ($arg instanceof XmlTree) {
return new DomXPath($this->loadXML($arg->getSource()));
} else if (is_string($arg)) {
return new DomXPath($this->loadXML($arg));
} else {
throw new IllegalArgumentException('Unsupported argument type');
}
}== vs. ==
function xpath(DomDocument $arg) {
return new DomXPath($arg);
}function xpath(XmlTree $arg) {
return new DomXPath($this->loadXML($arg->getSource())));
}function xpath($arg) { // Untyped = default
if (!is_string($arg)) {
throw new IllegalArgumentException('Unsupported argument type');
}
return new DomXPath($this->loadXML($arg));
}==
If we consider the readability argument only: Which one of the above more
readable? You decide:)
- Timm
As the PHP Documentation may have to is starting adding multiple
signatures (Docbook has no mechanism if grouping optional parameters
it seems), it seems only fair that PHP itself should (flame-retardant
suit fully fitted) "catch-up". (Hey when has documentation EVER been
ahead of the game!?!).
If this feature makes PHP (or at least some aspects of it), a typed
language, then that's OK by me. It means my code is more consistent
with other languages.
--
Richard Quadling
Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731
"Standing on the shoulders of some very clever giants!"
suit fully fitted) "catch-up". (Hey when has documentation EVER been
ahead of the game!?!).
Always? Otherwise there would be no need for documentation, if
everything was in the code. Some people even start with writing docs and
only then implement the actual code. Of course, it is not always the
case (and some code, like Zend Engine, is so obvious that no docs are
needed anyway ;) but documentation containing more insight and more
content than the pure code is almost always the case, especially with
properly documented code.
Stanislav Malyshev, Zend Software Architect
stas@zend.com http://www.zend.com/
(408)253-8829 MSN: stas@zend.com
suit fully fitted) "catch-up". (Hey when has documentation EVER been
ahead of the game!?!).Always? Otherwise there would be no need for documentation, if
everything was in the code. Some people even start with writing docs and
only then implement the actual code. Of course, it is not always the
case (and some code, like Zend Engine, is so obvious that no docs are
needed anyway ;) but documentation containing more insight and more
content than the pure code is almost always the case, especially with
properly documented code.
So all I need to do is document this great new feature, hope no-one
notices and then come screaming saying that it doesn't work as
documented.
Hmm.
Possibly not a good way to make friends and influence people.
This feature, along with property accessibility via some sort of
read/write mechanism would be more useful to me than some other
features (like cough-cough unicode cough-cough).
It is something I am familiar with in other languages.
Richard Quadling
Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731
"Standing on the shoulders of some very clever giants!"
Hans Moog wrote:
When it would be:
==
function xpath(DomDocument $arg) {
return new DomXPath($arg);
}function xpath(XmlTree $arg) {
return new DomXPath($this->loadXML($arg->getSource())));
}function xpath(string $arg) {
return new DomXPath($this->loadXML($arg));
}
function xpathFromDom($arg) {
return new DomXPath($arg);
}
function xpathFromTree($arg) {
return new DomXPath($this->loadXML($arg->getSource())));
}
function xpathFromString($arg) {
return new DomXPath($this->loadXML($arg));
}
Works perfectly well. And you don't even need to document them because
the names speak for themselves. A much simpler (and clearer) solution IMHO.
If you want an OO way I'd prefer something like
$xpath = $obj->getXPath();
to
$xpath = $this->xpath($obj);
anyway. This doesn't work with basic types like strings but having a
special case there is not a problem IMHO as using those two
interchangeably will lead to other problems anyway.
- Chris
And if you have more than one parameter you will name it methodFromStringIntegerSampleClassBoolean ?!?
And how would you do the same for constructors ?!? Create a initWithStringIntegerSampleClassBoolean method which has to be called after object creation ?!?
-----Ursprüngliche Nachricht-----
Von: Christian Schneider [mailto:cschneid@cschneid.com]
Gesendet: Di 16.10.2007 11:45
An: Hans Moog
Cc: internals@lists.php.net
Betreff: Re: AW: [PHP-DEV] Method overloading by method signature
Hans Moog wrote:
When it would be:
==
function xpath(DomDocument $arg) {
return new DomXPath($arg);
}function xpath(XmlTree $arg) {
return new DomXPath($this->loadXML($arg->getSource())));
}function xpath(string $arg) {
return new DomXPath($this->loadXML($arg));
}
function xpathFromDom($arg) {
return new DomXPath($arg);
}
function xpathFromTree($arg) {
return new DomXPath($this->loadXML($arg->getSource())));
}
function xpathFromString($arg) {
return new DomXPath($this->loadXML($arg));
}
Works perfectly well. And you don't even need to document them because
the names speak for themselves. A much simpler (and clearer) solution IMHO.
If you want an OO way I'd prefer something like
$xpath = $obj->getXPath();
to
$xpath = $this->xpath($obj);
anyway. This doesn't work with basic types like strings but having a
special case there is not a problem IMHO as using those two
interchangeably will lead to other problems anyway.
- Chris
Hans Moog wrote:
And if you have more than one parameter you will name it
methodFromStringIntegerSampleClassBoolean ?!?
No, I would rethink my interface.
And how would you do the same for constructors ?!? Create a
initWithStringIntegerSampleClassBoolean method which has to be called
after object creation ?!?
No, I would either use factory methods or the OO approach I outlined in
my previous message.
But let's agree that we fundamentally disagree on style. I think you're
using the wrong language but that's just me....
Over and out,
- Chris
And if you have more than one parameter you will name it
methodFromStringIntegerSampleClassBoolean ?!?No, I would rethink my interface.
Sometimes you need more than one parameter and even rethinking wouldn't "solve" this requirement.
And how would you do the same for constructors ?!? Create a
initWithStringIntegerSampleClassBoolean method which has to be called
after object creation ?!?No, I would either use factory methods or the OO approach I outlined in
my previous message.
O_O really?!? You would use factory methods just to allow more than one parameter to be passed to the constructor? Okayyyy.....
But let's agree that we fundamentally disagree on style.
Yup :)
I think you're using the wrong language but that's just me....
Over and out,
- Chris
I agree. But PHP (until PHP 5.2.x) was the wrong language for everyone who wanted to use namespaces, too.
But a programming language is able to evolve and sometimes new features are really usefull and should be included. And in this special case the new feature would not harm anyone because it would be fully backward compatible and in my humble opinion it would push php to an enterprise level when it comes to object orientation.
Btw: I really LOVE PHP and the way it handles things. You are able to develop applications VERY fast but when it comes to big applications it is sometimes better to be more strict and structure things a little bit different, especially when you have to mess around with 3rdparty developers.
I agree. But PHP (until PHP 5.2.x) was the wrong language for
everyone who wanted to use namespaces, too.
But a programming language is able to evolve and sometimes new
features are really usefull and should be included. And in this
special case the new feature would not harm anyone because it would
be fully backward compatible and in my humble opinion it would push
php to an enterprise level when it comes to object orientation.
The point is that I do not see this feature at all relevant to
solving the web problem. This is where PHP needs to focus. Namespaces
help in solving the web problem, because it eases cooperation of
independent developers to supply libraries. The feature you are
proposing is solved easily in userland (just like named parameters).
Its is a feature I could see using in a heavy lifting language, but
not in a glue language. Where do we draw the line? Somewhere
relatively arbitrary, but my gut tells me that this is a good
candidate to be on "lets leave it out of php" side of the line.
regards,
Lukas
The point is that I do not see this feature at all relevant to
solving the web problem. This is where PHP needs to focus. Namespaces
help in solving the web problem, because it eases cooperation of
independent developers to supply libraries. The feature you are
proposing is solved easily in userland (just like named parameters).
Its is a feature I could see using in a heavy lifting language, but
not in a glue language. Where do we draw the line? Somewhere
relatively arbitrary, but my gut tells me that this is a good
candidate to be on "lets leave it out of php" side of the line.regards,
Lukas
When it comes to interoperation between systems and or developers, it is always a
good idea to define strict standards of how the communication between libarys and components
has to take place (since public methods are something like an interface bewteen interoperating libraries and classes),
to be sure that all components work together as expected. One of the best approaches to
explain the usage of the class to a foreign developer and to be sure that your class is used
correctly and help the foreign developer to understand how your classes have to be used
(beside documentation), is to typehint your parameters.
But when you do so, you are no longer able to overload your methods anymore. Because of this, I think
this feature is EXTREMELY relevant to solving the web problem. Not being able to overload methods anymore
is a bad thing since overloaded methods allow to use a class in much more situations than just being able to use it
in one single way.
You are not able to tell the developer to use integer OR string values but NO arrays, booleans, Objects
and so on when calling your method.
You could mention it in the comment but when he misunderstands the comment, php doesn't notify him about
his error. When using type hinted parameters, php would do so.
You are not able to provide strict api's out of the box right now. You will always have to check the types of the
passed parameters manually.
When it comes to interoperation between systems and or developers,
it is always a
good idea to define strict standards of how the communication
between libarys and components
has to take place (since public methods are something like an
interface bewteen interoperating libraries and classes),
to be sure that all components work together as expected. One of
the best approaches to
explain the usage of the class to a foreign developer and to be
sure that your class is used
correctly and help the foreign developer to understand how your
classes have to be used
(beside documentation), is to typehint your parameters.
I do not agree that its always a good idea to make your communication
strict when you are using a type less language. Are you implying that
for some reason typeless-ness only becomes relevant in your user land
code? I do agree that libraries tend to be a bit more "static" and
therefore less in need of quickness in development, but I do not see
this validating your core assumption that strictness is a good thing
for libraries. PHP has always tended more towards keeping running
when at all possible rather than throwing fatal errors. The advent of
exceptions has changed part of this game already, but I do not think
that the solution is to start turning every situation the developer
hasnt thought off into fatal errors. Sometimes barely working is
better than not working and I think this is a mantra on the web.
But when you do so, you are no longer able to overload your methods
anymore. Because of this, I think
this feature is EXTREMELY relevant to solving the web problem. Not
being able to overload methods anymore
is a bad thing since overloaded methods allow to use a class in
much more situations than just being able to use it
in one single way.You are not able to tell the developer to use integer OR string
values but NO arrays, booleans, Objects
and so on when calling your method.You could mention it in the comment but when he misunderstands the
comment, php doesn't notify him about
his error. When using type hinted parameters, php would do so.You are not able to provide strict api's out of the box right now.
You will always have to check the types of the
passed parameters manually.
Your list of if's is quote long, which illustrates what kind of niche
problem you are solving. The bulk of all libraries can live quite
well without type hints. If you choose to use them, then accept their
limitations and add manual overloading via separate functions that
cover all reasonable permutations. Or implement this overloading the
same way other people handle the lack of named parameters. The
addition of type hinting was questionable enough, that they hardly
warrant yet another addition just to make them even more useful in a
niche case.
regards,
Lukas
Hi,
function xpath(DomDocument $arg) {
return new DomXPath($arg);
}function xpath(XmlTree $arg) {
return new DomXPath($this->loadXML($arg->getSource())));
}function xpath($arg) { // Untyped = default
if (!is_string($arg)) {
throw new IllegalArgumentException('Unsupported argument type');
}
return new DomXPath($this->loadXML($arg));
}==
If we consider the readability argument only: Which one of the above more
readable? You decide:)
Allowing an untyped version next to a typed one is imo the worst thing
one can do: f I want to read the code and first see the untyped
implementation I certainly go crazy while trying to debug the code.
johannes
Marcus Boerger wrote:
If you have such a patch you should definitively post it here so that we
can hve a look. Most interesting to us is however the oerformance impact. As
that was the main reason to go any further than adding return type hints.
Am I the only here who thinks that performance is not the major issue
with this approach?
Method signatures lead to a different style of programming I personally
wouldn't want to encourage in PHP.
I'm expecting some kind of "if you don't like it don't use it" answer
but I wouldn't want to bloat the language for such a feature anyway.
Example:
It's too easy for someone to think it's a good idea to change
function foo($x) { ... }
to something like
function foo(string $x) { ... }
function foo(int $x) { ... }
but this can lead to very subtle bugs as automatic type conversion can
trick you into passing something different than you thought and hence
could lead you to do
foo(strval($x));
all over the place to not get any surprises. A Bad Thing(TM) IMHO.
Regards,
- Chris
Christian Schneider wrote:
Marcus Boerger wrote:
If you have such a patch you should definitively post it here so
that we
can hve a look. Most interesting to us is however the oerformance
impact. As
that was the main reason to go any further than adding return type hints.Am I the only here who thinks that performance is not the major issue
with this approach?Method signatures lead to a different style of programming I personally
wouldn't want to encourage in PHP.I'm expecting some kind of "if you don't like it don't use it" answer
but I wouldn't want to bloat the language for such a feature anyway.Example:
It's too easy for someone to think it's a good idea to change
function foo($x) { ... }
to something like
function foo(string $x) { ... }
function foo(int $x) { ... }
but this can lead to very subtle bugs as automatic type conversion can
trick you into passing something different than you thought and hence
could lead you to do
foo(strval($x));
all over the place to not get any surprises. A Bad Thing(TM) IMHO.
Yup, I agree. Having to sit and count arguments and figure out the
types in order to determine which actual method is being called makes it
damn near impossible to debug code as far as I am concerned. And what
does the callgraph from a profiler look like? kcachegrind doesn't show
the function signature in the callgraph which means we would have to map
these methods to some unique name and people would have to know how to
map these names back to the correct function signature. Sounds like a
mess to me.
-Rasmus
Kcachegrind doesn't show the function signature in the callgraph because the parameter signature is not part of the function signature. If the parameter siganture would be moved into the function signature, kcachegrind would adept and show it.
Btw: You don't have to use it if you don't want to. But PHP5 didn't include type hints for no reason. The problem: Type hints don't make many sense right now.
Like I already wrote in another comment. If you would use type hints you would understand the need of overloading type hinted methods.
-----Ursprüngliche Nachricht-----
Von: Rasmus Lerdorf [mailto:rasmus@lerdorf.com]
Gesendet: Sonntag, 14. Oktober 2007 16:50
An: Christian Schneider
Cc: Marcus Boerger; internals@lists.php.net
Betreff: Re: AW: [PHP-DEV] Method overloading by method signature
Christian Schneider wrote:
Marcus Boerger wrote:
If you have such a patch you should definitively post it here so
that we
can hve a look. Most interesting to us is however the oerformance
impact. As
that was the main reason to go any further than adding return type hints.Am I the only here who thinks that performance is not the major issue
with this approach?Method signatures lead to a different style of programming I personally
wouldn't want to encourage in PHP.I'm expecting some kind of "if you don't like it don't use it" answer
but I wouldn't want to bloat the language for such a feature anyway.Example:
It's too easy for someone to think it's a good idea to change
function foo($x) { ... }
to something like
function foo(string $x) { ... }
function foo(int $x) { ... }
but this can lead to very subtle bugs as automatic type conversion can
trick you into passing something different than you thought and hence
could lead you to do
foo(strval($x));
all over the place to not get any surprises. A Bad Thing(TM) IMHO.
Yup, I agree. Having to sit and count arguments and figure out the
types in order to determine which actual method is being called makes it
damn near impossible to debug code as far as I am concerned. And what
does the callgraph from a profiler look like? kcachegrind doesn't show
the function signature in the callgraph which means we would have to map
these methods to some unique name and people would have to know how to
map these names back to the correct function signature. Sounds like a
mess to me.
-Rasmus
Hans Moog wrote:
Kcachegrind doesn't show the function signature in the callgraph because the parameter signature is not part of the function signature. If the parameter siganture would be moved into the function signature, kcachegrind would adept and show it.
Btw: You don't have to use it if you don't want to. But PHP5 didn't include type hints for no reason. The problem: Type hints don't make many sense right now.
Like I already wrote in another comment. If you would use type hints you would understand the need of overloading type hinted methods.
Right, but I am usually profiling and debugging other peoples' code, not
my own, so that argument doesn't work.
There are plenty of strictly typed languages in the world, we don't need
another. The Web is inherently untyped in that browsers don't type
anything. We have to intelligently deal with untyped data all the time
and do the appropriate type juggling.
-Rasmus
You are missing something. Using this new feature would be voluntarily (it is optional like type hints are already).
If you want to code the old way and you don't want to force coders to use your functions correctly, you could leave out typehints an check the parameters manually.
But if you want to write strict API's, that accept only a special type of parameters (and type hinted parameters were introduced to be able to do this) you would still be able to accept more than one type of parameters (without this additional feature you are not able to do it right now).
You do not use type hints in your functions, thats why you don't understand the need of overloding methods by parameter siganture. If you would understand the need of type hints for robust applications you would understand the need of overloading typehinted methods.
Btw: You could call foo((integer) $x); to ensure the right method is called.
Like I already said, this feature is only require for people writing complex web applications, offering api's to 3rdparty coders that are very strict. If you are coding a project within your company you can rely on the fact, that programmers should know how to use your methods, but a 3rdparty coder who extends the functionality of your product doesn't know and there should be no chance to crash the application.
-----Ursprüngliche Nachricht-----
Von: Christian Schneider [mailto:cschneid@cschneid.com]
Gesendet: Sonntag, 14. Oktober 2007 14:27
An: Marcus Boerger
Cc: internals@lists.php.net
Betreff: Re: AW: [PHP-DEV] Method overloading by method signature
Marcus Boerger wrote:
If you have such a patch you should definitively post it here so that we
can hve a look. Most interesting to us is however the oerformance impact. As
that was the main reason to go any further than adding return type hints.
Am I the only here who thinks that performance is not the major issue
with this approach?
Method signatures lead to a different style of programming I personally
wouldn't want to encourage in PHP.
I'm expecting some kind of "if you don't like it don't use it" answer
but I wouldn't want to bloat the language for such a feature anyway.
Example:
It's too easy for someone to think it's a good idea to change
function foo($x) { ... }
to something like
function foo(string $x) { ... }
function foo(int $x) { ... }
but this can lead to very subtle bugs as automatic type conversion can
trick you into passing something different than you thought and hence
could lead you to do
foo(strval($x));
all over the place to not get any surprises. A Bad Thing(TM) IMHO.
Regards,
- Chris
Hans Moog wrote:
You are missing something. Using this new feature would be
voluntarily (it is optional like type hints are already).
Thanks for bringing that up right after I told you that I won't accept
this argument ;-)
If you want to code the old way and you don't want to force coders to
use your functions correctly, you could leave out typehints an check
the parameters manually.
We don't check the parameters. Automatic type conversion does the right
thing for us in 99% of the case and the other 1% is handled manually.
Not checked, handled. Please note the difference.
You do not use type hints in your functions, thats why you don't
understand the need of overloding methods by parameter siganture. If
you would understand the need of type hints for robust applications
you would understand the need of overloading typehinted methods.
I disagree here:
- Type hints are not needed for robust applications. That's a myth.
They don't even make it easier IMHO. - Overloading is not necessary, you can use different names if you want
different flavours of a method. That's clearer and easier to debug (I
can search in the source code for a function name but not for a
parameter signature).
Btw: You could call foo((integer) $x); to ensure the right method is
called.
That's exactly what I want to avoid. Your code ends up being messy for
no good reason: Solving a problem you created yourself.
Like I already said, this feature is only require for people writing
complex web applications, offering api's to 3rdparty coders that are
very strict. If you are coding a project within your company you can
rely on the fact, that programmers should know how to use your
methods, but a 3rdparty coder who extends the functionality of your
product doesn't know and there should be no chance to crash the
application.
I don't think we would agree on what's a complex web application here
(complex being a negative term for me, I'd rather write a simple but
advanced web application) but even then the API should be simple. And
simple APIs don't need whips and chains.
And I don't believe in fool proof APIs, only fools can use those ;-)
- Chris
You are missing something. Using this new feature would be voluntarily (it is optional like type hints are already).
If you want to code the old way and you don't want to force coders to use your functions correctly, you could leave out typehints an check the parameters manually.
But if you want to write strict API's, that accept only a special type of parameters (and type hinted parameters were introduced to be able to do this) you would still be able to accept more than one type of parameters (without this additional feature you are not able to do it right now).
You do not use type hints in your functions, thats why you don't understand the need of overloding methods by parameter siganture. If you would understand the need of type hints for robust applications you would understand the need of overloading typehinted methods.
Btw: You could call foo((integer) $x); to ensure the right method is called.
Like I already said, this feature is only require for people writing complex web applications, offering api's to 3rdparty coders that are very strict. If you are coding a project within your company you can rely on the fact, that programmers should know how to use your methods, but a 3rdparty coder who extends the functionality of your product doesn't know and there should be no chance to crash the application.
For what it is worth, I would find this functionality EXTREMELY useful.
The classes I create ARE used by other developers both within my
organisation and outside and as I'm currently porting GUI apps from
Delphi to web apps using PHP and missing things like type hinting of
scalars AND overloaded function defs is a pain as I now have 1 method
with a header which has to parse the parameter types to determine what
is required to be called.
I think this is a GREAT feature. Even if it was JUST for E_STRICT
OOP code.
--
Richard Quadling
Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731
"Standing on the shoulders of some very clever giants!"
Will method overloading by method signature be implemented in php6 or
even php 5.3?
this feature isnt php .. or rather its a solution to a problem that
does not exist in php .. for good reason.
regards,
Lukas