PHP Arrays

13
PHP Arrays. The Good, the Bad and the Ugly. Mahesh Gattani

description

The good, the bad and the ugly of the PHP Arrays. Lets try to fix it some, shall we?

Transcript of PHP Arrays

Page 1: PHP Arrays

PHP Arrays.The Good, the Bad and the Ugly.

Mahesh Gattani

Page 2: PHP Arrays

About Me

● Senior Software Engineer at Lucid Software Inc.● Bachelors degree from IIT Delhi, India in Computer Science.● Interests:

○ Sports, not the adventure types. Cricket anyone?○ Long Drives○ Reading

● Follow me on○ methinking.tumblr.com

Page 3: PHP Arrays

What are arrays?

Page 4: PHP Arrays

PHP arrays: The good.

● Exceptionally easy to use.● No need to allocate memory.● Indexed and associative at the same time.● Different types of keys allows in the same instance.● Ordered.● ...

PHP arrays are not arrays...They are... Ordered Hash Maps

Page 5: PHP Arrays

PHP arrays: The bad

PHP arrays are bad because…

● Runtime○ O(n*n) worst case

■ Why you ask? Because they are maps. More about this later.○ Rehashing.

● Memory Usage○ Whole different beast.

Page 6: PHP Arrays

UGLY!

Weird, huh!?

Size: 8?Next Free: 6?

Bucket2, Bucket4?● Collisions!

Page 7: PHP Arrays

Hashmaps and collisions

PHP hash function● Integers: Integer mod size● Strings: Hash function DJBX33A

PHP DOS Attack by super colliding arrays!

Fixed in 5.3.9

Page 8: PHP Arrays

What to do?

Find your use case.

Ex: Judy array

Page 9: PHP Arrays

PHP Interfaces

● Array Access○ Interface to provide accessing objects as arrays

● Iterator○ Interface for external iterators or objects that can be iterated

themselves internally.

● Countable○ Classes implementing this can be used with count() function.

Page 10: PHP Arrays

Example

SQLiteDBArray

A simple, in memory, indexed array implementation in PHP using SQLiteDB

https://github.com/maheshgattani/SQLiteDBArray

Page 12: PHP Arrays

Thank you!

Page 13: PHP Arrays

Lucidchart● Building the next generation of

collaborative web applications.● VC funded, high growth,

profitable.● Graduates from Harvard, MIT,

Stanford● Team has worked at Google,

Amazon, Microsoft.

https://www.lucidchart.com/jobs