4

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?

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Trufa
  • 39,971
  • 43
  • 126
  • 190

6 Answers6

5

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
  • 528,062
  • 107
  • 651
  • 834
  • Thanks that is what I though! What would be a "classic" example of OOP in PHP? – Trufa Oct 09 '10 at 00:00
  • 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 –  Oct 09 '10 at 00:06
  • 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 Oct 09 '10 at 00:12
  • @DGM will definitely take a look! – Trufa Oct 09 '10 at 00:24
4

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 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!" (I haven't looked into Smalltalk though).

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.

Gordon Gustafson
  • 40,133
  • 25
  • 115
  • 157
  • Well that is clarifying! the thing is should everything be in clases to be considered OOP? – Trufa Oct 09 '10 at 00:03
  • 1
    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. – Gordon Gustafson Oct 09 '10 at 00:07
  • Ahh ok! I think you should include part of your comment in your answer found it very helpful! – Trufa Oct 09 '10 at 00:12
  • @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 Oct 09 '10 at 09:21
3

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
  • 2,180
  • 11
  • 9
  • Thanks for the contribution. "you ask them to tell you about themselves" Very clear "analogy"! – Trufa Oct 09 '10 at 01:36
  • 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. – Explosion Pills Oct 09 '10 at 04:27
1

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

Don Kirkby
  • 53,582
  • 27
  • 205
  • 286
0

This answer is inspired by this Man and his answer.

Object-Oriented technology is often described in terms of encapsulation, polymorphism, and inheritance. But these are only identity. If object-oriented technology is to be successfully it must emphasis on the object.

When we say Object-oriented or Object-orientation it can refer to several things:

  1. Object-oriented analysis and design[OOAD]
  2. Object-oriented design[OAD]
  3. Object-oriented database
  4. Object-oriented modeling
  5. Object-oriented operating system
  6. Object-oriented programming[OOP]-->topic of concern
  7. Object-oriented software engineering
  8. Object-oriented user interface

What Pure Object Oriented Programming Language[OOP] is?

Alan Kays["Considered by some to be the father of object-oriented programming"] [Defination]5 link by Gordon:

  1. EverythingIsAnObject.

  2. Objects communicate by sending and receiving messages (in terms of objects).

  3. Objects have their own memory (in terms of objects).

  4. Every object is an instance of a class (which must be an object).

  5. The class holds the shared behavior for its instances (in the form of objects in a program list)

Now clearly it can be seen Java,C++ and PHP violates rule 1?Why bcoz int, float etc. (there are a total of eight primitive types). so it cannot be Object oriented in strict sense but some folk's considered it as OOP.

The general approach of OOP is to view a software system as a collection of interacting entities called "objects" each of which is defined by an identity, a state described in terms of member variables, and a behavior described in terms of methods that can be invoked

What OOP is not?

Object-Oriented technology is often described in terms of encapsulation, polymorphism, and inheritance. But these are only identity.

An Object Oriented system, language, or environment should include at least Encapsulation, Polymorphism, and Inheritance.

  1. Polymorphism and Inheritance are certainly patterns that facilitate OO programming, but not only bound to it

enter image description here

  1. The object-oriented paradigm isn't completely the domain of high-level programming languages -->may topic of debate but i came across this OOP in Assembly

Uncle Bob aka Bob Martin in his lecture shows How C implements Encapsulation,Inheritance,and Polymorphism LINK

  1. OO is based on modeling real-world objects // For Marketing purpose

Difference Between OOP and Functional?

This may be not be perfect answer but i gave a try,Thnks to knowledge of valley.

Note: Images are randomly found on google

enter image description here

Community
  • 1
  • 1
Linus
  • 899
  • 3
  • 19
  • 33
0

It's been a long time since this question but I came upon this article and wanted to shre the author's point of view.

PHP is not object oriented!

Trufa
  • 39,971
  • 43
  • 126
  • 190