SPL objectstorage vs SPL array vs regular array

what is the difference * using * scenerio between regular ARray, SPL array and SPL storage? It would be great if someone could give a practical example of using SPLarray and SPL objects.

+6
source share
1 answer

The main advantage of SplFixedArray is that for a specific subset of use cases for arrays, it is much faster (this subset is an array that has only integer keys and a fixed length). So for example:

 $a = array("foo", $bar, 7, ... thousands of values ..., $quux); $b = \SplFixedArray::fromArray($a); // here, $b will be much faster to use than $a 

Using this class literally may be something you could use for an array, but found that they used to be too slow. Many times this happens in complex calculations on large data sets. For your typical PHP or web site, there will not be many (if any) cases where you need to improve performance.


However, the SplObjectStorage class can be useful in all typical cases. It provides a way to map objects to other data. So, for example, perhaps you have a Route class that you want to provide a mapping to the Controller class:

 $routeOne = new Route(/* ... */); $routeTwo = new Route(/* ... */); $controllerOne = new Controller(/* ... */); $controllerTwo = new Controller(/* ... */); $controllers = new \SplObjectStorage(); $controllers[$routeOne] = $controllerOne; $controllers[$routeTwo] = $controllerTwo; // now you can look up a controller for a given route by: $controllers[$route] 
+3
source

Source: https://habr.com/ru/post/921024/


All Articles