#native_company# #native_desc#

Three Advanced Object-Oriented PHP Features You Need to Know

By W. Jason Gilmore
on June 15, 2010

Although PHP didn’t start out as an object-oriented language, growing interest in building object-oriented PHP applications led the development team to reconsider PHP’s initially weak OOP implementation, making a much more capable and mature set of features available with the version 5 release. These vast improvements opened up an entirely new set of opportunities for the PHP community, including most notably the development of advanced frameworks such as the Zend Framework, CakePHP, and Symfony.
In this article I’ll introduce you to three of PHP’s advanced object-oriented features which seem to not have garnered the attention they deserve. The topics discussed here should be useful whether you’re a relative newcomer to object-oriented development and are looking to expand your knowledge, or have a background using languages such as Java or C# and are trying to learn more about what PHP has to offer.


Reflection is a powerful feature which allows you to learn more about class properties using metadata. This ability to dynamically learn more about class internals can prove very useful when creating general solutions for documenting (see PHPDoc) and testing (see PHPUnit) code. PHP’s reflection API provides a large number of methods capable of retrieving information about practically every aspect of a class, including class comments, methods, information about any parent classes, properties, and property scope. Consider for instance the following class:
<?php class Account { private $_name; private $_ssn; private $_city; public function setName($name) { $this->_name = $name; } public function getName() { return $this->_name; } } $reflection = new ReflectionClass('Account'); $methods = $reflection->getMethods(); foreach ($methods AS $method) { echo "Method: {$method->name}<br />"; } ?>
Executing this script produces the following output:

Method: setName Method: getName 

Understanding how easy it is to obtain class information using the reflection API gives you a pretty good idea of how solutions such as the aforementioned PHPDoc and PHPUnit are possible! See the PHP manual for more information about what’s possible.