Top 100 classesThe 100 PHP classes that you have to know

Here is the top 100 PHP classes : it is the list of the most often used PHP native classes.

The classes are ranked in order of project frequency. In a corpus of 2500 OSS projects, the classes (and a few interfaces) were detected. They may be used by instantiation, as a type hint (property, argument, function, instanceof), by extension, by implementation, or with the :: operator. One occurrence per project is enough, and they are then not differentiated.

For example, almost 46% projects use the Exception class. That is used on average 26 times, but may clock up to 450 usage in the same project.

Click on the link to go to the documentation. Some extra insights are at the bottom of the top 100.

1 Exception 46.81%
2 InvalidArgumentException 44.81%
3 RuntimeException 36.85%
4 Closure 33.01%
5 OutOfBoundsException 25.34%
6 throwable 24.39%
7 ReflectionClass 22.34%
8 stdClass 20.68%
9 DateTime 19.51%
10 LogicException 19.13%
11 RecursiveIteratorIterator 17.88%
12 RecursiveDirectoryIterator 17.17%
13 UnexpectedValueException 17.13%
14 ArrayIterator 16.55%
15 traversable 15.55%
16 ReflectionMethod 14.67%
17 BadMethodCallException 14.46%
18 DateTimeZone 13.59%
19 DOMDocument 13.30%
20 iterator 12.63%
21 ReflectionProperty 11.96%
22 ReflectionFunction 11.92%
23 datetimeinterface 11.88%
24 ArrayObject 11.63%
25 countable 11.30%
26 SplFileInfo 11.30%
27 TypeError 11.25%
28 ReturnTypeWillChange 11.09%
29 DateInterval 11.09%
30 Generator 10.96%
31 Error 10.88%
32 FilesystemIterator 10.80%
33 DateTimeImmutable 10.67%
34 arrayaccess 10.50%
35 ReflectionException 10.42%
36 PDO 10.34%
37 ErrorException 9.71%
38 iteratoraggregate 9.46%
39 SimpleXMLElement 9.46%
40 ReflectionObject 9.25%
41 ReflectionNamedType 9.17%
42 DirectoryIterator 9.09%
43 DOMXPath 8.13%
44 DOMElement 7.88%
45 Attribute 7.79%
46 ReflectionParameter 7.75%
47 DOMNode 7.63%
48 ValueError 7.63%
49 PDOException 7.63%
50 SplObjectStorage 7.59%
51 stringable 7.42%
52 ReflectionUnionType 6.71%
53 ZipArchive 6.59%
54 SplFileObject 6.25%
55 ReflectionType 5.79%
56 jsonserializable 5.71%
57 DomainException 5.42%
58 memcached 5.34%
59 redis 5.29%
60 Locale 5.09%
61 JsonException 5.04%
62 ReflectionFunctionAbstract 5.00%
63 __PHP_Incomplete_Class 5.00%
64 IteratorIterator 5.00%
65 PDOStatement 4.88%
66 reflector 4.88%
67 DatePeriod 4.67%
68 ReflectionIntersectionType 4.63%
69 DOMText 4.54%
70 Phar 4.46%
71 Normalizer 4.29%
72 FilterIterator 4.29%
73 RegexIterator 4.09%
74 ReflectionClassConstant 3.96%
75 NumberFormatter 3.92%
76 XMLReader 3.92%
77 AppendIterator 3.92%
78 IntlDateFormatter 3.63%
79 DOMException 3.63%
80 ResourceBundle 3.58%
81 recursiveiterator 3.54%
82 MessageFormatter 3.50%
83 finfo 3.50%
84 DOMAttr 3.46%
85 ParseError 3.46%
86 unitenum 3.42%
87 Transliterator 3.38%
88 Reflection 3.38%
89 ReflectionExtension 3.38%
90 SplDoublyLinkedList 3.33%
91 rediscluster 3.25%
92 DOMImplementation 3.21%
93 DOMDocumentType 3.17%
94 ReflectionAttribute 3.08%
95 DOMCharacterData 3.08%
96 serializable 3.04%
97 redisarray 3.04%
98 ReflectionGenerator 2.96%
99 WeakReference 2.92%
100 GMP 2.88%

Top 100 insights

Here are some extra insights.

  • The most commonly use PHP class is used in less than 50% of the projects. For comparison, a commonly used function is used in 81% of projects.

  • The most commonly used PHP class are exceptions. Most often is for catching them when they are thrown by some other part of the software (component, extension, etc.), are more rarely, to throw them.

  • The Exception class usage may be very ambiguous : when it is extended, it tends to hide its usage by giving it another name; On the other hand, catching Exception as a catch-all (sic) makes it a very common class.

  • Stdclass usage is a good 8th. Compared to the others, it is a hidden class. For example, the result of json_decode() or (object) is a Stdclass, without ever mentioning it.

  • The same could apply to Closure and Generator.

  • Some extensions manage to place their classes in that list : PDO, intl, imagick, redis, ast, xmlwriter, zip, sodium

  • SPL and the iterators are still very underused

  • JsonException usage levels shows that it is not a default option.

  • No one should use __php_incomplete_class… ever.

Challenges

If you are learning PHP, it is a good idea to review the 100 classes ranked here : they are the classes you’ll find the most often when landing on a coding team. They are not the only ones, but you’ll be better equiped by learning them.