Today we continue to learn about SPL. This article is about a series of functions provided in SPL. In fact, we have already touched on some of the functions provided in SPL in many previous articles. Let’s study it in detail this time.
Class information related functions are mainly used to view the information about a class, not to operate the functions of the class.
First, if you want to see which interface the current class implements, you simply use a class_implements() method.
interface A {}
interface B {}
class TestA implements A,B {}
var_dump(class_implements(new TestA));
// array(2) {
// ["A"]=>
// string(1) "A"
// ["B"]=>
// string(1) "B"
// }
Copy the code
As you can see, it returns an array showing the name of the interface implemented by our current class object. If the class we are querying does not implement any interface, it returns an empty array.
var_dump(class_implements(new stdClass()));
// array(0) {
// }
Copy the code
Similarly, we can look at the parent class of a class object.
class C{}
class TestB extends C {}
var_dump(class_parents(new TestB));
// array(1) {
// ["C"]=>
// string(1) "C"
// }
Copy the code
Even though PHP is a single-inheritance language, using class_parents() still returns an array. An empty array is also returned if the class object has no parent.
class TestC {
use D, E;
}
var_dump(class_uses(new TestC));
// array(2) {
// ["D"]=>
// string(1) "D"
// ["E"]=>
// string(1) "E"
// }
Copy the code
Finally, you can use the class_uses() function to get information about the traits used by the current class object.
Class hash and class ID Those of you who have done Java development have probably seen that all classes have a hashCode() method. What this method does in Java is return the Hash value of an object. This method is usually used to determine whether objects are equal or unique. In Java, all classes inherit from the base class Object by default, and this base class has its own method.
However, in PHP, classes do not have such a global base class, so there is no such method. Obviously, it is up to other extension tools to provide this capability. As luck would have it, that’s exactly what SPL provides.
var_dump(spl_object_hash(new TestA));
// string(32) "000000000ed109570000000025e36d74"
$a = new TestA;
var_dump(spl_object_hash($a));
// string(32) "000000000ed109570000000025e36d74"
var_dump(spl_object_id(new TestA));
// int(2)
var_dump(spl_object_id($a));
// int(1)
Copy the code
The spl_object_hash() function is used to retrieve the Hash value of an object, which is the full Hash value, unlike Java’s hashCode() method, which returns a numeric value. Objects instantiated by the same class template return the same content.
Spl_object_id () returns the ID of the object. Its results are different for different new, that is, instantiated objects. If the object persists, its ID value does not change, and if the object is destroyed, its ID value is reclaimed and given to other objects. We can actually just print the object and see the ID.
var_dump($a);
// object(TestA)#1 (0) {
// }
var_dump(new TestA);
// object(TestA)#2 (0) {
// }
Copy the code
The number after the pound sign is the ID value of our object, which is what spl_object_id() gets.
This function returns information about all available classes in the SPL library.
var_dump(spl_classes()); // array(55) { // ["AppendIterator"]=> // string(14) "AppendIterator" // ["ArrayIterator"]=> // string(13) "ArrayIterator" // ["ArrayObject"]=> // string(11) "ArrayObject" // ["BadFunctionCallException"]=> // string(24) "BadFunctionCallException" // ["BadMethodCallException"]=> // string(22) "BadMethodCallException" // ["CachingIterator"]=> // string(15) "CachingIterator" // ["CallbackFilterIterator"]=> // string(22) "CallbackFilterIterator" // ["DirectoryIterator"]=> // string(17) "DirectoryIterator" // ["DomainException"]=> // string(15) "DomainException" // ["EmptyIterator"]=> // string(13) "EmptyIterator" // ["FilesystemIterator"]=> // String (18) "FilesystemIterator" / / / "FilterIterator" = > / / string (14) "FilterIterator" / / / "GlobIterator" = > / /.......................................... ... / /................................................Copy the code
As you can see, many of the classes of information that we talked about are available here.
Iterator_to_array () Iterator_array () Iterator_to_array () iterator_to_array() iterator_to_array() iterator_to_array()
$iterator = new ArrayIterator(['a'=>'a1', 'b'=>'b1', 'c'=>'c1']);
var_dump(iterator_to_array($iterator, true));
// array(3) {
// ["a"]=>
// string(2) "a1"
// ["b"]=>
// string(2) "b1"
// ["c"]=>
// string(2) "c1"
// }
Copy the code
We can imagine that inside the function, we are actually going through the iterator with foreach() and returning all the results in an array. This function also takes a second argument that causes the keys to be arranged using the default array index instead of the original key value.
var_dump(iterator_to_array($iterator, false));
// array(3) {
// [0]=>
// string(2) "a1"
// [1]=>
// string(2) "b1"
// [2]=>
// string(2) "c1"
// }
Copy the code
In addition to getting the iterator traversal directly, we can also get the number of elements inside the iterator directly through a function.
var_dump(iterator_count($iterator)); // int(3)
Copy the code
Iterator_count () is like an iterator version of the count() function.
function printCaps($iterator){
echo strtoupper($iterator->current()), PHP_EOL;
return true;
}
iterator_apply($iterator, "printCaps", array($iterator));
// A1
// B1
// C1
Copy the code
Finally, the iterator_apply() function lets us iterate over an iterator with a specified callback.
Auto-loading related functions For auto-loading functions, we have already touched on this in the first series of articles on Composer. But all we learned was the spl_autoload_register() function. Today we’ll look at two more functions, but first we’ll look at the use of the spl_autoload_register() function.
function autoloadA($class){ if(is_file('./autoloadA/' . $class . '.php')){ require_once './autoloadA/' . $class . '.php'; } } spl_autoload_register('autoloadA'); spl_autoload_register(function($class){ if(is_file('./autoloadB/' . $class . '.php')){ require_once './autoloadB/' . $class . '.php'; }}); $sky = new Sky(); $sky->result(); // This is Sky PHP! $planet = new Planet(); $planet->result(); // This is Planet PHP!Copy the code
In this test code, we registered two spl_autoload_registers () in the form of callback and anonymous functions. This allows us to look up the two Autoloads when we use classes that are not defined in the current file. Spl_autoload_register () has an advantage over __autolod() in that it maintains an auto-loaded list, equivalent to multiple __autoload() functions. We can use another function to see how many auto-loaded functions we have registered.
var_dump(spl_autoload_functions());
// array(2) {
// [0]=>
// string(9) "autoloadA"
// [1]=>
// object(Closure)#3 (1) {
// ["parameter"]=>
// array(1) {
// ["$class"]=>
// string(10) "<required>"
// }
// }
// }
Copy the code
Of course, can be continuously registered in can also be deleted.
spl_autoload_unregister('autoloadA');
var_dump(spl_autoload_functions());
// array(1) {
// [0]=>
// object(Closure)#3 (1) {
// ["parameter"]=>
// array(1) {
// ["$class"]=>
// string(10) "<required>"
// }
// }
// }
Copy the code
Summary how, all the way down is not found in fact many functions we have been exposed to in the daily development of learning. These functions are the functions provided by the SPL extension library. In fact, through the learning of these several articles, we have found that the SPL extension library provides us with some very basic functions such as data structures, iterators, design patterns and so on. There are many things that are really simpler and more convenient than their own implementation. This includes file manipulation and implementation of design patterns, which we will continue to study in the next article.