> Articles > PHP articles > PHP and OOP techniques (Object-Oriented Programming)
PHP and OOP techniques (Object-Oriented Programming)
|
Q: How many object programmers does it take to screw in a lightbulb?
A: None. You just send a change bulb message to the socket object!
|
OOP (Object-Oriented Programming) is an approach used for the development of relatively big
projects with a long life. PHP is getting more and more popular
and is now used in many professional environments. Thus a OOP
techniques are getting more used in PHP projects. Here
we discuss OOP techniques appropriate for use in PHP.
When starting a project one not always knows if it is going to be
a big long term project or if it will be rather small and very rarely modified.
In such casses it's recommended to develop project from the beginning
in OOP style, since developing programs using OOP approach results in the more readable
and flexible code.
It really does not matter whether to use OOP in small projects, or in projects with
a relatively short life.
On the other hand, if you intend to add more features
and modify your project later, OOP will save you some time.
Another remarkable thing about OOP (which sometimes can be applied to
many programming aspects) is that one will not properly use OOP approach
untill having enough real life experience. University courses and tutorials
by themselfs are just not enough.
People study OOP, get good marks, get diplomas, but the still only use
the procedurall programming techniques. The only way to learn to use OOP properly
is the experience, learning by trial and error.
Sometimes beginners think, if they use classes, then they are using
OOP. That is definetely not true. You can use classes and still write
programs in procedurall style.
Here we list a number of OOP concepts and discuss their usage in PHP.
-
Multiple inheritance is not implemented in PHP and it is very unlikely that it will ever be implemented.
Many other OOP languages do not have the multiple inheritance feature as well (Java, Object Pascal, C#, etc).
-
You can not have several functions that differ in a number of parameters or parameters types.
That is explained by two facts.
- Variables are not declared in PHP you can not differ two functions by parameters types.
- One can call any function declared in PHP with any number of parameters not less
than declared in the function definition. That funcion still will be called and additional
parameters will be available via the
func_num_args(),
func_get_args() and
func_get_arg() functions.
So one can build the following workaround
function foo()
{
switch(func_num_args())
{
case 1:
// 1 argument
break;
case 2:
$args = func_get_args();
if (gettype($args[0]) == 'integer' && gettype($args[1]) == 'array')
{
// two arguments, an integer and an array
}
break;
}
}
-
Function overriding works fine.
You can override member functions in child classes. Example
class A { function foo() { echo "A::foo();"; } }
class B extends A { function foo() { echo "B::foo();"; } }
$obj = new B();
$obj->foo();
outputs B::foo();.
-
Base class function calls work like in C++.
class A { function foo() { echo "test A"; } }
class B extends A { function foo() { A::foo(); echo " from class B"; } }
$obj = new B();
$obj->foo();
outputs test A from class B.
-
Virtual functions.
By definition all class member functions are virtual. Each
object "knows" its class and all its functions.
A short example
class A
{
function foo() { echo "1"; }
function boo() { $this->foo(); }
}
class B extends A
{
function foo() { echo "2"; }
}
$b = new B();
$b->boo();
outputs 2 demonstrating that the function
foo() is virtual.
-
Polymorphism.
Since all functions are virtual and variables are not declared, polymorphism is present by definition.
- Static functions.
If class member function does not reference any class variables, it can be called
as static, i.e.
class A { function foo() { return "test"; } }
echo A::test();
outputs test.
- No static class variables.
It is arguable whether static class variables are needed in PHP or not.
Some OOP principles state that static variables should not be used. There are
some exceptions when they are usefull, for example
$HTTP_POST_DATA and similar.
One can have global static variables outside of
the class and access it via
$GLOBALS or declare them global explicitly whenever
accessing.
- No templates (parametrized classes) since they are almost not needed
because variables are not declared and there are no strict types. For instance,
you can hold objects the belong do different classes as well as numbers
and strings in the same array.
- No private/protected variables and functions. It would not hurt if we have
them in PHP, but they are not really demanded in a language like PHP.
- There are no functions to cast from one class to another like in C++, but that
feature is specific to C++ and is not even present in Java.
-
There are no C like structures, but in fact one does not need them.
The only difference between C++ structures and C++ classes is default setting for private member data and functions.
So with classes one does not need structures in PHP.
Many new OOP features like exception handling, namespaces and other
are going to be implemented in the Zend Engine version 2.0. There is no planned
release date yet. See http://www.zend.com/zend/future.php
If you would like to study OOP in details we recommend to read
Timothy A Budd - An Introduction to Object-Oriented Programming, ISBN 0-201-76031-2.
See ftp://ftp.cs.orst.edu/pub/budd/oopintro/3rdEdition/info.html.
Although PHP is not discussed in this book, it contains
examples in C++, SmallTalk, Object Pascal and Java. Also it shows that
language is not the main issue, the most important thing is usage of
OOP techniques.
Mikhail Dubakov translated this article into Russian. Translation can be found at OOP in PHP by Valentin Samko.
Copyright © Val Samko, DigiWays. Written by Valentin Samko mailto:val@digiways.com
|