Don't want to take up much of you're time, just wondered if anybody
could point me to the reason why some primitives aren't in php.
Would find it very very useful to have byte, short, long, float, double
and char in php. (primarily byte and char).
while I'm here I may as well also ask about further adding type hinting
for the existing scalars and array.
and finally different method signatures such as:
class Whatever {
public function __construct(Bar b);
public function __construct(Foo f);
public function doSomething(Bar b);
public function doSomething(Foo f);
}
Regards!
Hi,
I think the reason there aren't more primitive types in PHP is because of
the nature of the language. One of the main features of PHP over say, C (and
even Java), is that the memory managment is completely transparent to the
devloper. This means that it really shouldent matter to the devloper how an
integer is implimented on the machine. Same with doubles and chars.
Granted, I can see some reasons why forcing the data to be of a specific
length or type would be handy, but I dont know if there would be a wide
enough requierment for this to actually impliment it as a language feature.
Plus, you usually can find a way around it anyway :). But basically, if your
interest in byte and char are for conserving memory then dont be worried...
PHP does a fairly good job with your memory and this wouldent actually
translate into a memory savings on the machine.
I too would love to see more type hinting :).
~Graham Kelly
Don't want to take up much of you're time, just wondered if anybody could
point me to the reason why some primitives aren't in php.Would find it very very useful to have byte, short, long, float, double and
char in php. (primarily byte and char).while I'm here I may as well also ask about further adding type hinting for
the existing scalars and array.and finally different method signatures such as:
class Whatever {
public function __construct(Bar b);
public function __construct(Foo f);
public function doSomething(Bar b);
public function doSomething(Foo f);
}Regards!
Graham Kelly wrote:
Hi,
I think the reason there aren't more primitive types in PHP is because of
the nature of the language. One of the main features of PHP over say, C (and
even Java), is that the memory managment is completely transparent to the
devloper. This means that it really shouldent matter to the devloper how an
integer is implimented on the machine. Same with doubles and chars.
Granted, I can see some reasons why forcing the data to be of a specific
length or type would be handy, but I dont know if there would be a wide
enough requierment for this to actually impliment it as a language feature.
Plus, you usually can find a way around it anyway :). But basically, if your
interest in byte and char are for conserving memory then dont be worried...
PHP does a fairly good job with your memory and this wouldent actually
translate into a memory savings on the machine.
mainly it was for the strictness and for the extra speed gained by
having a primitive at php level, to implement using say class Byte
(which I have done) is seriously heavy processor and memory wise for
something so simple.. especially if you consider an implementation of
ByteArray..
obviously there isn't much call otherwise this would have been
implemented back at v4 or at least 5; however I for one would love to
see this simple little bit of support added.
Would open up masses of avenues, including the ability to make some
decent orm's for php (much easier to map "byte to byte" than "scalar
might be a byte but is really an integer to byte" if you know what I mean.
regards!
Nathan Rixham wrote:
[...]
while I'm here I may as well also ask about further adding type
hinting for the existing scalars and array.
+1, but I don't know what might have stopped it being implemented before
(time, parsing API changes, etc), so it would be interesting to look
into the history.
and finally different method signatures such as:
class Whatever {
public function __construct(Bar b);
public function __construct(Foo f);
public function doSomething(Bar b);
public function doSomething(Foo f);
}
+1. Having overloaded function definitions like this would be very
useful, but I don't know how awkward it would be to implement - I don't
know how the function pointers are currently stored. I could imagine
adding information about the type signature to the internal method name,
and looking up based on that... but then I don't know how it's stored so
I might be talking rubbish!
Also, what about this case:
class MyTestClass {
public function blah(Foo $f);
public function blah(Bar $b);
public function blah($v);
}
I would argue that the most specific function should be called, but how
costly would that be to determine? What if you have a "Baz" subclass of
Bar, but no corresponding method? What if Bar itself is a subclass of Foo?
Dave
2008/12/18 Dave Ingram dave@dmi.me.uk
Also, what about this case:
class MyTestClass {
public function blah(Foo $f);
public function blah(Bar $b);
public function blah($v);
}I would argue that the most specific function should be called, but how
costly would that be to determine? What if you have a "Baz" subclass of
Bar, but no corresponding method? What if Bar itself is a subclass of Foo?
Ideally a most specific first approach, failing that I'd suggest taking
doing it the nearest first way (like we do when catching exceptions) ?
2008/12/18 Dave Ingram dave@dmi.me.uk
Nathan Rixham wrote:
[...]
while I'm here I may as well also ask about further adding type
hinting for the existing scalars and array.
+1, but I don't know what might have stopped it being implemented before
(time, parsing API changes, etc), so it would be interesting to look
into the history.
This is simple to achieve with
assert('is_string($argument)');
class MyTestClass {
public function blah(Foo $f);
public function blah(Bar $b);
public function blah($v);
}I would argue that the most specific function should be called, but how
costly would that be to determine? What if you have a "Baz" subclass of
Bar, but no corresponding method? What if Bar itself is a subclass of Foo?
I remember that multiple signatures was said to have a possible very
difficult implementation. However, a similar behaviour can be achieved by
some instanceof().
--
Giorgio Sironi
Piccolo Principe & Ossigeno Scripter
http://www.sourceforge.net/projects/ossigeno
I remember that multiple signatures was said to have a possible very
difficult implementation. However, a similar behaviour can be achieved by
some instanceof().
I thought it probably would be awkward, but we do already have some type
hinting that can also be accomplished with instanceof() -- this is just
an extension of that idea. Although it's not my idea as such!
Dave
Dave Ingram wrote:
I remember that multiple signatures was said to have a possible very
difficult implementation. However, a similar behaviour can be achieved by
some instanceof().I thought it probably would be awkward, but we do already have some type
hinting that can also be accomplished with instanceof() -- this is just
an extension of that idea. Although it's not my idea as such!
instance of is handy however it isn't the cleanest; consider:
public static function parseByte( $var )
{
$testPassed = false;
if( $var instanceof Number ) {
$var = $var->byteValue();
$testPassed = true;
} if( $var instanceof String ) {
$var = $var->__toString();
}
if( !$testPassed && is_numeric( $var ) ) {
$var = 0 + $var;
$testPassed = true;
}
if( $testPassed && Byte::MIN_VALUE <= $var && Byte::MAX_VALUE >=
$var ) {
return $var;
}
throw new NumberFormatException();
}
could be:
public static function parseByte( Number $var )
{
return $var->byteValue();
}
public static function parseByte( String $var )
{
if( is_numeric( $var = $var->__toString() ) ) {
$var = 0 + $var;
if( Byte::MIN_VALUE <= $var && Byte::MAX_VALUE >= $var ) {
return $var;
}
}
throw new NumberFormatException();
}
mini-use-case but shows how useful it would be..
Dave Ingram escribió:
class MyTestClass {
public function blah(Foo $f);
public function blah(Bar $b);
public function blah($v);
}
Looks like you are using the wrong language, you need JAVA instead.
--
"We have art in order not to die of the truth" - Friedrich Nietzsche
Cristian Rodríguez R.
Platform/OpenSUSE - Core Services
SUSE LINUX Products GmbH
Research & Development
http://www.opensuse.org/