Is PHP a true OOP language?

KBleivik
Site Admin
Posts: 88
Joined: Tue Jan 31, 2006 3:10 pm
Location: Moss Norway
Contact:

Is PHP a true OOP language?

Postby KBleivik » Wed Feb 01, 2006 5:56 pm

The answer to this is no, but it becomes more and more a true OOP language. What is the main difference from a true OOP language as C++?

1. Privacy.
PHP 5.0 is more OO than PHP 4.0 since it opens for privacy of class members.

2. Inheritance.
As far as I know PHP has no multiple inheritance possibility.

3. Pointers and references
If you are used to C++ or Java, a reference in PHP is not the same as a reference in C++ or Java. In those languages, a reference is a pointer to a memory location. So passing a variable by value or by reference is different in PHP. If you intend to link to the original variable, you must pass values by reference. If you pass by value (copy) and the original variable changes, your (copied) variable is not the same as the source variable.

Good Example from the PHP book above in chapter 2. & is the reference operator.

<?php
$color = 'blue';
$settings['color'] = $color; // Makes a copy
$color = 'red'; //color changes
echo $settings['color']; // Displays "blue"
?>

Then note:

<?php
$color = 'blue';
$settings['color'] = &$color; // Makes a reference
$color = 'red'; //color changes
echo $settings['color']; // Displays "red"
?>

So passing by reference allows you to keep the new variable "linked" to the original source variable.

Remember: And C++ like Java is a compiled language, while PHP is interpreted. That means that the same operation is much slower in PHP.
Kjell Gunnar Bleivik
Make it simple, as simple as possible but no simpler: | DigitalPunkt.no |

Return to “PHP”

Who is online

Users browsing this forum: No registered users and 1 guest