PHP

How Object Oriented is PHP?

How OO is PHP?

How “object-oriented” is PHP? Your answer to that question probably depends on your particular litmus tests for object-orientedness. In this sidebar, we offer a whirlwind tour of features that typically show up in OOP languages and briefly discuss the extent to which PHP supports them. Some of these issues are explored more broadly in the section “Advanced OOP Features,” later in this chapter. (Note: This sidebar is really only of interest to developers who are coming to PHP from a different OO language; everyone else may want to skip this game of buzzword bingo.)

Single inheritance
PHP allows a class definition to inherit from another class, using the extends clause. Both member variables and member functions are inherited.

Multiple inheritance
PHP offers no support for multiple inheritance and no notion of interface inheritance as in Java. Each class inherits from, at most, one parent class (though a class may implement many interfaces).


Constructors
Every class can have one constructor function, which in PHP is called __construct(). Note that there are two underscore characters at the front of that function name. Because prior to PHP5 (under Zend Engine 1), a class's constructor function had the same name as the class, PHP still allows (but discourages) that strategy for purposes of backward compatibility. Constructors of parent classes are not automatically called but must be invoked explicitly.

Destructors
PHP supports explicit destructor functions as of version 5. The destructor function of a class is always called __destruct().

Encapsulation/access control
PHP supports public, private, and protected variables and member functions as of version 5. Polymorphism/overloading

PHP supports polymorphism in the sense of allowing instance of subclasses to be used in place of parent instances. The correct member function will be dispatched to at runtime. There is no support for method overloading, where dispatch happens based on the method's signature— each class only has one member function of a given name. However, PHP's weak typing and support for variable numbers of arguments makes workarounds possible. See the section “Simulating polymorphism” later in this chapter (in the section “Advanced OOP Features”).

Early versus late binding
Two equally good answers are: 1) The question doesn't arise, due to PHP being loosely typed, and 2) All binding is late. In PHP, values are typed but variables are not, so there is no question about what method to call when the variable is of a different type than the value.

Static (or class) functions
PHP offers static member variables and static methods as of version 5. It is also possible to call member functions via the Classname::function() syntax.

Introspection
PHP offers a wide variety of functions here, including the capability to recover class names, member function names, and member variable names from an instance. (See the section “Introspection Functions,” later in this chapter.)

From Chapter 20 - Object-Oriented Programming with PHP, PHP5 and MySQL Bible.

Twitter
LinkedIn
YouTube
Instagram