carlwenrich Posted August 25, 2008 Share Posted August 25, 2008 I know I can use a class to make a struct, but how do I make a union? (I'm trying to update some old c code). Quote Link to comment https://forums.phpfreaks.com/topic/121308-how-do-i-emulate-a-c-union/ Share on other sites More sharing options...
DarkWater Posted August 25, 2008 Share Posted August 25, 2008 What does this have to do with PHP...? Quote Link to comment https://forums.phpfreaks.com/topic/121308-how-do-i-emulate-a-c-union/#findComment-625421 Share on other sites More sharing options...
carlwenrich Posted August 25, 2008 Author Share Posted August 25, 2008 Like I said, I'm trying to update (convert to php) some old c code. Quote Link to comment https://forums.phpfreaks.com/topic/121308-how-do-i-emulate-a-c-union/#findComment-625427 Share on other sites More sharing options...
DarkWater Posted August 25, 2008 Share Posted August 25, 2008 You didn't say "convert to PHP" in your first post. Can I see the C code? There's no such thing as a union in PHP because it's loosely typed, so the type of the variable doesn't matter, and it'll occupy the same location in memory even if you change types. >_> Quote Link to comment https://forums.phpfreaks.com/topic/121308-how-do-i-emulate-a-c-union/#findComment-625431 Share on other sites More sharing options...
carlwenrich Posted August 25, 2008 Author Share Posted August 25, 2008 Sorry, I thought it would be obvious that I'm converting to php, since this is a php forum... my bad. Here's the code I need to convert: typedef struct { char from; char to; char promote; char bits; } move_bytes; typedef union { move_bytes b; int u; } move; Quote Link to comment https://forums.phpfreaks.com/topic/121308-how-do-i-emulate-a-c-union/#findComment-625439 Share on other sites More sharing options...
DarkWater Posted August 25, 2008 Share Posted August 25, 2008 What is the whole code supposed to be used for? Quote Link to comment https://forums.phpfreaks.com/topic/121308-how-do-i-emulate-a-c-union/#findComment-625443 Share on other sites More sharing options...
carlwenrich Posted August 25, 2008 Author Share Posted August 25, 2008 comparing chess moves Quote Link to comment https://forums.phpfreaks.com/topic/121308-how-do-i-emulate-a-c-union/#findComment-625444 Share on other sites More sharing options...
DarkWater Posted August 25, 2008 Share Posted August 25, 2008 Edit: Nvm, didn't work. >_< Brb, dinner. Quote Link to comment https://forums.phpfreaks.com/topic/121308-how-do-i-emulate-a-c-union/#findComment-625447 Share on other sites More sharing options...
DarkWater Posted August 25, 2008 Share Posted August 25, 2008 Okay. I tried to write up a basic union class, and it seems to work for me. For the types though, you need to use the type that gettype() returns, and not the actual type. Ex: int would really be integer in the class constructor. Here: <?php class union { protected $data; protected $types; public function __construct(array $types) { $this->types = $types; } public function access($name) { if ($this->data['name'] == $name) { return $this->data['value']; } else { return null; } } public function set($name, $val) { $type = $this->_type($val); if (in_array($name, array_keys($this->types)) && ($this->types[$name] == $type || (gettype($var) == 'object' && $var instanceof $this->types[$name]))) { $this->data['name'] = $name; $this->data['value'] = $val; $this->data['type'] = $type; return true; } else { return false; } } private function _type($var) { $type = gettype($var); if ($type == 'object') { $type = get_class($var); } return $type; } } class foo{public function __toString() { return "Foo!"; }} class bar{ } $u = new union(array('test' => 'foo')); if ($u->set('test', new foo())) { echo $u->access('test'); } else { echo "Failed"; } echo "\n"; ?> Try it. >_< Quote Link to comment https://forums.phpfreaks.com/topic/121308-how-do-i-emulate-a-c-union/#findComment-625482 Share on other sites More sharing options...
Barand Posted August 26, 2008 Share Posted August 26, 2008 have a look at these www.php.net/pack www.php.net/unpack <?php $i = pack('c*', 0x41,0x42,0x43,0x44); echo '<br>'; $a = unpack('c*', "$i"); // unpack as char echo '<pre>', print_r($a, true), '</pre>'; $b = unpack('N', $i); // unpack as int echo '<pre>', print_r($b, true), '</pre>'; ?> --> Array ( [1] => 65 [2] => 66 [3] => 67 [4] => 68 ) Array ( [1] => 1094861636 ) Quote Link to comment https://forums.phpfreaks.com/topic/121308-how-do-i-emulate-a-c-union/#findComment-625514 Share on other sites More sharing options...
carlwenrich Posted August 26, 2008 Author Share Posted August 26, 2008 Thanks a lot. Quote Link to comment https://forums.phpfreaks.com/topic/121308-how-do-i-emulate-a-c-union/#findComment-625517 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.