 | News Feed |
 | Jobs Feed |
Sections
|
| feed this: |  |
Jeremy Cook's Blog: Implementing the ArrayAccess Interface
by Chris Cornutt February 02, 2012 @ 13:56:43
Jeremy Cook is back with the next part of his series looking at the handy features PHP's SPL provides. In this new post he looks at the ArrayAccess interface and how it can make your data more accessible to PHP's own array handing functions.
ArrayAccess allows you to treat an object that implements it as if it is an array for the purposes of setting, unsetting and retrieving data from it. Please note the emphasis in the last sentence! ArrayAccess does not make an object behave like an array in any other way. If you pass an object that implements ArrayAccess to a PHP array function such as in_array() you'll still get an error. This will become a little clearer with some of the examples below.
He shows what you'll need to use this interface in your class - implementing the interface and defining a set of four methods to get/set and check for the value in your array. He includes a practical example of pulling data back from an API and wrapping it in a class to make accessing it simpler (also implementing the Countable interface as well, see the previous post for more on that). Code is include to illustrate how it can be used.
voice your opinion now!
arrayaccess spl tutorial array standardphplibrary countable
PHPClasses.org: PHP Vulnerability May Halt Millions of Servers
by Chris Cornutt January 12, 2012 @ 08:21:55
On the PHPClasses.org blog today there's a new post looking at the security vulnerability that effected not only PHP but lots of other languages making them susceptible to attack from the outside.
In PHP and several other languages used to implement Web applications, arrays are used to store the values of request variables such as $_GET, $_POST, $COOKIE, etc.. IF you receive a request with a large number of request values, until recent versions PHP may run into trouble.
He goes on to explain why there's an issue with the array overloading and what PHP has done in recent releases to help correct the issue - the max_input_vars setting in the php.ini. He also points out that this is not a new issue - it was originally identified back in 2003 (with a video of the original presentation). He points out that the most recent releases of the PHP language have this fix in them and, if at all possible, you should upgrade to protect your applications.
voice your opinion now!
vulnerability server array overload upgrade
Nikita Popov's Blog: Supercolliding a PHP array
by Chris Cornutt December 29, 2011 @ 12:15:30
In a new post to his blog Nikita Popov talks about a little trick with inserting values into arrays that can make it take a lot longer than it should (because of how PHP stores its array values in hashtables).
PHP internally uses hashtables to store arrays. The above creates a hashtable with 100% collisions (i.e. all keys will have the same hash). [...] Because every hash function has collisions this C array doesn't actually store the value we want, but a linked list of possible values. [...] Normally there will be only a small number of collisions, so in most cases the linked list will only have one value. But the [included script] creates a hash where all elements collide.
He explains why it works, noting that it's relatively simple to do in PHP because of how it applies a table mask. The slowness comes in when PHP is forced to go through the entire list when it tries to insert. Because of this issue, there's the potential for a Denial of Service attack that could potentially take a server down. There's a fix already in place for the problem, though, so keep an eye out for the next release (that will include a max_input_vars setting to prevent it).
voice your opinion now!
collision array hashtable mask denialofservice overload
Zend PHP Certification Blog: PHP Sorting Functions
by Chris Cornutt December 21, 2011 @ 11:39:06
On the "Zend PHP Certification" blog (study notes), there's sort and natsort).
In all the countless hours I've spent with php, I've maybe used three or four of these sorting functions. I really had no idea that there is a total of eleven functions used for sorting arrays. Anyway, I'm betting that it may be useful to have these memorized before I take the Zend PHP Certification Exam so here is a brief overview of each one.
He talks about the various flags that can be used in the sorting (for regular, numeric, string and locale-based string handling) and the parameters to call for normal sorting, "natural" sorting, reverse key sorting and others. You can find specifics on these array sorting methods in the PHP manual.
voice your opinion now!
sorting function array natural reverse key user
Nikic's Blog: How big are PHP arrays (and values) really? (Hint BIG!)
by Chris Cornutt December 16, 2011 @ 10:28:39
In this recent blog post nikic takes an in-depth look at how large PHP arrays really are - how memory is used in the creation and management of these handy PHP variable types.
In this post I want to investigate the memory usage of PHP arrays (and values in general) using the following script as an example, which creates 100000 unique integer array elements and measures the resulting memory usage. [...] How much would you expect it to be? [...] Now try and run the above code. You can do it online if you want. This gives me 14649024 bytes. Yes, you heard right, that's 13.97 MB - eightteen times more than we estimated.
He goes into the details of PHP's memory management and breaks it down into the different totals (for 64 bit and 32 bit OSes) and details on each - zvalue_value, zvalue, cycles collector, Zend MM allocator and the buckets used to isolate one array (hash table/dictionary) from another.
What does this tell us? PHP ain't C. That's all this should tell us. You can't expect that a super dynamic language like PHP has the same highly efficient memory usage that C has. You just can't.
voice your opinion now!
memory management array datatype backend c
PHPBuilder.com: PHP Arrays Advanced Iteration and Manipulation
by Chris Cornutt December 09, 2011 @ 12:50:11
In this new tutorial from PHPBuilder.com, Jason Gilmore shows you some of the more advanced things you can do with arrays in PHP (specifically in the areas of iterating through them and manipulating their contents).
Sporting more than 70 native array-related functions, PHP's array manipulation capabilities have long been one of the language's most attractive features. [...] There are however many array-related tasks which ask a bit more of the developer than merely knowing what part of the manual one needs to consult. Many such tasks require a somewhat more in-depth understanding of the native features, or are possible only when a bit of imagination is applied to the problem.
In his examples he shows how to do things like sorting a multi-dimensional array, iterating recursively (with the help of a RecursiveArrayIterator), converting an object to an array and doing "natural" sorting on an array's contents.
voice your opinion now!
array manipulation advanced iteration spl recursive sort
Davey Shafik's Blog: Faster Arrays
by Chris Cornutt November 07, 2011 @ 08:54:58
In this new post to his blog Davey Shafik looks at an alternative to the traditional arrays most scripts use - something a little faster and more specific: SplFixedArray, part of the Standard PHP Library included with every release.
The SplFixedArray class provides a super-fast, fixed size array implementation. There are some limitations however, first you must use numeric keys and secondly you cannot use anonymous assignment (i.e. $array[] = 'value';). You'll notice one requirement was missing, that it should have a fixed size. While having a fixed size is what will bring you the speed increase it's actually not a requirement that the size be fixed.
Because of these restrictions, the SplFixedArray is faster than its cousin - between 20 and 40 percent faster, depending on the size of the array. He includes a few snippets in the the post - one showing how he benchmarked the differences against simple arrays and another showing a more advanced example with another SPL type, a FilterIterator.
voice your opinion now!
spl splfixedarray array benchmark filteriterator
PHPMaster.com: Array Handling Functions
by Chris Cornutt October 11, 2011 @ 08:37:55
On PHPMaster.com today there's a new tutorial introducing you to the array handling features in PHP - sorting, slicing and more.
In my previous article on PHP arrays I suggested a number of things that are tables and therefore can also be expressed as arrays. In this article I'll use a pack of playing cards to explore some of the built-in array functions most often needed by PHP programmers.
To highlight some of the array-handling functions PHP offers, I'll be using some components of Buraco - a game very popular in my part of the world and quite similar to Rummy.
In the example he represents a deck of cards with an array of values like "A", "03" and "13". This array is then looped to make the full set of 52 cards and shuffled to deal a "hand". Array functions put to use include array_rand, in_array and sort.
voice your opinion now!
array handling function tutorial example buraco card game
PHPMaster.com: Introduction to PHP Arrays
by Chris Cornutt September 20, 2011 @ 08:54:16
On the PHPMaster.com site today, there's a good introduction to a basic data type in PHP - working with arrays. This tutorial is a low level look at what arrays are and how to work with them (briefly).
Tables organize data in such a way that we can easily find correlations or perform straightforward computations. A array is essentially a way to organize data in a table-like manner. The name "array" comes from the same Latin roots as the word "arrangement."
If you're anything other than completely new to the language, this post won't help you much. If you're new to programming, though, learning about arrays in PHP is key to your budding development skills. For more in-depth looks at using arrays, checkout these results.
voice your opinion now!
introduction array datatype language beginner
Matt Farina's Blog: SplFixedArray, An Underutilized PHP Gem
by Chris Cornutt September 09, 2011 @ 10:43:11
Matt Farina has a new post today looking at an "underutilized gem" he's found in the offerings of the Standard PHP Library (SPL) - the SplFixedArray.
Arrays in PHP are not arrays per the typical array data type. Instead, as Matt Butcher recently pointed out arrays in PHP are similar to hashes in other languages. This can be a very important point to know when tracking down bugs in code and to programmers coming to PHP from other languages. But, what if we wanted something like a traditional array data type? Maybe something that preserved numeric order. Enter SplFixedArray.
He gives an example of using the SplFixedArray object versus the normal array variables in a simple PHP snippet showing the preservation of numbering order. He also touches on the memory consumption difference between the two, with the fixed array coming in quite a bit lower than the normal array data type (around 25% based on his basic testing). There are some catches to using it, though including incompatibility with array methods and the fact that it doesn't implement things like Iterator or Countable interfaces.
voice your opinion now!
splfixedarray array replacement issues performance memory usage
|
Community Events
Don't see your event here? Let us know!
|