views:

157

answers:

5

I have been reading around the definition of OOP and couldn´t get why PHP is considered object oriented.

Can this have anything to do that the "basic level" of PHP isn´t and more advanced features are?

Thanks in advance!

+5  A: 

OO features were added to PHP in stages through versions 3-5, after much of the standard library had already been created and the language was already established. Background

For this reason the standard library is not object-oriented and so everyday PHP scripts need not use any OO-style features at all. Although PHP by now has most of the standard features of an object-oriented language, many authors don't use them.

Library functions added to the language later continued to use functional style for consistency, though many extension modules do use objects.

bobince
Thanks that is what I though! What would be a "classic" example of OOP in PHP?
Trufa
The last sentence ("functional style") is... confusing at first, if one constantly thinks of functional programming (as in Haskell). But sums it up perfectly, +1
delnan
For a great example of OO style in php, look at http://www.htmlpurifier.org When I needed to add some functionality to it, the OO nature was very refreshing.
DGM
@DGM will definitely take a look!
Trufa
+1  A: 

PHP is to OO PHP as C is to C++

Gabriel
i thought that was a decent analogy :)
Gabriel
Thanks for it anyway but I think my question was much more basic, Im barely starting grasp what OOP is and really cant see the analogy. Thanks anyway!
Trufa
Even C can be OO. I think GTK+ has some sort of OO design... I had trouble grokking it though.
DGM
+1  A: 

You can write classes with PHP, but most of the core features are not object-oriented.

Don Kirkby
+2  A: 

Almost any language that allows you to create and instantiate classes can be considered object oriented.

PHP has these capabilities, but doesn't really stretch them. You can use OOP to help your code, but it isn't required. Java, C#, and C++ barely allow you to write non-OO code, as everything must be in a class.

Can this have anything to do that the "basic level" of PHP isn´t and more advanced features are?

You could say that about just about any OO language. The general definition of OO code is where you create classes and instantiate them in your code, calling methods on them from other classes. Nothing stops you from using only static methods or one super class with a 'run' method that only calls other methods inside the class, both of which would definitely NOT be object oriented. As far as I know, there aren't any languages that say "You must create classes and instantiate them or you will be banished!" Even if there were, I doubt they would get very close to mainstream because of the control-freakness.

Beginners often learn the basics while putting all their code in just one method that gets called at the stat of the program. Once they get to more 'advanced' features like methods and classes, they are offered other options. :D

CrazyJugglerDrummer
Well that is clarifying! the thing is should everything be in clases to be considered OOP?
Trufa
the general definition of OO code is where you create classes and instantiate them in your code, calling methods on them from other classes. Nothing stops you from using only static methods or one super class with a 'run' method that only calls other methods inside the class, both of which would definitely NOT be object oriented.
CrazyJugglerDrummer
Ahh ok! I think you should include part of your comment in your answer found it very helpful!
Trufa
@Trufa thanks. Mission accomplished ;)
CrazyJugglerDrummer
@CrazyJugglerDrummer Nope, thank you!
Trufa
@CrazyJuggler: Well technically in languages like C# and Java you're already instantiating (built-in) classes by simply using Strings in your code. And you'll be very hard pressed to avoid calling methods on objects since most of the standard libraries' functionality lives in instance methods (as opposed to php where it lives in top-level functions).
sepp2k
+2  A: 

There is already a sufficient (and accepted) answer here, but I thought I'd throw another log on the fire for clarity's sake.

The "class" keyword (and the enforcement of its ubiquity, as in Java) does not Object-Oriented Programming make. As CrazyJungleDrummer pointed out, it is perfectly feasible (and all too common) to write entirely procedural code in something like Java; the fact that the code lies between curly braces in a class called HelloWorld doesn't change that fact. And just hiding a bunch of functions in a class and calling them static methods isn't OOP either -- it's namespacing.

Think of a proper object as a struct (or "custom type", depending on your previous language exposure) that knows what to do. Objects are data that you don't (or shouldn't) act upon directly; you ask them to do things to themselves, and you ask them to tell you about themselves. You create entities and pass messages. OOP is about treating your data like it's all grown up and can handle itself. It's not about where the main line of code lives, but how data are treated.

Oh, and one more thing -- even in a language that is more obviously canted toward OOP, real OOP is not always the right approach. It's all about the data.

Stan Rogers
Thanks for the contribution. "you ask them to tell you about themselves" Very clear "analogy"!
Trufa
Yes, it is dubious to call a language itself Object Oriented. Only applications themselves can be OO. You can, however, say that a language supports object orientation.
tandu