FRIHOSTFORUMSSEARCHFAQTOSBLOGSCOMPETITIONS
You are invited to Log in or Register a free Frihost Account!


[PHP] Some beginner questions





davidv
So I just started using PHP and I wanted to re-create some programs I've made in other languages to get me started. I just have a few questions, let's get right into it Very Happy

1. Because PHP is dynamic typed, is it uncommon to use generics? Example?

2. Does PHP have APIs? I've been on PHP.net a couple times and the source is pretty helpful but all I sometimes want to see is API -> ADT. Are there any online resources like that?

Thanks.
Peterssidan
davidv wrote:
1. Because PHP is dynamic typed, is it uncommon to use generics? Example?
I don't think PHP has a need for generics. You can store any type of object in an array and call functions on it.

davidv wrote:
2. Does PHP have APIs? I've been on PHP.net a couple times and the source is pretty helpful but all I sometimes want to see is API -> ADT. Are there any online resources like that?
Yes of course PHP has APIs. SPL has some datastructures. I have never used SPL and I'm not sure Frihost support all of it. The build in PHP array is actually an ordered map so you can do a lot of thing with it.
davidv
Oh wow PHP's array seems a lot more powerful than Java's. If you don't mind me asking, if it's an ordered map and I'm able to treat it like a hashtable, does it have the same expected running time as a hash table O(1)? Is there an underlying implementation that PHP has provided us which allows us to treat it as a hash table over a map? Or is it just saying we can treat it "like" a dictionary or hash table but it's just an ordered map with O(n) time complexity?

Quote:
An array in PHP is actually an ordered map. A map is a type that associates values to keys. This type is optimized for several different uses; it can be treated as an array, list (vector), hash table (an implementation of a map), dictionary, collection, stack, queue, and probably more. As array values can be other arrays, trees and multidimensional arrays are also possible.


Also, if someone could tell me if Frihost supports SPL, that would be awesome.

Thanks.
Peterssidan
More powerful but most likely slower than Java arrays. PHP arrays are implemented as hash tables. I don't know what you mean by "expected running time". The complexity depends on the operations.
davidv
PHP.net wrote:
An array in PHP is actually an ordered map. A map is a type that associates values to keys. This type is optimized for several different uses; it can be treated as an array, list (vector), hash table (an implementation of a map), dictionary, collection, stack, queue, and probably more. As array values can be other arrays, trees and multidimensional arrays are also possible.


Quote:
More powerful but most likely slower than Java arrays.


You’re probably right if it’s just a map but I don't know anything about PHP data structures.

Quote:
PHP arrays are implemented as hash tables.


According to the website, it's an ordered map not a hash table. This means the insertion and deletion of a <K,V> pair does not have an average-case running time of O(1) unless you're adding at the end/front and of course this also depends on the map implementation.

Maps and Hash tables are two different types of data structures... as I recall, Hash tables extend Maps. They have bucket arrays and hash functions, maps do not.

Quote:
I don't know what you mean by "expected running time".


The complexity of an algorithm. How long we expect it to finish.

Quote:
The complexity depends on the operations.


Obviously. However, all hash table operations have an expected running time of O(1). My question is... if we're able to treat the PHP array (which is an ordered map) like a hash table, do I have some kind explicit way of telling PHP to treat it as a hash table and inherit the time complexity of a hash table rather a map OR is it just saying we treat it "like" a hash table but it's not a hash table.
Peterssidan
Map is an abstract name that doesn't say anything about how it is implemented. With a map you have keys that maps to some value. If you have the key you can lookup the value. One way to implement a map is to use a self-balancing binary search tree. Another way is to use a hash table. Even simple arrays like in C++ or Java can be seen as a map with integer keys.

PHP array uses a hash table. I think it need to do some extra work to keep the order.

The keys can only be integers or strings. I forgot to mention that before.
davidv
Peterssidan wrote:
Map is an abstract name that doesn't say anything about how it is implemented. With a map you have keys that maps to some value. If you have the key you can lookup the value. One way to implement a map is to use a self-balancing binary search tree. Another way is to use a hash table. Even simple arrays like in C++ or Java can be seen as a map with integer keys.

PHP array uses a hash table. I think it need to do some extra work to keep the order.

The keys can only be integers or strings. I forgot to mention that before.


You're right! After a bit of thinking and reading documentation online, Map is indeed just an abstract. I was thinking about the linked list implementation of a map. Must of got confused with something else (hence the linear time). You're also right that PHP's array is implemented using a hash table... very interesting.

There's so much more to PHP than what I initially thought. Thanks for the responses Wink
Fire Boar
The next question is, are PHP arrays constant time? They are implemented as a hash table, yes, but there must be more to it than that for ordering to make sense.
davidv
That's a good question, maybe they're fully associative arrays. I'll need to do some research!
Related topics
PHP Resources.
simple php problem, please help...
PHP Validation Class
Ashens Web Design Questions Thread
Embarrassing newbie php/SQL questions
Hi, I'm cheh...
PHP Image Questions
about guitar....
I'm PHP beginner. looking for a PHP editor...
PHP beginner help!!!
AJAX + PHP, UPDATE mysql table nto working.
Help With Features & Questions. :)
[Completed]500 Frih$ for simple flash banner
Redirect from one domain to another domain
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.