9

My Thrift service expects to receive a Long integer representing a timestamp in milliseconds, but coming from PHP, I know PHP thrift is supposed to automagically turn my PHP types into thrift types, but which PHP type does it expect for Long integers? I think my computer is 64-bit, but since I think that PHP integers' length is platform dependent, I don't really want to depend upon a platform-dependent length for my integers.

I am currently grabbing microtime() and multiplying by 1000, then converting to integer. Is this the "correct" way to work with PHP & thrift long ints?

nnythm
  • 3,280
  • 4
  • 26
  • 36
  • Please refer to this: http://stackoverflow.com/questions/864058/how-to-have-64-bit-integer-on-php – Frank He Jul 12 '12 at 13:54
  • Not exactly my question. Thrift magically converts types to thrift types, but I don't know which types it is willing to convert. As I said, I don't really want to rely upon hoping I have 64-bit architecture. – nnythm Jul 13 '12 at 04:35
  • Why does it have to be an integer? What's wrong with strings? How are you communicating with thrift? Over HTTP or is there some PHP extension that lets you talk to it via some other protocol (I don't know what Thrift is, google yields Apache Thrift)? I don't see a reason to force a type within PHP if all you have to do is pass a number that PHP hasn't got anything to do with in sense that it does calculations with it. – N.B. Jul 17 '12 at 15:46
  • Yes, apache thrift [http://thrift.apache.org/] The work I'm doing needs long integers for a few reasons--one of them is that we get probabilistic uniqueness by generating a random long, which is much less likely to be unique with smaller numbers. Long story short, I need to have long integers in php. Furthermore, the interface is defined by someone else, so I cannot just use strings. – nnythm Jul 17 '12 at 15:49

2 Answers2

7

You are right,

The size of an integer is platform-dependent, although a maximum value of about two billion is the usual value (that's 32 bits signed). 64-bit platforms usually have a maximum value of about 9E18. PHP does not support unsigned integers. Integer size can be determined using the constant PHP_INT_SIZE, and maximum value using the constant PHP_INT_MAX since PHP 4.4.0 and PHP 5.0.5.

http://www.php.net/manual/en/language.types.integer.php

If you use microtime() you need not to divide it by 1000. Its float, you may want to multiply it by 1000.

You may use BC Math for calclulate it as numbers, using string types. I guess string are OK to communicate with any other thing.

In case of multiplying by 1000, you even not need BCMath. Just delete comma from string representation of microtime(true) (or space from microtime)

RiaD
  • 46,822
  • 11
  • 79
  • 123
  • good call, I was multiplying by 1000, I've changed my question to reflect this. – nnythm Jul 17 '12 at 15:51
  • I would like to give you the bounty, but your answer doesn't seem to address the part of my question that concerns Thrift. Do you know anything about how php thrift expects long integers to work? – nnythm Jul 22 '12 at 20:10
  • @nnythm, sorry, but I can't add anything – RiaD Jul 23 '12 at 12:19
1

Maybe you should use id of string type as like twitter: https://dev.twitter.com/docs/twitter-ids-json-and-snowflake

Frank He
  • 536
  • 3
  • 9
  • I am hooking into a twitter service that uses thrift longs. I could change it, but then I would also have to change all of the twitter code that hooks into it, which would be a pain. – nnythm Jul 14 '12 at 21:55