Is PHP5 fully OO?

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

chrisgoff
Forum Newbie
Posts: 2
Joined: Mon May 23, 2005 10:27 am
Location: UK

Is PHP5 fully OO?

Post by chrisgoff »

Hi

I am trying to answer a question that is likely to come up in one of my final year exams in the next few days...is PHP5 fully object oriented, or merely object based?

I don't know an awful lot about object oriented programming if I'm honest, so what I've cobbled together is a somewhat limited understanding on which I'm basing any evaluation of this answer, so any help in pointing out mistakes will be much appreciated!

I have identified the main principles of an OO language of it being able to support; classes and instances of those classes (objects), encapsulation, inheritance, extensibility and polymorphism.

From what I've read on a variety of sites, including the PHP manual, I seem to have determined that PHP5 handles all the above. My lecturer however, seems to have the opinion that another necessity of an OO language is that it supports data hiding, and that PHP5 does not. I however, disagree, as I have read various bits and pieces about defining the visibility of classes and methods in PHP5 using the public, protected and private key words. Have I got this right?

Anyway, if anyone can shed an opinion on this question I'd much appreciate it.

Cheers
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

from what i understand PHP5 is a quite competent OOP language, but still, there are much better OOP languages (Java for eg)

many of the functionalities you require will be found in PHP5 however, bare in mind, PHP was not designed as an OOP language but rather, it was brought into the mainstream with PHP5, PHP has always had the ability for OOP design but it was somewhat limited

Ive never coded in php5 and to be honest, i have no need to for the foreseeable future, but i should imagine that PHP5 will be able to do 99% of the things you require although some stuff will probably be buggy as it is very much in its "first stage" of OOP based "language"

mcGruff etc will be able to answer you much better as i know they love OOP and can help you out
chrisgoff
Forum Newbie
Posts: 2
Joined: Mon May 23, 2005 10:27 am
Location: UK

Post by chrisgoff »

Cheers malcolm...

Look forward to hearing from any other significantly-better-phpers-than-myselfs out there :o)

Chris
User avatar
Pyrite
Forum Regular
Posts: 769
Joined: Tue Sep 23, 2003 11:07 pm
Location: The Republic of Texas
Contact:

Post by Pyrite »

Afaik, PHP5 supports data hiding by declaring class data members private instead of public. So your teacher is wrong (afaik) :)

I believe the concept data hiding is refered to as Visibility in PHP5.
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

In php4 the ability to freelly access notionally private class properties can be convenient when you're unit-testing. For example, say you have an error message passed to a trigger_error call. In a test, you can set up the error state and then:

Code: Select all

$this->assertError($foo_class->_bar_error_message);
If the error text is edited, it doesn't require a corresponding edit to the test (it would if you'd asserted a specific string). Changing the interface by adding a getter just for the test would change the apparent intent of the class ever so slightly.
Last edited by McGruff on Sat Aug 06, 2005 1:34 pm, edited 1 time in total.
User avatar
andre_c
Forum Contributor
Posts: 412
Joined: Sun Feb 29, 2004 6:49 pm
Location: Salt Lake City, Utah

Post by andre_c »

I would consider PHP5 fully object oriented, but i found this on the manual:
In PHP 5, object comparison is more complicated than in PHP 4 and more in accordance to what one will expect from an Object Oriented Language (not that PHP 5 is such a language).
which makes me think that the makers of php don't think so

... or maybe they just don't want to argue about it :D
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

this question can be easily answered once you answer the following question:

- What do you expect from a language that claims itself to be OOP? What are the requirements?

- Lookup if you can achieve them easily with PHP.
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

McGruff wrote:In php4 the ability to freelly access notionally private class properties can be convenient when you're unit-testing. For example, say you have an error message passed to a trigger_error call. In a test, you can set up the error state and then:

Code: Select all

$this->assertError($foo_class->_bar_error_message);
If the error text is edited, it doesn't require a corresponding edit to the test (it would if you'd asserted a specific string). Changing the interface by adding a getter just for the test would change the apparent intent of the class ever so slightly.
lmao, like i said "mcgruff will know as i know he likes OOP", i didnt bloody understand a word of what you just said mate :wink:
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

OOP in PHP is not mandatory, like in some languages. Kinda obvious, but doesn't that mean that PHP is not fully object oriented?
User avatar
Pyrite
Forum Regular
Posts: 769
Joined: Tue Sep 23, 2003 11:07 pm
Location: The Republic of Texas
Contact:

Post by Pyrite »

Therefore, php object-oriented programming can just be called 'poop' :lol:
User avatar
andre_c
Forum Contributor
Posts: 412
Joined: Sun Feb 29, 2004 6:49 pm
Location: Salt Lake City, Utah

Post by andre_c »

Ambush Commander wrote:OOP in PHP is not mandatory, like in some languages. Kinda obvious, but doesn't that mean that PHP is not fully object oriented?
hmm, would C++ be considered fully object-oriented then?
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Not really: http://en.wikipedia.org/wiki/C_Plus_Plus
Wikipedia wrote:It is a statically typed free-form multi-paradigm language supporting procedural programming, data abstraction, object-oriented programming, and generic programming.
Most languages these days, I venture, are not purely one thing: they expanded into other areas, sometimes with weird results. Perl, for example, can handle multi-leveled arrays, but it's an incredibly complex mix of references that took me several days to figure out (I definitely would not like to work with those in Perl).

Closer to home, remember when PHP just started off? It was "Personal Home Page" tools, a collection of "small tight cgi binaries written in C". But it turned out that people wanted to "use the framework for creating add-ons... ... being able to embed logic directly in their web pages for creating conditional HTML, custom tags, and other features." And then, they created the if tag. And then the else tag. And then the whole kaboodle (Shamelessly ripped off from the Introduction in Programming PHP by Rasmus Lerdorf and Kevin Tatroe).

It's kinda pointless then to argue whether or not a language is "fully" a paradigm, rather, you should ask whether or not it fulfills all the requirements of the paradigm and fulfills them well (like many previous posters mentioned).
User avatar
MathewByrne
Forum Commoner
Posts: 38
Joined: Sat Mar 27, 2004 9:49 pm
Location: Australia

Post by MathewByrne »

Having programmed in Java for the past few years I'm well aware of the capabilities of an Object Oriented language. PHP5 is a very capable OOP language. That said it doesn't have the power or versatility of Java but is a huge step forward from PHP4.

Your lecturer is wrong, PHP5 does support private and protected functions and variables. (See here for more info) and really does a lot more to promote encapsulation than previous versions.
fastfingertips
Forum Contributor
Posts: 242
Joined: Sun Dec 28, 2003 1:40 am
Contact:

Post by fastfingertips »

PHP5 is not a full OOL.

Is just enough to see how method overloading works and this will take you out of your minds :P.
User avatar
andre_c
Forum Contributor
Posts: 412
Joined: Sun Feb 29, 2004 6:49 pm
Location: Salt Lake City, Utah

Post by andre_c »

fastfingertips wrote:PHP5 is not a full OOL.

Is just enough to see how method overloading works and this will take you out of your minds :P.
err... you can't overload methods in php (i'm guessing you meant overriding)
Post Reply